【问题标题】:Imageview scaling method "centercrop" as codeImageview缩放方法“centercrop”作为代码
【发布时间】:2017-02-18 00:07:06
【问题描述】:

我试图找出 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;
}

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

这是一个例子:

原图

原始图像视图中心裁剪

尝试过的代码

编辑: 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 的绘图缓存,但我必须以某种方式处理和编辑它。

【问题讨论】:

  • 贴出完整drawable的代码?你是如何在 ImageView 上应用它的?
  • 1) 您从ImageView 获取位图,其中drawable 可能已从原始位置更改; 2)您使用的是屏幕大小而不是目标视图的大小; 3) 您正在创建一个缩放位图,而不仅仅是使用scaleType="matrix"。我需要查看更多代码以了解您要完成的工作,但这应该是一个简单的修复。将您的活动代码与具有目标视图的 XML 布局一起发布,并更详细地描述您要解决的确切问题。
  • 我用一些代码编辑了我的帖子,并更好地描述了我的问题。希望这次清楚明白。感谢您的帮助
  • @AhmetKazaman 你解决了这个问题还是需要解决方案?
  • 你设置drawingcache为真,得到的drawingcache

标签: android imageview scaling


【解决方案1】:

我改编自源代码。 它将与您一起更改返回到 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多