ImageView缩放方法"centercrop"的代码

9
我正在尝试了解安卓在缩放图像时的操作,特别是"centercrop"类型。为了找到答案,我搜索了ImageView的源代码,并在这里找到了它。
我尝试了以下代码:
public Bitmap buildBluredBoxBackground () {
        int [] screenSize = Utilities.getScreenSize(mainActivityContext); //screensize[0] = x and [1] is y
        Matrix mDrawMatrix = new Matrix();

        Bitmap bitmap = ((BitmapDrawable)fullscreenViewHolder.imageViewArt.getDrawable()).getBitmap();

        float scale;
        float dx = 0, dy = 0;

        if (bitmap.getWidth() * screenSize[1] > screenSize[0] * bitmap.getHeight()) {
            scale = (float) screenSize[1] / (float) bitmap.getHeight();
            dx = (screenSize[0] - bitmap.getWidth() * scale) * 0.5f;
        } else {
            scale = (float) screenSize[0] / (float) bitmap.getWidth();
            dy = (screenSize[1] - bitmap.getHeight() * scale) * 0.5f;
        }

        mDrawMatrix.setScale(scale, scale);
        mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));

        result = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),mDrawMatrix,true);

        ... //Some processing work

        return result;
}

但结果不同,我做错了什么?

以下是一个示例:

原始图片

输入图像描述

原始ImageView中心剪裁

输入图像描述

尝试的代码

输入图像描述

编辑: ImageView的XML

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageViewFullscreenArt"/>
            <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/imageViewFullscreenArtBluredBox"/>
</FrameLayout>

我的ImageView是全屏显示的,因此我使用屏幕尺寸来处理它。

下面是应用代码:

Bitmap bluredBoxBackground  = buildBluredBoxBackground();
imageViewBluredBox.setImageDrawable(new BitmapDrawable(getResources(),bluredBoxBackground));

详细说明: 我只是想达到与ImageView.setScaleType(ScaleType.CENTER_CROP)相同的效果。因此,我的代码应该像原始的setScaleType方法一样工作。为什么需要将它作为代码?因为在我的情况下,我无法获取ImageView的drawingcache,但我必须以某种方式处理和编辑它。


发布完整的可绘制代码?你是如何将其应用于ImageView的? - Anurag Singh
  1. 你正在从ImageView获取位图,其中可绘制对象可能已经与原始对象不同;
  2. 你正在使用屏幕的大小而不是目标视图的大小;
  3. 你正在创建一个缩放的位图,而不仅仅使用scaleType="matrix"。我需要看更多的代码才能理解你想要实现什么,但这应该是一个简单的修复。请发布你的活动代码以及具有目标视图的XML布局,再加上更详细的描述你试图解决的确切问题。
- kris larson
我编辑了我的帖子,加入了一些代码和更好的问题描述。希望这次能够清楚地理解。感谢您的帮助。 - Ahmet K
@AhmetKazaman 你已经解决了这个问题还是需要解决方案? - Anurag Singh
你将drawingcache设置为true并获取<FrameLayout>的drawingcache - Qamar
1个回答

0

我从源代码进行了适应。 如果您更改返回为矩阵,它将按您的应用程序工作。

public Matrix buildBluredBoxBackground () {

    int dwidth = imageView.getDrawable().getIntrinsicWidth();
    int dheight = imageView.getDrawable().getIntrinsicHeight();

    int vwidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
    int vheight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();

    Matrix mDrawMatrix = imageView.getImageMatrix();
    Bitmap bMap = imageView.getDrawingCache();

    float scale;
    float dx = 0, dy = 0;

    if (dwidth * vheight > vwidth * dheight) {
        scale = (float) vheight / (float) dheight;
        dx = (vwidth - dwidth * scale) * 0.5f;
    } else {
        scale = (float) vwidth / (float) dwidth;
        dy = (vheight - dheight * scale) * 0.5f;
    }
    mDrawMatrix.setScale(scale, scale);
    mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));
    return mDrawMatrix;
}

然后使用:

Matrix bluredBoxBackground = buildBluredBoxBackground();
imageViewBluredBox.setImageMatrix(bluredBoxBackground));
imageViewBluredBox.invalidate();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接