下面列出了android.graphics.RectF#width ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Informs the CropOverlayView of the image's position relative to the
* ImageView. This is necessary to call in order to draw the crop window.
*
* @param boundsPoints the image's bounding points
* @param viewWidth The bounding image view width.
* @param viewHeight The bounding image view height.
*/
public void setBounds(float[] boundsPoints, int viewWidth, int viewHeight) {
if (boundsPoints == null || !Arrays.equals(mBoundsPoints, boundsPoints)) {
if (boundsPoints == null) {
Arrays.fill(mBoundsPoints, 0);
} else {
System.arraycopy(boundsPoints, 0, mBoundsPoints, 0, boundsPoints.length);
}
mViewWidth = viewWidth;
mViewHeight = viewHeight;
RectF cropRect = mCropWindowHandler.getRect();
if (cropRect.width() == 0 || cropRect.height() == 0) {
initCropWindow();
}
}
}
private int pointToAlpha(int x){
final RectF rect = mAlphaRect;
final int width = (int) rect.width();
if(x < rect.left){
x = 0;
}
else if(x > rect.right){
x = width;
}
else{
x = x - (int)rect.left;
}
return 0xff - (x * 0xff / width);
}
/**
* Informs the CropOverlayView of the image's position relative to the ImageView. This is
* necessary to call in order to draw the crop window.
*
* @param boundsPoints the image's bounding points
* @param viewWidth The bounding image view width.
* @param viewHeight The bounding image view height.
*/
public void setBounds(float[] boundsPoints, int viewWidth, int viewHeight) {
if (boundsPoints == null || !Arrays.equals(mBoundsPoints, boundsPoints)) {
if (boundsPoints == null) {
Arrays.fill(mBoundsPoints, 0);
} else {
System.arraycopy(boundsPoints, 0, mBoundsPoints, 0, boundsPoints.length);
}
mViewWidth = viewWidth;
mViewHeight = viewHeight;
RectF cropRect = mCropWindowHandler.getRect();
if (cropRect.width() == 0 || cropRect.height() == 0) {
initCropWindow();
}
}
}
@Override public void onMarkerViewMeasure(RectF viewRect, Matrix matrix, float highlightPointX,
float highlightPointY, String[] markerText, @Size(min = 4) @NonNull float[] markerViewInfo) {
if (viewRect.top > highlightPointY || highlightPointY > viewRect.bottom) {
return;
}
markerTextPaint.getTextBounds(markerText[1], 0, markerText[1].length(), textRect);
width = Math.max(width,
textRect.width() + (attribute.markerBorderLRPadding - attribute.markerBorderWidth) * 2);
height = Math.max(height,
textRect.height() + (attribute.markerBorderTBPadding - attribute.markerBorderWidth) * 2);
highlightPointY = highlightPointY - height / 2;
if (highlightPointY < viewRect.top) {
highlightPointY = viewRect.top + inset;
}
if (highlightPointY > viewRect.bottom - height) {
highlightPointY = viewRect.bottom - height - inset;
}
if (yMarkerAlign == GridMarkerAlign.LEFT) {
markerInsets.left = viewRect.left + inset;
} else if (yMarkerAlign == GridMarkerAlign.RIGHT) {
markerInsets.left = viewRect.right - width - inset;
} else if (highlightPointX > viewRect.left + viewRect.width() / 2) {
markerInsets.left = viewRect.right - width - inset;
} else {
markerInsets.left = viewRect.left + inset;
}
markerInsets.top = highlightPointY;
markerInsets.right = markerInsets.left + width;
markerInsets.bottom = markerInsets.top + height;
markerViewInfo[0] = markerInsets.left - inset;
markerViewInfo[2] = markerInsets.right + inset;
}
private void checkBorder() {
RectF rect = getMatrixRectF();
float deltaX = 0;
float deltaY = 0;
if (rect.width() >= mClipBorder.width()) {
if (rect.left > mClipBorder.left) {
deltaX = -rect.left + mClipBorder.left;
}
if (rect.right < mClipBorder.right) {
deltaX = mClipBorder.right - rect.right;
}
}
if (rect.height() >= mClipBorder.height()) {
if (rect.top > mClipBorder.top) {
deltaY = -rect.top + mClipBorder.top;
}
if (rect.bottom < mClipBorder.bottom) {
deltaY = mClipBorder.bottom - rect.bottom;
}
}
mScaleMatrix.postTranslate(deltaX, deltaY);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (this.photoViewAttacher == null)
return false;
ImageView imageView = photoViewAttacher.getImageView();
if (null != photoViewAttacher.getOnPhotoTapListener()) {
final RectF displayRect = photoViewAttacher.getDisplayRect();
if (null != displayRect) {
final float x = e.getX(), y = e.getY();
// Check to see if the user tapped on the photo
if (displayRect.contains(x, y)) {
float xResult = (x - displayRect.left)
/ displayRect.width();
float yResult = (y - displayRect.top)
/ displayRect.height();
photoViewAttacher.getOnPhotoTapListener().onPhotoTap(imageView, xResult, yResult);
return true;
}else{
photoViewAttacher.getOnPhotoTapListener().onOutsidePhotoTap();
}
}
}
if (null != photoViewAttacher.getOnViewTapListener()) {
photoViewAttacher.getOnViewTapListener().onViewTap(imageView, e.getX(), e.getY());
}
return false;
}
public void fling(int viewWidth, int viewHeight, int velocityX, int velocityY) {
final RectF rect = getDisplayRect();
if (null == rect) {
return;
}
final int startX = Math.round(-rect.left);
final int minX, maxX, minY, maxY;
if (viewWidth < rect.width()) {
minX = 0;
maxX = Math.round(rect.width() - viewWidth);
} else {
minX = maxX = startX;
}
final int startY = Math.round(-rect.top);
if (viewHeight < rect.height()) {
minY = 0;
maxY = Math.round(rect.height() - viewHeight);
} else {
minY = maxY = startY;
}
mCurrentX = startX;
mCurrentY = startY;
if (DEBUG) {
Log.d(LOG_TAG, "fling. StartX:" + startX + " StartY:" + startY + " MaxX:" + maxX + " MaxY:" + maxY);
}
// If we actually can move, fling the scroller
if (startX != maxX || startY != maxY) {
mScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, 0, 0);
}
}
private RectF calcFrameRect(RectF imageRect) {
float frameW = getRatioX(imageRect.width());
float frameH = getRatioY(imageRect.height());
float imgRatio = imageRect.width() / imageRect.height();
float frameRatio = frameW / frameH;
float l = imageRect.left, t = imageRect.top, r = imageRect.right, b = imageRect.bottom;
if (frameRatio >= imgRatio) {
l = imageRect.left;
r = imageRect.right;
float hy = (imageRect.top + imageRect.bottom) * 0.5f;
float hh = (imageRect.width() / frameRatio) * 0.5f;
t = hy - hh;
b = hy + hh;
} else if (frameRatio < imgRatio) {
t = imageRect.top;
b = imageRect.bottom;
float hx = (imageRect.left + imageRect.right) * 0.5f;
float hw = imageRect.height() * frameRatio * 0.5f;
l = hx - hw;
r = hx + hw;
}
float w = r - l;
float h = b - t;
float cx = l + w / 2;
float cy = t + h / 2;
float sw = w * mInitialFrameScale;
float sh = h * mInitialFrameScale;
return new RectF(cx - sw / 2, cy - sh / 2, cx + sw / 2, cy + sh / 2);
}
public ScaleHelper(SparkAdapter adapter, RectF contentRect, float lineWidth, boolean fill) {
final float leftPadding = contentRect.left;
final float topPadding = contentRect.top;
// subtract lineWidth to offset for 1/2 of the line bleeding out of the content box on
// either side of the view
final float lineWidthOffset = fill ? 0 : lineWidth;
this.width = contentRect.width() - lineWidthOffset;
this.height = contentRect.height() - lineWidthOffset;
this.size = adapter.getCount();
// get data bounds from adapter
RectF bounds = adapter.getDataBounds();
// if data is a line (which technically has no size), expand bounds to center the data
bounds.inset(bounds.width() == 0 ? -1 : 0, bounds.height() == 0 ? -1 : 0);
final float minX = bounds.left;
final float maxX = bounds.right;
final float minY = bounds.top;
final float maxY = bounds.bottom;
// xScale will compress or expand the min and max x values to be just inside the view
this.xScale = width / (maxX - minX);
// xTranslation will move the x points back between 0 - width
this.xTranslation = leftPadding - (minX * xScale) + (lineWidthOffset / 2);
// yScale will compress or expand the min and max y values to be just inside the view
this.yScale = height / (maxY - minY);
// yTranslation will move the y points back between 0 - height
this.yTranslation = minY * yScale + topPadding + (lineWidthOffset / 2);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (this.photoViewAttacher == null)
return false;
ImageView imageView = photoViewAttacher.getImageView();
if (null != photoViewAttacher.getOnPhotoTapListener()) {
final RectF displayRect = photoViewAttacher.getDisplayRect();
if (null != displayRect) {
final float x = e.getX(), y = e.getY();
// Check to see if the user tapped on the photo
if (displayRect.contains(x, y)) {
float xResult = (x - displayRect.left)
/ displayRect.width();
float yResult = (y - displayRect.top)
/ displayRect.height();
photoViewAttacher.getOnPhotoTapListener().onPhotoTap(imageView, xResult, yResult);
return true;
}
}
}
if (null != photoViewAttacher.getOnViewTapListener()) {
photoViewAttacher.getOnViewTapListener().onViewTap(imageView, e.getX(), e.getY());
}
return false;
}
public List layOutComponents(List<OBPhoneme> comps,OBLabel mLabel,OBFont font)
{
List<Float> rights = new ArrayList<>();
String text = mLabel.text();
int cumlength = 0;
for(int i = 0;i < comps.size();i++)
{
cumlength += comps.get(i).text.length();
String subtx = text.substring(0,cumlength);
RectF r = boundingBoxForText(subtx,font);
float f = r.width();
rights.add(f);
}
List labs = new ArrayList<>();
int idx = 0,i = 0;
List syllableLefts = new ArrayList<>();
for(OBPhoneme phoneme : comps)
{
OBLabel l = new OBLabel(phoneme.text,font);
l.setColour(Color.BLACK);
l.setPosition(mLabel.position());
if (i == 0)
syllableLefts.add(0f);
else
syllableLefts.add(rights.get(i-1));
l.setRight(mLabel.left() + rights.get(i));
l.setProperty("origpos",new PointF(l.position().x,l.position().y));
labs.add(l);
attachControl(l);
i++;
}
leftOffsets = syllableLefts;
return labs;
}
public void fling(int viewWidth, int viewHeight, int velocityX,
int velocityY) {
final RectF rect = getDisplayRect();
if (rect == null) {
return;
}
final int startX = Math.round(-rect.left);
final int minX, maxX, minY, maxY;
if (viewWidth < rect.width()) {
minX = 0;
maxX = Math.round(rect.width() - viewWidth);
} else {
minX = maxX = startX;
}
final int startY = Math.round(-rect.top);
if (viewHeight < rect.height()) {
minY = 0;
maxY = Math.round(rect.height() - viewHeight);
} else {
minY = maxY = startY;
}
mCurrentX = startX;
mCurrentY = startY;
// If we actually can move, fling the scroller
if (startX != maxX || startY != maxY) {
mScroller.fling(startX, startY, velocityX, velocityY, minX,
maxX, minY, maxY, 0, 0);
}
}
private RectF toViewCoordinate(RectF rect)
{
if (rect == null) {
return new RectF();
}
float left = rect.left - mViewBitmapRect.left;
float right = left + rect.width();
float top = rect.top - mViewBitmapRect.top;
float bottom = top + rect.height();
return new RectF(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable d = getDrawable();
if (!mPaused && d != null) {
if (mDrawableRect.isEmpty()) {
updateDrawableBounds();
} else if (hasBounds()) {
if (mCurrentTrans == null) { // Starting the first transition.
startNewTransition();
}
if (mCurrentTrans.getDestinyRect() != null) { // If null, it's supposed to stop.
mElapsedTime += System.currentTimeMillis() - mLastFrameTime;
RectF currentRect = mCurrentTrans.getInterpolatedRect(mElapsedTime);
float widthScale = mDrawableRect.width() / currentRect.width();
float heightScale = mDrawableRect.height() / currentRect.height();
// Scale to make the current rect match the smallest drawable dimension.
float currRectToDrwScale = Math.min(widthScale, heightScale);
// Scale to make the current rect match the viewport bounds.
float currRectToVpScale = mViewportRect.width() / currentRect.width();
// Combines the two scales to fill the viewport with the current rect.
float totalScale = currRectToDrwScale * currRectToVpScale;
float translX = totalScale * (mDrawableRect.centerX() - currentRect.left);
float translY = totalScale * (mDrawableRect.centerY() - currentRect.top);
/* Performs matrix transformations to fit the content
of the current rect into the entire view. */
mMatrix.reset();
mMatrix.postTranslate(-mDrawableRect.width() / 2, -mDrawableRect.height() / 2);
mMatrix.postScale(totalScale, totalScale);
mMatrix.postTranslate(translX, translY);
setImageMatrix(mMatrix);
// Current transition is over. It's time to start a new one.
if (mElapsedTime >= mCurrentTrans.getDuration()) {
fireTransitionEnd(mCurrentTrans);
startNewTransition();
}
} else { // Stopping? A stop event has to be fired.
fireTransitionEnd(mCurrentTrans);
}
}
mLastFrameTime = System.currentTimeMillis();
postInvalidateDelayed(FRAME_DELAY);
}
super.onDraw(canvas);
}
public void fling(int viewWidth, int viewHeight, int velocityX,
int velocityY) {
final RectF rect = getDisplayRect();
if (null == rect) {
return;
}
final int startX = Math.round(-rect.left);
final int minX, maxX, minY, maxY;
if (viewWidth < rect.width()) {
minX = 0;
maxX = Math.round(rect.width() - viewWidth);
} else {
minX = maxX = startX;
}
final int startY = Math.round(-rect.top);
if (viewHeight < rect.height()) {
minY = 0;
maxY = Math.round(rect.height() - viewHeight);
} else {
minY = maxY = startY;
}
mCurrentX = startX;
mCurrentY = startY;
if (DEBUG) {
LogManager.getLogger().d(
LOG_TAG,
"fling. StartX:" + startX + " StartY:" + startY
+ " MaxX:" + maxX + " MaxY:" + maxY);
}
// If we actually can move, fling the scroller
if (startX != maxX || startY != maxY) {
mScroller.fling(startX, startY, velocityX, velocityY, minX,
maxX, minY, maxY, 0, 0);
}
}
public void setupPaint(Paint paint, RectF box, float scale, float opacity) {
float height = box.height();
float width = box.width();
float midX = box.centerX();
float midY = box.centerY();
float offsetX = (midX - width / 2);
float offsetY = (midY - height / 2);
int[] stopsColors = mStopColors;
float[] stops = mStops;
//parseGradientStops(mColors, stopsCount, stops, stopsColors, opacity);
if (mType == GradientType.LINEAR_GRADIENT) {
float x1 = ParserHelper.fromPercentageToFloat(mPoints.get(0), width, offsetX, scale);
float y1 = ParserHelper.fromPercentageToFloat(mPoints.get(1), height, offsetY, scale);
float x2 = ParserHelper.fromPercentageToFloat(mPoints.get(2), width, offsetX, scale);
float y2 = ParserHelper.fromPercentageToFloat(mPoints.get(3), height, offsetY, scale);
paint.setShader(
new LinearGradient(
x1,
y1,
x2,
y2,
stopsColors,
stops,
Shader.TileMode.CLAMP));
} else {
float rx = ParserHelper.fromPercentageToFloat(mPoints.get(2), width, 0f, scale);
float ry = ParserHelper.fromPercentageToFloat(mPoints.get(3), height, 0f, scale);
float cx = ParserHelper.fromPercentageToFloat(mPoints.get(4), width, offsetX, scale);
float cy = ParserHelper.fromPercentageToFloat(mPoints.get(5), height, offsetY, scale) / (ry / rx);
// TODO: support focus point.
float fx = ParserHelper.fromPercentageToFloat(mPoints.get(0), width, offsetX, scale);
float fy = ParserHelper.fromPercentageToFloat(mPoints.get(1), height, offsetY, scale) / (ry / rx);
Shader radialGradient = new RadialGradient(
cx,
cy,
rx,
stopsColors,
stops,
Shader.TileMode.CLAMP
);
Matrix radialMatrix = new Matrix();
radialMatrix.preScale(1f, ry / rx);
radialGradient.setLocalMatrix(radialMatrix);
paint.setShader(radialGradient);
}
}
public void center(boolean horizontal, boolean vertical) {
if (mBitmapDisplayed == null) {
return;
}
Matrix m = getImageViewMatrix();
RectF rect = new RectF(0, 0,
mBitmapDisplayed.getWidth(),
mBitmapDisplayed.getHeight());
m.mapRect(rect);
float height = rect.height();
float width = rect.width();
float deltaX = 0, deltaY = 0;
if (vertical) {
int viewHeight = getHeight();
if (height < viewHeight) {
deltaY = (viewHeight - height) / 2 - rect.top;
} else if (rect.top > 0) {
deltaY = -rect.top;
} else if (rect.bottom < viewHeight) {
deltaY = getHeight() - rect.bottom;
}
}
if (horizontal) {
int viewWidth = getWidth();
if (width < viewWidth) {
deltaX = (viewWidth - width) / 2 - rect.left;
} else if (rect.left > 0) {
deltaX = -rect.left;
} else if (rect.right < viewWidth) {
deltaX = viewWidth - rect.right;
}
}
postTranslate(deltaX, deltaY);
setImageMatrix(getImageViewMatrix());
}
public final boolean onSingleTapConfirmed(MotionEvent e) {
// Activity activity = (Activity) mContext;
// if(mFromChatActivity){
// activity.finish();
// }else {
// RelativeLayout titleRl = (RelativeLayout) activity.findViewById(R.id.title_bar_rl);
// RelativeLayout checkBoxRl = (RelativeLayout) activity.findViewById(R.id.check_box_rl);
// WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
// //如果标题栏,菜单栏可见,单击后隐藏并设置全屏模式
// if(mTitleBarVisible){
// attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
// activity.getWindow().setAttributes(attrs);
// activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// titleRl.setVisibility(View.GONE);
// checkBoxRl.setVisibility(View.GONE);
// mTitleBarVisible = false;
// //否则显示标题栏、菜单栏,并取消全屏
// }else {
// attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
// activity.getWindow().setAttributes(attrs);
// activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// titleRl.setVisibility(View.VISIBLE);
// checkBoxRl.setVisibility(View.VISIBLE);
// mTitleBarVisible = true;
// }
// }
ImageView imageView = getImageView();
if (null != imageView) {
if (null != mPhotoTapListener) {
final RectF displayRect = getDisplayRect();
if (null != displayRect) {
final float x = e.getX(), y = e.getY();
// Check to see if the user tapped on the photo
if (displayRect.contains(x, y)) {
float xResult = (x - displayRect.left) / displayRect.width();
float yResult = (y - displayRect.top) / displayRect.height();
mPhotoTapListener.onPhotoTap(imageView, xResult, yResult);
return true;
}
}
}
if (null != mViewTapListener) {
mViewTapListener.onViewTap(imageView, e.getX(), e.getY());
}
}
return false;
}
public void demob()throws Exception
{
setStatus(STATUS_DOING_DEMO);
RectF botrect = objectDict.get("bottomrect") .frame();
PointF destpt = new PointF(rightMostLabelX() + botrect.width() / 2,botrect.centerY());
PointF startpt = pointForDestPoint(destpt,45);
loadPointerStartPoint(startpt,destpt);
movePointerToPoint(destpt,-1,true);
waitForSecs(0.3f);
playAudioQueuedScene("DEMO",true);
waitForSecs(0.4f);
for(OBLabel sl : staticLabels)
{
String tx = sl.text();
OBLabel targetlab = candidateLabelWithText(tx);
movePointerToPoint(OB_Maths.locationForRect(0.5f, 0.7f, targetlab.frame()),-1,true);
waitForSecs(0.2f);
moveObjects(Arrays.asList(targetlab,thePointer),sl.position(),-0.7f,OBAnim.ANIM_EASE_IN_EASE_OUT);
targetlab.setProperty("staticLabelIndex",letterIdx);
playSfxAudio("letterin",false);
dashes.get(letterIdx).hide();
movePointerForwards(applyGraphicScale(-100),-1);
waitSFX();
waitForSecs(0.2f);
if(letterAudio != OCSP_LETTER_AUDIO_NONE)
{
int col = targetlab.colour();
targetlab.setColour(Color.RED);
playLetterAudio(sl.text() );
waitForSecs(0.2f);
waitAudio();
targetlab.setColour(col);
waitForSecs(0.2f);
}
highlightDash(++letterIdx);
waitForSecs(0.4f);
}
blendWord();
waitForSecs(0.3f);
speakWord(currWordID);
waitForSecs(0.3f);
thePointer.hide();
waitForSecs(0.3f);
flyWordToBottom();
showDashes();
waitForSecs(0.3f);
letterIdx = 0;
highlightMarker();
waitForSecs(0.4f);
playAudioQueuedScene("DEMO2",true);
waitForSecs(0.2f);
if(RADemo)
{
buttonDemo();
}
nextScene();
}
@Override
protected void onDraw(Canvas canvas) {
Drawable d = getDrawable();
if (!mPaused && d != null) {
if (mDrawableRect.isEmpty()) {
updateDrawableBounds();
} else if (!mViewportRect.isEmpty()) {
if (mCurrentTrans == null) { // Starting the first transition.
startNewTransition();
}
if (mCurrentTrans.getDestinyRect() != null) { // If null, it's supposed to stop.
mElapsedTime += System.currentTimeMillis() - mLastFrameTime;
RectF currentRect = mCurrentTrans.getInterpolatedRect(mElapsedTime);
float widthScale = mDrawableRect.width() / currentRect.width();
float heightScale = mDrawableRect.height() / currentRect.height();
// Scale to make the current rect match the smallest drawable dimension.
float currRectToDrwScale = Math.min(widthScale, heightScale);
// Scale to make the current rect match the viewport bounds.
float currRectToVpScale = mViewportRect.width() / currentRect.width();
// Combines the two scales to fill the viewport with the current rect.
float totalScale = currRectToDrwScale * currRectToVpScale;
float translX = totalScale * (mDrawableRect.centerX() - currentRect.left);
float translY = totalScale * (mDrawableRect.centerY() - currentRect.top);
/* Performs matrix transformations to fit the content
of the current rect into the entire view. */
mMatrix.reset();
mMatrix.postTranslate(-mDrawableRect.width() / 2, -mDrawableRect.height() / 2);
mMatrix.postScale(totalScale, totalScale);
mMatrix.postTranslate(translX, translY);
setImageMatrix(mMatrix);
// Current transition is over. It's time to start a new one.
if (mElapsedTime >= mCurrentTrans.getDuration()) {
fireTransitionEnd(mCurrentTrans);
startNewTransition();
}
} else { // Stopping? A stop event has to be fired.
fireTransitionEnd(mCurrentTrans);
}
}
mLastFrameTime = System.currentTimeMillis();
postInvalidateDelayed(FRAME_DELAY);
}
super.onDraw(canvas);
}