This article is an example of the implementation of Android programming, such as proportional display of the image method. Share to everyone for your reference, specific as follows:
In Android, because of the density of the impact, if you want the picture of the width is not good, specific why I will probably say, the specific please search for the mother or ancient brother. The reason is that if you put the picture in the drawable-mdpi, and the phone is drawable-hdpi, the picture is automatically magnified, so the width and height is not necessarily correct. So how do you get Android's image to be based on the proportions of the original image, first you can create a drawable-nodpi directory under the Res directory, where the picture is not stretched or shrunk by the number of DPI. Then, is based on the screen width and width of the picture on the screen to show the high, wide is fixed, is the width of the screen, so do not forget.
private void Getwidth_height () {
Display display = Getwindowmanager (). Getdefaultdisplay ();
int width = display.getwidth (); Deprecated
int height = display.getheight ();//deprecated
Bitmap mbitmap = Createimagewithresouce ( R.DRAWABLE.HISTORY4);
Image.setlayoutparams (New Layoutparams (width, width/getbitmapwidth (mbitmap) * Getbitmapheight (MBITMAP)));
Image.setimagebitmap (Createimagewithresouce (R.drawable.history4));
}
Private Bitmap createimagewithresouce (int resourceid) {
Bitmap bit = Bitmapfactory.decoderesource (Getresources (), R.DRAWABLE.HISTORY4);
return bit;
}
private int getbitmapwidth (Bitmap Bitmap) {return
bitmap.getwidth ();
}
private int getbitmapheight (Bitmap Bitmap) {return
bitmap.getheight ();
}
Free Bitmap
private void Releasebitmap (Bitmap Bitmap) {
if (bitmap!=null &&!bitmap.isrecycled ()) { C21/>bitmap.recycle ();
bitmap = null;
}
}
It is recommended to use the following LRUCache as a management
public class Imageutil {private lrucache<string, bitmap> Mmemorycache;
Private final context Mcontext;
private static Imageutil Imageutil;
private static Object obj = new Object ();
private int memclass;
private int cacheSize;
Private Imageutil (context mcontext) {this.mcontext = Mcontext;
Createlrucache (Mcontext); } private void Createlrucache (context mcontext) {Memclass = ((Activitymanager) Mcontext.getsystemservice (context.activ
Ity_service)). Getmemoryclass ();
CacheSize = 1024 * 1024 * MEMCLASS/8; Mmemorycache = new lrucache<string, bitmap> (cacheSize) {@Override protected int sizeOf (String key, Bitmap Val
UE) {//TODO auto-generated Method stub return Value.getrowbytes ();
}
}; public static Imageutil getinstance (context Mcontext) {if (Imageutil = = null) {synchronized (obj) {if (imag
Eutil = = null) {Imageutil = new Imageutil (mcontext);
}} return imageutil; } public void Adjustimagesize (ImagevieW imageview, int imageresourceid) {Bitmap mbitmap = null;
Display display = ((mainactivity) mcontext). Getwindowmanager (). Getdefaultdisplay (); int width = display.getwidth (); deprecated int height = display.getheight ();
Deprecated Bitmap Bitmapcache = mmemorycache.get (Imageresourceid + "");
if (Bitmapcache!= null) {mbitmap = Bitmapcache;
else {Mbitmap = Createimagewithresouce (Mcontext, Imageresourceid);
Mmemorycache.put (Imageresourceid + "", mbitmap); } relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (width, width/getbitmapwidth (mBitmap)
* Getbitmapheight (MBITMAP));
Layoutparams.addrule (Relativelayout.align_parent_bottom);
Imageview.setlayoutparams (Layoutparams);
Imageview.setbackgrounddrawable (New bitmapdrawable (Mbitmap));
Imageview.setimagebitmap (MBITMAP); private static Bitmap Createimagewithresouce (context context, int ResourceID) {Bitmap bit = Bitmapfactory.decodereso Urce (Context.getresources (), R.drawable.ic_launcher);
return bit;
private int getbitmapwidth (Bitmap Bitmap) {return bitmap.getwidth ();
private int getbitmapheight (Bitmap Bitmap) {return bitmap.getheight ();
}
}
I hope this article will help you with your Android programming.