问题描述
我有一个ImageView
和一个Bitmap
,并且ScaleType
是FitXY
,当我从Imageview
中得到Bitmap
时:
I have ImageView
with a Bitmap
, and the ScaleType
is FitXY
, when I get the Bitmap
out of the Imageview
:
Bitmap currentBitmap = ((BitmapDrawable) context.getDrawable()).getBitmap();
返回的是带有原始Width/Height
而不是Scaled Size
的原始Image
,如何获得Scaled
Bitmap
不是原始的Scaled
?
the returned is the original Image
with the original Width/Height
not the Scaled Size
, how is it possible to get the Scaled
Bitmap
not the original one?
推荐答案
好吧,缩放效果实际上是在画布"级别上应用的.因此,您可以做的是获取当前的Canvas增强,然后将您的Bitmap绘制为具有更改的新对象,但是,您将不得不重写ImageView类以显示支持Canvas的Matrix./p>
Well, the scaling effects are actually applied at the Canvas level. So, what you can do is get the current Canvas augmentations and then draw your Bitmap into a new one with the changes, BUT, you're going to have to override the ImageView class in order to expose the Matrix that backs the Canvas.
public class ExposedImageView extends ImageView {
protected Matrix matrix = new Matrix();
... // constructors
@Override
public void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.getMatrix(matrix);
}
public Matrix getCanvasMatrix(){
return matrix;
}
}
然后您可以使用以下内容获得调整后的图像:
And then you can get the adjusted image with something like:
public static Bitmap getRealImage(ExposedImageView view) throws Throwable { // OutOfMemoryError, etc.
Bitmap original = ((BitmapDrawable) context.getDrawable())
.getBitmap();
Bitmap adjusted = Bitmap.createBitmap(original.getWidth(),
original.getHeight(),
original.getConfig());
Canvas canvas = new Canvas(adjusted);
canvas.setMatrix(view.getCanvasMatrix());
canvas.drawBitmap(original, 0, 0, null);
return adjusted;
}
这篇关于缩放后获取ImageView的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!