vhi*_*efa 4 android resize image getresource imageview
我有两个ImageViews如下:
<ImageView android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
<ImageView android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
/>
Run Code Online (Sandbox Code Playgroud)
第一个imageView以实际大小显示图像,第二个ImageView应该以fitCenter大小显示图像。
我一直在尝试此代码:
ImageView img1 = (ImageView) findViewById(R.id.image1);
ImageView img2 = (ImageView) findViewById(R.id.image2);
img2.setImageDrawable(img1.getDrawable());
Run Code Online (Sandbox Code Playgroud)
但是第二个ImageView只是将图像显示为第一个。是的,以其实际大小。
谁能帮忙找到解决方案?
谢谢
您可以尝试如下操作:
ImageView img2 = (ImageView) findViewById(R.id.image2);
img2.setScaleType(ImageView.ScaleType.CENTER_CROP);
img2.setImageDrawable(img1.getDrawable());
Run Code Online (Sandbox Code Playgroud)
不要像那样使用相同的可绘制对象。可绘制对象有界限,这样做它们将共享这些界限。您需要制作 Drawable 的副本,并将其设置为 img2 的drawable。
Drawable clone = drawable.getConstantState().newDrawable();
Run Code Online (Sandbox Code Playgroud)
将制作可绘制对象的副本。