繁体   English   中英

图像缩放Android

[英]Image Scaling Android

我正在显示从图库到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:工作。

原始图片

以上代码的输出。

情况2:无法运作

原始图像。

以上代码的输出。

在情况2中,为什么无法正确缩放图像以填充imageview? 请让我知道我要去哪里了。

Android已经提供了一种创建缩放位图的方法。 查看此链接

您要伸展吗? 使用fitxy。

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

例:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM