Android 在自定义视图中使用比例类型中心绘制位图
我有用于显示两个位图的customview,但问题是它正在缩放到中心裁剪,但我不想裁剪位图,而是希望在画布中显示完整的位图Android 在自定义视图中使用比例类型中心绘制位图,android,canvas,bitmap,custom-view,Android,Canvas,Bitmap,Custom View,我有用于显示两个位图的customview,但问题是它正在缩放到中心裁剪,但我不想裁剪位图,而是希望在画布中显示完整的位图 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mLayoutWidth = MeasureSpec.getSize(widthMeasureSpec); mLayoutHeight = MeasureSpec.getSize(heightMeas
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
mLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
mLayoutHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(mLayoutWidth, mLayoutHeight);
mBitmap = scaleCenterCrop(mBitmap, mLayoutWidth, mLayoutHeight);
overlayDefault = FastBlur.doBlur(mBitmap, 20, false);
overlayDefault = scaleCenterCrop(overlayDefault, mLayoutWidth,
mLayoutHeight);
overlay = mBitmap.copy(Config.ARGB_8888, true);
overlay = scaleCenterCrop(overlay, mLayoutWidth, mLayoutHeight);
cx = (mLayoutWidth - mBitmap.getWidth()) >> 1;
cy = (mLayoutHeight - mBitmap.getHeight()) >> 1;
c2 = new Canvas(overlay);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final Rect bitmapRect = ImageViewUtil.getBitmapRectCenterInside(
mBitmap.getWidth(), mBitmap.getHeight(), mLayoutWidth, mLayoutHeight);
setBitmapRect(bitmapRect);
invalidate();
}
public Bitmap scaleCenterCrop(Bitmap original, int deviceHeight,
int deviceWidth) {
int old_width = original.getWidth();
int old_height = original.getHeight();
float scale = Math.max((float) deviceHeight / old_height,
(float) deviceWidth / old_width);
float newWidth = scale * old_width;
float newHeight = scale * old_height;
float left = (deviceWidth - newWidth) / 2;
float top = (deviceHeight - newHeight) / 2;
RectF rectF = new RectF(left, top, left + newWidth, top + newHeight);
Bitmap scaled = Bitmap.createBitmap(deviceWidth, deviceHeight,
original.getConfig());
Canvas canvas = new Canvas(scaled);
canvas.drawBitmap(original, null, rectF, null);
return scaled;
}
您不需要创建缩放的位图,只需缩放画布(Canvas.scale()/Canvas.translate())或使用Canvas.drawBitmap()和矩阵参数。我尝试过使用Canvas scale选项,但它无法根据我的需要工作。您可以举一个示例来说明设置矩阵参数矩阵。setRectToRect()@用户3572506你找到解决方案了吗?我有同样的要求