This happens when you load a picture with bitmapfactory, and the size of the returned picture doesn't match the actual size. This is because when we put the picture resources under the Res/drawable file path, the selected file is different, different folders will have different zoom.
1. Loading and displaying pictures
Place the 70*98 picture in the res/drawable-hdpi.
public class Mainactivity extends Activity {TextView tv_wl; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); tv_wl= (TextView) Findviewbyid (R.ID.TV_WL); Bitmap Bm1=bitmapfactory.decoderesource (Getresources (), r.drawable.img); int bm_width=bm1.getwidth (); int bm_height= Bm1.getheight (); Tv_wl.settext ("Width=" +bm_width+ "; Height= "+bm_height);}}
The results of the operation are as follows:
It's the same size as the original.
2, modify the file storage path
(1) Put under RES/DRAWABLE-MDPI, run effect
It's 1.5 times times bigger in size.
(2) put under RES/DRAWABLE-XHDPI, run effect
1.3 times times reduction in size
(3) put under RES/DRAWABLE-XXDPI, run effect
Twice times reduction in size
3. Find out why
The size of the computer piece, measured in pixels. Android phone screens ldpi, MDPI, hdpi, and even xhdpi, for MDPI (density=160) devices, 1dp=1px, hdpi (density=240) devices, 1dp=1.5px. So, put the picture in the res/drawable-mdpi directory, and the running Android device screen belongs to hdpi, resulting in the image size will be enlarged 1.5 times times.
4. Solutions
(1) The current Android devices are generally hdpi and above, so it is recommended to put the picture resources mainly in the hdpi.
(2) Modify the above code to set the scale to False
<span style= "White-space:pre" ></span>bitmapfactory.options bfooptions = new BitmapFactory.Options (); bfooptions.inscaled = false; Bitmap img1 = Bitmapfactory.decoderesource (Getresources (), r.drawable.img, bfooptions); int bm_width=img1.getwidth (); int Bm_height=img1.getheight ();
After running you will find that wherever you place the picture, it is the original size.
Reference Documentation:
Http://www.eoeandroid.com/blog-633294-2751.html
http://q.cnblogs.com/q/47175/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bitmap by GetWidth and getheight to get size mismatch