我希望ImageView能够按照相同的比例缩放以填充布局,而不被裁剪。由于宽度将首先匹配,因此有效地意味着缩放直到宽度最大化。
我的代码如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
ImageView img = findViewById(R.id.displayImg);
img.setImageBitmap(bmp); // bmp is a public static Bitmap set by another class
img.setRotation(90);
img.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
我的布局是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/displayImg" />
</android.support.constraint.ConstraintLayout>
根据Android ImageView ScaleType: A Visual Guide,
CENTER_INSIDE
应该:将图像等比例缩放(保持图像的宽高比),使得图像的宽度和高度都小于或等于视图对应的维度(减去填充)。当前显示的是黑色图像,我已经画出了我认为应该看到的图像(显然,我是现代版的米开朗基罗)。
![image](https://istack.dev59.com/k0vHu.webp)
wrap_content
来显示实际大小。而如果图像比屏幕尺寸小,并且您将其设置为match_parent
,则会出现模糊或旋转时缩放异常的情况。 - tyczj