Android 显示图片缩放的注意事项

最近项目有个需求,在ImagView显示一张图片的预览效果,要求该图片宽度占对话框宽度的4/5,高度与宽度成比例缩放(不能变形),看下面的示意图

示意图


图片宽度占对话框的4/5容易解决,使用weight属性就可以了,代码如下

<LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:layout_below="@+id/et_messages"
	    >
	    <TextView 
	        android:layout_width="0dp"
	        android:layout_height="wrap_content"
	        android:layout_weight="1"
	        />
	    <ImageView 
		    android:id="@+id/iv_picture"
		    android:layout_width="0dp"
		    android:layout_height="wrap_content"
		    android:layout_marginRight="5dip"
		    android:scaleType="centerInside"
		    android:layout_weight="4"
		    />
	</LinearLayout>


Java 代码

ImageView ivPicture = (ImageView)dialog.findViewById(R.id.iv_picture);
try {
		bitmap = ImageUtils.getBitmapFromAssets(context, "WS_MainMenuCommonShared_iphone.png");
	
}catch(IOException e) {
	e.printStackTrace();
}
ivPicture.setBackgroundDrawable(new BitmapDrawable(bitmap));




大家可以看到,图片缩放是 android:scaleType="centerInside" ,原以为这样就大功告成,可是结果并没有像想象的那样,图片没有按预期的方式缩放。

Google了一下,原来使用android:scaleType="centerInside" 时,图片不能使用设置背景的方式,最后一行代码改为

ivPicture.setImageBitmap(bitmap);

就可以了,效果刚刚好。这里之所以把这个问题记下来,其实想汇总一些ImageView使用时的一些特别之处,以后还会继续补充,谢谢!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值