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

标签 android imageview scaling

我试图找出 android 在缩放图像时正在做什么,特别是“centercrop”类型。所以为了找到答案,我搜索了 ImageView 源代码并找到了它 here .

所以我尝试的是这段代码:

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;
}

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

举个例子:

原图

enter image description here

原始 ImageView 中心裁剪

enter image description here

尝试过的代码

enter image description here

编辑: 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 是全屏的。这就是为什么我使用 screenSize 来处理它。

编码我如何应用它

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

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

最佳答案

我改编自 teh 来源。 它会根据您的申请方式更改对 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;
}

然后使用:

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

关于android - Imageview 缩放方法 "centercrop"作为代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42309477/

相关文章:

algorithm - 我应该使用什么算法来平滑地缩放图形或 map ?

android - 为什么我的 Android 应用程序中的图像选择器会自动将纵向图像旋转为横向图像?

android - 1个imageview android上的多个动画

android - 转到上一个 fragment 时如何避免关闭 Activity

java - Activity 之间的预加载器 - android java

javascript - 如何显示特定宽度和高度的图像的一部分?

c# - 您如何让 XAML 元素缩放以适合其容器?

c++ - Opengl 奇怪的旋转

android - 根据android中的微调器选择在listview中填充不同的数据

java - Google Play 游戏服务 VS Google Drive