下面列出了android.graphics.Rect#width ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* A factory method to build the appropriate LuminanceSource object based on the format
* of the preview buffers, as described by Camera.Parameters.
*
* @param data A preview frame.
* @param width The width of the image.
* @param height The height of the image.
* @return A PlanarYUVLuminanceSource instance.
*/
public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
Rect rect = getFramingRectInPreview();
int previewFormat = configManager.getPreviewFormat();
String previewFormatString = configManager.getPreviewFormatString();
switch (previewFormat) {
// This is the standard Android format which all devices are REQUIRED to support.
// In theory, it's the only one we should ever care about.
case PixelFormat.YCbCr_420_SP:
// This format has never been seen in the wild, but is compatible as we only care
// about the Y channel, so allow it.
case PixelFormat.YCbCr_422_SP:
return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
rect.width(), rect.height());
default:
// The Samsung Moment incorrectly uses this variant instead of the 'sp' version.
// Fortunately, it too has all the Y data up front, so we can read it.
if ("yuv420p".equals(previewFormatString)) {
return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top,
rect.width(), rect.height());
}
}
throw new IllegalArgumentException("Unsupported picture format: " +
previewFormat + '/' + previewFormatString);
}
@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
// draw placeholder circle
float radius = Math.min(bounds.width(), bounds.height()) / 2f;
float xPos = bounds.left + (bounds.width() / 2f);
canvas.drawCircle(xPos, bounds.top + bounds.height() / 2f, radius, mPlaceholderCirclePaint);
if (mPlaceholderImage == null) {
// draw placeholder text
float yPos = (bounds.top + (bounds.height() / 2f) -
((mPlaceholderTextPaint.descent() + mPlaceholderTextPaint.ascent()) / 2f));
canvas.drawText(mPlaceholderText, xPos, yPos, mPlaceholderTextPaint);
} else {
// draw placeholder image
int horizontalPadding = (bounds.width() - mPlaceholderImageSize) / 2;
int verticalPadding = (bounds.height() - mPlaceholderImageSize) / 2;
mPlaceholderImage.setBounds(bounds.left + horizontalPadding, bounds.top + verticalPadding,
bounds.right - horizontalPadding, bounds.bottom - verticalPadding);
mPlaceholderImage.draw(canvas);
}
}
void handleMotion(int edge, float dx, float dy) {
Rect r = computeLayout();
if (edge == MOVE) {
// Convert to image space before sending to moveBy()
moveBy(dx * (cropRect.width() / r.width()),
dy * (cropRect.height() / r.height()));
} else {
if (((GROW_LEFT_EDGE | GROW_RIGHT_EDGE) & edge) == 0) {
dx = 0;
}
if (((GROW_TOP_EDGE | GROW_BOTTOM_EDGE) & edge) == 0) {
dy = 0;
}
// Convert to image space before sending to growBy()
float xDelta = dx * (cropRect.width() / r.width());
float yDelta = dy * (cropRect.height() / r.height());
growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta,
(((edge & GROW_TOP_EDGE) != 0) ? -1 : 1) * yDelta);
}
}
@Override
public void getTransformImpl(
Matrix outTransform,
Rect parentRect,
int childWidth,
int childHeight,
float focusX,
float focusY,
float scaleX,
float scaleY) {
float scale = Math.min(Math.min(scaleX, scaleY), 1.0f);
float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
outTransform.setScale(scale, scale);
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
rect = new Rect( innerPadding, innerPadding, w - innerPadding, h - innerPadding );
bitmap = Bitmap.createBitmap( w - 2 * innerPadding, h - 2 * innerPadding, Config.ARGB_8888 );
fullCircleRadius = Math.min( rect.width(), rect.height() ) / 2;
innerCircleRadius = fullCircleRadius * ( 1 - FADE_OUT_FRACTION );
scaledWidth = rect.width() / scale;
scaledHeight = rect.height() / scale;
scaledFullCircleRadius = Math.min( scaledWidth, scaledHeight ) / 2;
scaledInnerCircleRadius = scaledFullCircleRadius * ( 1 - FADE_OUT_FRACTION );
scaledFadeOutSize = scaledFullCircleRadius - scaledInnerCircleRadius;
scaledPixels = new int[ scaledWidth * scaledHeight ];
pixels = new int[ rect.width() * rect.height() ];
createBitmap();
}
@Override
public void getTransformImpl(
Matrix outTransform,
Rect parentRect,
int childWidth,
int childHeight,
float focusX,
float focusY,
float scaleX,
float scaleY) {
float scale = Math.min(scaleX, scaleY);
float dx = parentRect.left + (parentRect.width() - childWidth * scale) * 0.5f;
float dy = parentRect.top + (parentRect.height() - childHeight * scale) * 0.5f;
outTransform.setScale(scale, scale);
outTransform.postTranslate((int) (dx + 0.5f), (int) (dy + 0.5f));
}
/**
* dataList will be reset when called is method.
* @param bottomStringList The String ArrayList in the bottom.
*/
public void setBottomTextList(ArrayList<String> bottomStringList){
// this.dataList = null;
this.bottomTextList = bottomStringList;
Rect r = new Rect();
bottomTextDescent = 0;
barWidth = MINI_BAR_WIDTH;
for(String s:bottomTextList){
textPaint.getTextBounds(s,0,s.length(),r);
if(bottomTextHeight<r.height()){
bottomTextHeight = r.height();
}
if(autoSetWidth&&(barWidth<r.width())){
barWidth = r.width();
}
if(bottomTextDescent<(Math.abs(r.bottom))){
bottomTextDescent = Math.abs(r.bottom);
}
}
setMinimumWidth(2);
postInvalidate();
}
private boolean startDragging(MotionEvent ev, boolean ignoreTrackIfInScrollContainer) {
final Rect bounds = mTempRect;
mThumb.copyBounds(bounds);
// Grow the current thumb rect for a bigger touch area
bounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
mIsDragging = (bounds.contains((int) ev.getX(), (int) ev.getY()));
if (!mIsDragging && mAllowTrackClick && !ignoreTrackIfInScrollContainer) {
// If the user clicked outside the thumb, we compute the current position
// and force an immediate drag to it.
mIsDragging = true;
mDragOffset = (bounds.width() / 2) - mAddedTouchBounds;
updateDragging(ev);
// As the thumb may have moved, get the bounds again
mThumb.copyBounds(bounds);
bounds.inset(-mAddedTouchBounds, -mAddedTouchBounds);
}
if (mIsDragging) {
setPressed(true);
attemptClaimDrag();
setHotspot(ev.getX(), ev.getY());
mDragOffset = (int) (ev.getX() - bounds.left - mAddedTouchBounds);
if (mPublicChangeListener != null) {
mPublicChangeListener.onStartTrackingTouch(this);
}
}
return mIsDragging;
}
private void updateMatrix(){
if(mBitmap == null)
return;
Rect bounds = getBounds();
if(bounds.width() == 0 || bounds.height() == 0)
return;
mMatrix.reset();
float scale = bounds.height() / (float)Math.min(mBitmap.getWidth(), mBitmap.getHeight());
mMatrix.setScale(scale, scale, 0, 0);
mMatrix.postTranslate((bounds.height() - mBitmap.getWidth() * scale) / 2, (bounds.height() - mBitmap.getHeight() * scale) / 2);
mBitmapShader.setLocalMatrix(mMatrix);
}
/**
* calc rotated point
* @param dot
* @param radAngle
*/
private PointF getRotatePoint(PointF dot, float radAngle, Paint textPaint, String rateText) {
PointF rotateDot = new PointF();
Rect rect = new Rect();
textPaint.getTextBounds(rateText, 0, rateText.length(), rect);
rotateDot.x = (float) (chartCenter.x + radius/1.4 *Math.cos(radAngle) - rect.width()/2);
rotateDot.y = (float) (chartCenter.y + radius/1.4 *Math.sin(radAngle) + rect.height()/2);
return rotateDot;
}
private void buildComponents(Rect bounds) {
// Card is offset mShadowMultiplier * maxShadowSize to account for the shadow shift.
// We could have different top-bottom offsets to avoid extra gap above but in that case
// center aligning Views inside the CardView would be problematic.
if (isCircle) {
mCornerRadius = bounds.width() / 2;
}
final float verticalOffset = mRawMaxShadowSize * mShadowMultiplier;
mContentBounds.set(bounds.left + mRawMaxShadowSize, bounds.top + verticalOffset,
bounds.right - mRawMaxShadowSize, bounds.bottom - verticalOffset);
getWrappedDrawable().setBounds((int) mContentBounds.left, (int) mContentBounds.top,
(int) mContentBounds.right, (int) mContentBounds.bottom);
buildShadowCorners();
}
/**
* Fix the given rectangle if it doesn't confirm to aspect ration rule.<br>
* Make sure that width and height are equal if 1:1 fixed aspect ratio is requested.
*/
private static void fixRectForAspectRatio(Rect rect, int aspectRatioX, int aspectRatioY) {
if (aspectRatioX == aspectRatioY && rect.width() != rect.height()) {
if (rect.height() > rect.width()) {
rect.bottom -= rect.height() - rect.width();
} else {
rect.right -= rect.width() - rect.height();
}
}
}
/**
* Compute 3A regions for a sensor-referenced touch coordinate.
* Returns a MeteringRectangle[] with length 1.
*
* @param nx x coordinate of the touch point, in normalized portrait coordinates.
* @param ny y coordinate of the touch point, in normalized portrait coordinates.
* @param fraction Fraction in [0,1]. Multiplied by min(cropRegion.width(), cropRegion.height())
* to determine the side length of the square MeteringRectangle.
* @param cropRegion Crop region of the image.
* @param sensorOrientation sensor orientation as defined by
* CameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION).
*/
private static MeteringRectangle[] regionsForNormalizedCoord(float nx, float ny,
float fraction, final Rect cropRegion, int
sensorOrientation)
{
// Compute half side length in pixels.
int minCropEdge = Math.min(cropRegion.width(), cropRegion.height());
int halfSideLength = (int) (0.5f * fraction * minCropEdge);
// Compute the output MeteringRectangle in sensor space.
// nx, ny is normalized to the screen.
// Crop region itself is specified in sensor coordinates.
// Normalized coordinates, now rotated into sensor space.
PointF nsc = CameraUtil.normalizedSensorCoordsForNormalizedDisplayCoords(
nx, ny, sensorOrientation);
int xCenterSensor = (int) (cropRegion.left + nsc.x * cropRegion.width());
int yCenterSensor = (int) (cropRegion.top + nsc.y * cropRegion.height());
Rect meteringRegion = new Rect(xCenterSensor - halfSideLength,
yCenterSensor - halfSideLength,
xCenterSensor + halfSideLength,
yCenterSensor + halfSideLength);
// Clamp meteringRegion to cropRegion.
meteringRegion.left = CameraUtil.clamp(meteringRegion.left, cropRegion.left, cropRegion.right);
meteringRegion.top = CameraUtil.clamp(meteringRegion.top, cropRegion.top, cropRegion.bottom);
meteringRegion.right = CameraUtil.clamp(meteringRegion.right, cropRegion.left, cropRegion.right);
meteringRegion.bottom = CameraUtil.clamp(meteringRegion.bottom, cropRegion.top, cropRegion.bottom);
return new MeteringRectangle[]{new MeteringRectangle(meteringRegion, CAMERA2_REGION_WEIGHT)};
}
/**
* A factory method to build the appropriate LuminanceSource object based on
* the format of the preview buffers, as described by Camera.Parameters.
*
* @param data A preview frame.
* @param width The width of the image.
* @param height The height of the image.
* @return A PlanarYUVLuminanceSource instance.
*/
public PlanarYUVLuminanceSource buildLuminanceSource(byte[] data, int width, int height) {
Rect rect = activity.getCropRect();
if (rect == null) {
return null;
}
// Go ahead and assume it's YUV rather than die.
return new PlanarYUVLuminanceSource(data, width, height, rect.left, rect.top, rect.width(), rect
.height(), false);
}
@Override protected void animatePrepare(CharSequence text) {
Rect bounds = new Rect();
mPaint.getTextBounds(mText.toString(), 0, mText.length(), bounds);
mTextHeight = bounds.height();
distWidth = bounds.width() + padding * 2 + xLineWidth;
distHeight = bounds.height() + padding * 2 + xLineWidth;
xLineLength = mHTextView.getWidth();
yLineLength = mHTextView.getHeight();
}
private void onSaveClicked() {
if (cropView == null || isSaving) {
return;
}
isSaving = true;
Bitmap croppedImage;
Rect r = cropView.getScaledCropRect(sampleSize);
int width = r.width();
int height = r.height();
int outWidth = width;
int outHeight = height;
if (maxX > 0 && maxY > 0 && (width > maxX || height > maxY)) {
float ratio = (float) width / (float) height;
if ((float) maxX / (float) maxY > ratio) {
outHeight = maxY;
outWidth = (int) ((float) maxY * ratio + .5f);
} else {
outWidth = maxX;
outHeight = (int) ((float) maxX / ratio + .5f);
}
}
try {
croppedImage = decodeRegionCrop(r, outWidth, outHeight);
} catch (IllegalArgumentException e) {
setResultException(e);
finish();
return;
}
if (croppedImage != null) {
imageView.setImageRotateBitmapResetBase(new RotateBitmap(croppedImage, exifRotation), true);
imageView.center();
imageView.highlightViews.clear();
}
saveImage(croppedImage);
}
private void ensurePosition(Rect bounds) {
float boundSize = Math.min(bounds.width(), bounds.height());
radius = (float) ((Math.sin(Math.PI / 4) * boundSize / 2 + boundSize / 2) / 2 - 2);
cx = (float) (1.0 * bounds.width() / 2 + bounds.left);
cy = (float) (1.0 * bounds.height() / 2 + bounds.top);
}
@Override
protected void onDraw(Canvas canvas) {
// to draw possible background, etc
super.onDraw(canvas);
final Drawable drawable = mDrawable;
if (drawable == null) {
return;
}
final int scrollX = mScrollX;
final int scrollY = mScrollY;
final int width = canvas.getWidth();
final int height = canvas.getHeight();
final Rect rect = drawable.getBounds();
final int drawableWidth = rect.width();
final int drawableHeight = rect.height();
final int startX = start(scrollX, drawableWidth);
final int iterationsX = iterations(width, startX, drawableWidth);
final int startY = start(scrollY, drawableHeight);
final int iterationsY = iterations(height, startY, drawableHeight);
final int save = canvas.save();
try {
canvas.translate(startX, startY);
for (int x = 0; x < iterationsX; x++) {
for (int y = 0; y < iterationsY; y++) {
drawable.draw(canvas);
canvas.translate(.0F, drawableHeight);
}
canvas.translate(drawableWidth, -(drawableHeight * iterationsY));
}
} finally {
canvas.restoreToCount(save);
}
}
public static Size of(Rect rectangle)
{
return new Size(rectangle.width(), rectangle.height());
}
@Override
public void draw(Canvas canvas) {
final Rect bounds = getBounds();
if (bounds.width() == 0 || bounds.height() == 0) {
// too small to draw
return;
}
final int saveCount = canvas.save();
final boolean needMirroring = needMirroring();
canvas.translate(bounds.left, bounds.top);
if (needMirroring) {
canvas.translate(bounds.width(), 0);
canvas.scale(-1.0f, 1.0f);
}
// Color filters always override tint filters.
final ColorFilter colorFilter = mColorFilter == null ? mTintFilter : mColorFilter;
if (!mAllowCaching) {
// AnimatedVectorDrawable
if (!mVectorState.hasTranslucentRoot()) {
mVectorState.mVPathRenderer.draw(
canvas, bounds.width(), bounds.height(), colorFilter);
} else {
mVectorState.createCachedBitmapIfNeeded(bounds);
mVectorState.updateCachedBitmap(bounds);
mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
}
} else {
// Static Vector Drawable case.
mVectorState.createCachedBitmapIfNeeded(bounds);
if (!mVectorState.canReuseCache()) {
mVectorState.updateCachedBitmap(bounds);
mVectorState.updateCacheStates();
}
mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
}
canvas.restoreToCount(saveCount);
}