fhu*_*cho 38 android imageview android-imageview
我有一个ImageView android:layout_width=100dp,android:layout_height=wrap_content和android:adjustViewBounds=true
它的来源是50 x 50像素的图片.但不保留纵横比 - ImageView的高度为50px,而不是100px(即adjustViewBounds不工作).如果我有一个200x200px的图片它可以工作 - 宽度和高度是100px.此代码生成100px宽和50px高的图片,但src图像是正方形:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/photo"
android:src="@drawable/icon"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Rom*_*rik 66
问题是,adjustViewBounds 会不会增加尺寸的ImageView超越绘制的天然尺度.它只会缩小视图以保持纵横比; 如果您提供500x500图像而不是50x50图像,这应该可行.
如果您对实现此行为的位置感兴趣,请参阅ImageView.java的onMeasure实现.
一种解决方法是实现ImageView更改此行为的自定义onMeasure.
vla*_*ija 12
有一种更简单的方法.使你的ImageView像这样:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:adjustViewBounds="true"/>
Run Code Online (Sandbox Code Playgroud)
这样,drawable将通过保留纵横比来拉伸以适合ImageView中心.我们只需要计算正确的高度,使其成比例,这样我们就没有任何空白:
private void setImageBitmap(Bitmap bitmap, ImageView imageView){
float i = ((float)imageWidth)/((float)bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
imageView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth, (int) imageHeight));
imageView.setImageBitmap(bitmap);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47838 次 |
| 最近记录: |