如何将随机大小的图像拟合到ImageView?
什么时候:
ImageView尺寸为250dp*250dpImageView缩放后,尺寸应与缩放图像的尺寸相匹配例如,对于100*150的图像,图像ImageView应为166*250.
例如,对于150*100的图像,图像ImageView应为250*166.
如果我将界限设置为
<ImageView
android:id="@+id/picture"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:adjustViewBounds="true" />
Run Code Online (Sandbox Code Playgroud)
图像适合在ImageView,但ImageView总是250dp*250dp.
我需要调整ImageView的大小以使其适合屏幕,保持相同的宽高比.以下条件成立:
例如,400像素宽屏幕上的小型50x50图像会将图像缩放到400x400像素.800x200图像将缩放到400x100.
我已经查看了大多数其他线程,并且许多提议的解决方案就像简单地更改adjustViewBounds并且scaleType只会缩小图像,而不是扩展它.
在我的应用程序中,绘制位图就好像颜色是一些较低质量的类型.如果我使用图库应用程序加载背景图像,它加载很好,看起来不是它的超低质量.我用来加载和绘制图像的代码很简单:
//Code for initializing the Bitmap
Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.none), (int) (canvas.getWidth() * compression), (int) (canvas.getHeight() * compression), true);
//...
//Code for drawing this Bitmap
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), null);
Run Code Online (Sandbox Code Playgroud)
如果代码中没有任何内容告诉您出了什么问题,我制作了一个图像,比较图像在计算机或其他图像查看器上的实际外观,以及它在应用程序中的外观.
