缩放后获取ImageView的位图 [英] Get Bitmap of ImageView after scaling

查看:114
本文介绍了缩放后获取ImageView的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView和一个Bitmap,并且ScaleTypeFitXY,当我从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的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆