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

解决方案 »

  1.   

    因为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);
    }
      

  2.   

    这样做后,得到的Bitmap,使用setImageBitmap(),就和直接使用setImageResource()设置的图片一样大了吗?