Scaling large Size Bitmap in Android
In android we go through a really common problem of Bitmap scaling , As we know android have several devices and we have to decode our image in dynamic so that it wont look stretched or blurred on any of android devices , There are several methods provided by android OS for Bitmap decoding but when we override them directly for large size Bitmap then some time we face Memory Leak issues for smaller devices i.e(Low processing devices) ,
To over come this problem We have keep one thing in mind we shouldn’t change the image Aspect ration.
Aspect ratio Means–
For example If we have set the image using all the width of the device and some specific height , then we have Scaled the Bitmap height Wise , that means the Width constant should be constant and height will vary device to device . Here is the easy way to implement the same —
// Method to Scale the image height wiseprivate Bitmap decodeFile(String FileName) {WindowManager windowManager= ((Activity) this).getWindowManager();Display divDisplay = windowManager.getDefaultDisplay();divDisplay = window.getDefaultDisplay();width = divDisplay.getWidth(); // Width of the actual deviceheight = divDisplay.getHeight(); // height of the actual deviceString issueListURL = SplashScreen.SDCARD_PATH_FORUNZIP + "/issue"+ issueID + "/" + FileName + "";Bitmap originalBitmap = BitmapFactory.decodeFile(issueListURL);Bitmap Objbitmap = originalBitmap;if (bitmap != null) {// scaling the bitmap in respect to the width of the device to get// variant height for different android //devicesint heightofBitMap = Objbitmap.getHeight();int widthofBitMap = Objbitmap.getWidth();heightofBitMap = width * heightofBitMap / widthofBitMap;widthofBitMap = width;// Scaling the bitmap according to new height and widthbitmap = Bitmap.createScaledBitmap(bitmap, widthofBitMap,heightofBitMap, true);}return bitmap;}
Comments
Post a Comment