Imageview缩放方法"centercrop"作为代码

Ahm*_*man 10 scaling android imageview

我试图找出android在缩放图像时正在做什么,特别是"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;
}
Run Code Online (Sandbox Code Playgroud)

但它并没有给我相同的结果.我究竟做错了什么 ?

下面是一个例子:

原始图片

在此输入图像描述

原始的ImageView Centercrop

在此输入图像描述

试过的代码

在此输入图像描述

编辑: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>
Run Code Online (Sandbox Code Playgroud)

所以我的ImageView是完全筛选的.这就是为什么我使用screenSize来处理它.

编码我如何应用它

Bitmap bluredBoxBackground  = buildBluredBoxBackground();
imageViewBluredBox.setImageDrawable(new BitmapDrawable(getResources(),bluredBoxBackground));
Run Code Online (Sandbox Code Playgroud)

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

Hen*_*ira 0

我改编自该来源。它将与您一起更改返回到 Matrix 的方式。

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;
}
Run Code Online (Sandbox Code Playgroud)

然后使用:

Matrix bluredBoxBackground = buildBluredBoxBackground();
imageViewBluredBox.setImageMatrix(bluredBoxBackground));
imageViewBluredBox.invalidate();
Run Code Online (Sandbox Code Playgroud)