当前位置: 代码迷 >> Android >> Android图片有关问题
  详细解决方案

Android图片有关问题

热度:30   发布时间:2016-05-01 21:35:37.0
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);
}
  相关解决方案