首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android图片有关问题

2012-04-14 
Android图片问题大家好,我使用ImageView显示图片的时候,设置的属性:android:layout_widthwrap_contenta

Android图片问题
大家好,我使用ImageView显示图片的时候,设置的属性:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
使用资源:
mImageView.setImageResource(R.drawable.image);
和直接使用图片:
mImageView.setImageBitmap(BitmapFactory.decodeResource(R.drawable.image));
两者在手机上显示的大小不一致。
请问这是为什么?谢谢大家。

[解决办法]
因为itmapFactory.decodeResource会在inTargetDensity获取当前屏幕的DPI,所以如果你放图片的文件夹的分辨率和DPI不对应,图片可能就缩小了,可以用下面方式处理。
private Bitmap decodeResource(Resources resources, int id) {
TypedValue value = new TypedValue();
resources.openRawResource(id, value);
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inTargetDensity = value.density;
return BitmapFactory.decodeResource(resources, id, opts);
}

热点排行