【问题标题】:Image Scaling Android图像缩放安卓
【发布时间】:2016-02-15 21:28:51
【问题描述】:

我正在向 Imageview 显示图片库中的图像。

我正在使用下面的代码

 Bitmap background = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
                float originalWidth = bMap.getWidth(), originalHeight = bMap.getHeight();
                Canvas canvas = new Canvas(background);
                float scale = width/originalWidth;
                float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
                Matrix transformation = new Matrix();
                transformation.postTranslate(xTranslation, yTranslation);
                transformation.preScale(scale, scale);
                Paint paint = new Paint();
                paint.setFilterBitmap(true);
                canvas.drawBitmap(bMap, transformation, paint);
                imageView1.setImageBitmap(background);

案例 1:工作。

Original Image

Output of above code.

案例 2:不工作

Original Image.

Output of above code.

在情况 2 中,为什么图像没有正确缩放以填充图像视图? 请让我知道我哪里出错了。

【问题讨论】:

  • 为什么不让 ImageView 为您缩放和裁剪图像?
  • 因为我想保持纵横比..打算怎么做?
  • 查看 ImageView scaleTypecropCenter。
  • 让我试试.. :)
  • cropCenter 工作.. :)

标签: android matrix imageview


【解决方案1】:

Android 已经提供了一种创建缩放位图的方法。看看这个LINK

【讨论】:

  • 但是纵横比呢?
  • 这很容易。只需提供保持原始纵横比的新尺寸。这就是我要保持纵横比的方法。
【解决方案2】:

你想让它伸展吗?使用fitxy。

【讨论】:

  • 然后你可以使用它,因为你的第一张图片也是在你的样本上裁剪的,
【解决方案3】:

在布局 xml 文件中使用 ImageView 上的 scaleType 属性。

示例:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop" />

然后您可以使用setImageUri(Uri uri)ImageView 上设置您的图像(我假设您有要显示的图像的Uri)。

【讨论】:

  • scaleType 应该是 centerInside 以满足您的要求。
  • 对不起。它应该是 centerCrop。这绝对是正确的方法。如果它不起作用,则您的代码中有另一个错误。
猜你喜欢
  • 2011-07-19
  • 2023-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
相关资源
最近更新 更多