开发者

Android ImageView.setImageURI scales image

开发者 https://www.devze.com 2022-12-20 14:52 出处:网络
I have a view that I am drawing to a bitmap, saving to disk and then putting in an ImageView via setImageURI. However, when the image is displayed in the ImageView it is not being shown at the correct

I have a view that I am drawing to a bitmap, saving to disk and then putting in an ImageView via setImageURI. However, when the image is displayed in the ImageView it is not being shown at the correct size. It is about 1/3 smaller than it should be. I'm guessing that this is a density issue, but I can't figure out what's going wrong (my emulator is WVG开发者_运维技巧A). Ideas anyone?


I ran into this issue today too. I ended up loading a bitmap instead.

            Bitmap bit = BitmapFactory.decodeFile(image_path);
            mImageView.setImageBitmap(bit);

Which displays correctly.


You're right This is a density issue. To ensure that your image displays in the correct dimensions regardless of the density of the device you should consider using dip (density independent pixel) units.

Also, Android 1.5 does not support image density -- ie. it doesn't know how to distinguish between mdpi, hdpi, ldpi bitmaps. Android 1.6 and above does. You can use Bitmap.setDensity() or BitmapDrawable.setTargetDensity()

Finally -- you mention it is 1/3 small that it should be which is a good indication that the problem is related to density as mdpi density is 160dpi and hdpi is 240dpi -- 160/240 = 2/3 which is 1/3 less than your original image.

Hope this helps!


Try this:

ImageView photo = (ImageView)findViewById(R.id.photo);
photo.setScaleType(ImageView.ScaleType.CENTER);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号