android api16怎么设置图片的宽高,android

我有下一个xml布局:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fff"

android:gravity="center_vertical"

android:orientation="vertical" >

android:id="@+id/my_frame"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#fff"

android:gravity="center_vertical" >

android:id="@+id/image_areas"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:adjustViewBounds="true"

android:background="#9a05e7"

android:scaleType="fitXY"

android:src="@drawable/mapa_mask"

android:visibility="invisible"

tools:ignore="ContentDescription" />

android:id="@+id/image"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentRight="true"

android:adjustViewBounds="true"

android:background="#fff"

android:scaleType="fitXY"

android:src="@drawable/mapa_default"

android:visibility="visible"

tools:ignore="ContentDescription" />

有两个图像“覆盖”.

如果我在已安装Android 4.3或更高版本(> = API 18)的设备上测试该应用程序,则图像纵横比将保持完美.但是,如果我在API> = 14 API< = 17之间的设备上测试该应用程序,则图像显示为“高度狭窄”.

我用下一个设置了正确的大小,但是没有用:

// load the original BitMap (768 x 583 px)

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),

R.drawable.mapa_default);

// iv is an ImageView referenced in onCreate (mapa_default)

int width = bitmapOrg.getWidth();

int height = bitmapOrg.getHeight();

int newWidth = iv.getWidth();

int newHeight = heightRealImg;

// calculate the scale

float scaleWidth = ((float) newWidth) / width;

float scaleHeight = ((float) newHeight) / height;

// create a matrix for the manipulation

Matrix matrix = new Matrix();

// resize the bit map

matrix.postScale(scaleWidth, scaleHeight);

// recreate the new Bitmap

Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,

height, matrix, true);

// make a Drawable from Bitmap to allow to set the BitMap

// to the ImageView, ImageButton or what ever

BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

// set the Drawable on the ImageView

iv.setImageDrawable(bmd);

// center the Image

iv.setScaleType(ScaleType.FIT_XY);

iv.setAdjustViewBounds(true);

之所以崩溃,是因为iv.getWidth返回0.我认为这是因为布局尚未完成加载.

如何保持图像宽高比?

谢谢

我的解决方案:

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/my_frame"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="#fff"

android:gravity="center_vertical" >

android:id="@+id/image_areas"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:adjustViewBounds="true"

android:background="#9a05e7"

android:scaleType="fitXY"

android:src="@drawable/mapacyl_mask"

android:visibility="invisible"

tools:ignore="ContentDescription" />

android:id="@+id/image"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#fff"

android:scaleType="fitXY"

android:src="@drawable/mapacyl"

android:visibility="visible"

tools:ignore="ContentDescription" />

在这种情况下,我可以管理“所有”屏幕的触摸,并且不需要获取图像的宽度和高度或修改这些值.而且使用fitXY图像可以扩展并适应所有屏幕尺寸,但比例无法保持.

非常感谢@Doctoror Drive

解决方法:

为什么不使用

android:scaleType="centerCrop"?

编辑:

您无需在代码中做任何其他事情.

您误解了wrap_content和scale类型的含义.

wrap_content表示View将与图像一样小,并且不大于其父边界.

fitXY表示将在不保留宽高比的情况下调整图像大小以适合整个ImageView.

canterCrop手段

Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).

所以也许你想尝试

android:scaleType="centerInside"

Scale the image uniformly (maintain the image’s aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).

标签:imageview,android

来源: https://codeday.me/bug/20191122/2057417.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值