本文整理了Java中android.graphics.Rect
类的一些代码示例,展示了Rect
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Rect
类的具体详情如下:
包路径:android.graphics.Rect
类名称:Rect
[英]Rect holds four integer coordinates for a rectangle. The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom). These fields can be accessed directly. Use width() and height() to retrieve the rectangle's width and height. Note: most methods do not check to see that the coordinates are sorted correctly (i.e. left Note that the right and bottom coordinates are exclusive. This means a Rect being drawn untransformed onto a android.graphics.Canvas will draw into the column and row described by its left and top coordinates, but not those of its bottom and right.
[中]Rect保存矩形的四个整数坐标。矩形由其4条边(左、上、右下)的坐标表示。这些字段可以直接访问。使用width()和height()检索矩形的宽度和高度。注意:大多数方法都不会检查坐标是否正确排序(即,左侧注意,右侧和底部坐标是互斥的。这意味着未经转换绘制到android.graphics.Canvas上的Rect将绘制到由其左侧和顶部坐标描述的列和行中,但不会绘制其底部和右侧坐标。
代码示例来源:origin: androidquery/androidquery
private static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
代码示例来源:origin: stackoverflow.com
protected void onDraw(Canvas canvas){
final String s = "Hello. I'm some text!";
Paint p = new Paint();
Rect bounds = new Rect();
p.setTextSize(60);
p.getTextBounds(s, 0, s.length(), bounds);
float mt = p.measureText(s);
int bw = bounds.width();
Log.i("LCG", String.format(
"measureText %f, getTextBounds %d (%s)",
mt,
bw, bounds.toShortString())
);
bounds.offset(0, -bounds.top);
p.setStyle(Style.STROKE);
canvas.drawColor(0xff000080);
p.setColor(0xffff0000);
canvas.drawRect(bounds, p);
p.setColor(0xff00ff00);
canvas.drawText(s, 0, bounds.bottom, p);
}
代码示例来源:origin: naman14/Timber
private BitmapDrawable createFloatingBitmap(View v) {
floatingItemStatingBounds = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
floatingItemBounds = new Rect(floatingItemStatingBounds);
Bitmap bitmap = Bitmap.createBitmap(floatingItemStatingBounds.width(),
floatingItemStatingBounds.height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
BitmapDrawable retDrawable = new BitmapDrawable(v.getResources(), bitmap);
retDrawable.setBounds(floatingItemBounds);
return retDrawable;
}
代码示例来源:origin: stackoverflow.com
Rect bounds = new Rect();
Paint textPaint = textView.getPaint();
textPaint.getTextBounds(text, 0, text.length(), bounds);
int height = bounds.height();
int width = bounds.width();
代码示例来源:origin: PhilJay/MPAndroidChart
/**
* calculates the approximate size of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*
* @param paint
* @param demoText
* @param outputFSize An output variable, modified by the function.
*/
public static void calcTextSize(Paint paint, String demoText, FSize outputFSize) {
Rect r = mCalcTextSizeRect;
r.set(0,0,0,0);
paint.getTextBounds(demoText, 0, demoText.length(), r);
outputFSize.width = r.width();
outputFSize.height = r.height();
}
代码示例来源:origin: PhilJay/MPAndroidChart
/**
* calculates the approximate height of a text, depending on a demo text
* avoid repeated calls (e.g. inside drawing methods)
*
* @param paint
* @param demoText
* @return
*/
public static int calcTextHeight(Paint paint, String demoText) {
Rect r = mCalcTextHeightRect;
r.set(0,0,0,0);
paint.getTextBounds(demoText, 0, demoText.length(), r);
return r.height();
}
代码示例来源:origin: navasmdc/MaterialDesignLibrary
public Bitmap cropCircle(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth()/2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
代码示例来源:origin: stackoverflow.com
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(convertToPixels(context, 11));
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
if(textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
paint.setTextSize(convertToPixels(context, 7)); //Scaling needs to be used for different dpi's
int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(text, xPos, yPos, paint);
final float conversionScale = context.getResources().getDisplayMetrics().density;
代码示例来源:origin: stackoverflow.com
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
float smallest = Math.min(bmp.getWidth(), bmp.getHeight());
float factor = smallest / radius;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
代码示例来源:origin: stackoverflow.com
Resources resources = mContext.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
bitmap = bitmap.copy(bitmapConfig, true);
paint.setColor(Color.rgb(110,110, 110));
paint.setTextSize((int) (12 * scale));
paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);
Rect bounds = new Rect();
paint.getTextBounds(mText, 0, mText.length(), bounds);
int x = (bitmap.getWidth() - bounds.width())/6;
int y = (bitmap.getHeight() + bounds.height())/5;
canvas.drawText(mText, x * scale, y * scale, paint);
代码示例来源:origin: scwang90/SmartRefreshLayout
final Drawable drawable = PathsDrawable.this;
final Rect bounds = drawable.getBounds();
final int width = bounds.width();
final int height = bounds.height();
if (mPaint.getAlpha() == 0xFF) {
canvas.save();
canvas.translate(bounds.left-mStartX, bounds.top-mStartY);
if (mPaths != null) {
for (int i = 0; i < mPaths.size(); i++) {
if (mColors != null && i < mColors.size()) {
mPaint.setColor(mColors.get(i));
canvas.drawPath(mPaths.get(i), mPaint);
mPaint.setAlpha(0xFF);
canvas.restore();
createCachedBitmapIfNeeded(width, height);
if (mCacheDirty) {
mCachedBitmap.eraseColor(Color.TRANSPARENT);
Canvas tmpCanvas = new Canvas(mCachedBitmap);
drawCachedBitmap(tmpCanvas);
代码示例来源:origin: rey5137/material
@Override
public void draw(Canvas canvas) {
int saveCount = canvas.save();
Rect bounds = getBounds();
float halfHeight = bounds.height() / 2f;
mPaint.setShader(null);
mPaint.setColor(mBackgroundColor);
mRect.set(1, 0, bounds.height() + 1, bounds.height());
canvas.drawArc(mRect, 90, 180, true, mPaint);
mRect.set(bounds.width() - bounds.height(), 0, bounds.width(), bounds.height());
canvas.drawArc(mRect, 270, 180, true, mPaint);
mRect.set(halfHeight, 0, bounds.width() - halfHeight, bounds.height());
canvas.drawRect(mRect, mPaint);
if(mBitmap != null){
mPaint.setShader(mBitmapShader);
canvas.drawCircle(halfHeight, halfHeight, halfHeight, mPaint);
}
if(mContactName != null && mBoringLayout != null) {
canvas.translate(bounds.height() + mPaddingLeft, (bounds.height() - mBoringLayout.getHeight()) / 2f);
mBoringLayout.draw(canvas);
}
canvas.restoreToCount(saveCount);
}
代码示例来源:origin: bumptech/glide
private Bitmap createSolidRedBitmap(int width, int height) {
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setColor(Color.RED);
Rect rect = new Rect(0, 0, width, height);
canvas.drawRect(rect, paint);
return result;
}
}
代码示例来源:origin: stackoverflow.com
Paint paint = new Paint();
Rect r = new Rect(10, 10, 200, 100);
@Override
public void onDraw(Canvas canvas) {
// fill
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.MAGENTA);
canvas.drawRect(r, paint);
// border
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
canvas.drawRect(r, paint);
}
代码示例来源:origin: ZieIony/Carbon
private void drawChecked(@NonNull Canvas canvas, float radius) {
Rect bounds = getBounds();
paint.setShader(null);
canvas.drawBitmap(uncheckedBitmap, bounds.left, bounds.top, paint);
maskCanvas.drawColor(0xffffffff);
maskPaint.setXfermode(porterDuffClear);
maskCanvas.drawCircle(maskBitmap.getWidth() / 2 + bounds.width() * offset.x, maskBitmap.getHeight() / 2 + bounds.height() * offset.y, radius, maskPaint);
maskPaint.setXfermode(porterDuffSrcIn);
maskCanvas.drawBitmap(filledBitmap, 0, 0, maskPaint);
canvas.drawBitmap(maskBitmap, bounds.left, bounds.top, paint);
paint.setShader(checkedShader);
canvas.drawCircle(bounds.centerX() + bounds.width() * offset.x, bounds.centerY() + bounds.height() * offset.y, radius, paint);
}
代码示例来源:origin: stackoverflow.com
private Rect rect; // Variable rect to hold the bounds of the view
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
// Construct a rect of the view's bounds
rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
if(!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())){
// User moved outside bounds
}
}
return false;
}
代码示例来源:origin: stackoverflow.com
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Rect dialogBounds = new Rect();
getWindow().getDecorView().getHitRect(dialogBounds);
if (!dialogBounds.contains((int) ev.getX(), (int) ev.getY())) {
// Tapped outside so we finish the activity
this.finish();
}
return super.dispatchTouchEvent(ev);
}
代码示例来源:origin: android-cjj/Android-MaterialRefreshLayout
private void init() {
Log.i(Tag, "init");
mSunRadius = changeDp(DEFAULT_SUN_RADIUS);
mEyesRadius = changeDp(DEFAULT_EYES_RADIUS);
//圆的配置
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mCirclePaint.setColor(Color.RED);
mCirclePaint.setStyle(Paint.Style.FILL);
debugRect = new Rect();
mouthRect = new RectF();
}
代码示例来源:origin: robolectric/robolectric
@Test
public void shouldDescribeBitmapDrawing_withDestinationRect() throws Exception {
Canvas canvas = new Canvas(targetBitmap);
canvas.drawBitmap(imageBitmap, new Rect(1,2,3,4), new Rect(5,6,7,8), new Paint());
assertEquals("Bitmap for file:/an/image.jpg at (5,6) with height=2 and width=2 taken from Rect(1, 2 - 3, 4)", shadowOf(canvas).getDescription());
}
代码示例来源:origin: airbnb/lottie-android
@Override public void drawLayer(@NonNull Canvas canvas, Matrix parentMatrix, int parentAlpha) {
Bitmap bitmap = getBitmap();
if (bitmap == null || bitmap.isRecycled()) {
return;
}
float density = Utils.dpScale();
paint.setAlpha(parentAlpha);
if (colorFilterAnimation != null) {
paint.setColorFilter(colorFilterAnimation.getValue());
}
canvas.save();
canvas.concat(parentMatrix);
src.set(0, 0, bitmap.getWidth(), bitmap.getHeight());
dst.set(0, 0, (int) (bitmap.getWidth() * density), (int) (bitmap.getHeight() * density));
canvas.drawBitmap(bitmap, src, dst , paint);
canvas.restore();
}
内容来源于网络,如有侵权,请联系作者删除!