android - How can i create custom image view so that the pic which i have taken should be fit into that image view -


i trying make application, fetches image storage , place application image size looks it.

scaling bitmaps memory can memory intensive. avoid crashing app on old devices, recommend doing this.

i use these 2 methods load bitmap , scale down. split them 2 functions. createlightweightscaledbitmapfromstream() uses options.insamplesize perform rough scaling required dimensions. then, createscaledbitmapfromstream() uses more memory intensive bitmap.createscaledbitmap() finish scaling image desired resolution.

call createscaledbitmapfromstream() , should set.

lightweight scaling

public static bitmap createlightweightscaledbitmapfromstream(inputstream is, int minshrunkwidth, int minshrunkheight, bitmap.config config) {    bufferedinputstream bis = new bufferedinputstream(is, 32 * 1024);   try {     bitmapfactory.options options = new bitmapfactory.options();     if (config != null) {       options.inpreferredconfig = config;     }      final bitmapfactory.options decodeboundsoptions = new bitmapfactory.options();     decodeboundsoptions.injustdecodebounds = true;     bis.mark(integer.max_value);     bitmapfactory.decodestream(bis, null, decodeboundsoptions);     bis.reset();      final int width = decodeboundsoptions.outwidth;     final int height = decodeboundsoptions.outheight;     log.v("original bitmap dimensions: %d x %d", width, height);     int sampleratio = math.max(width / minshrunkwidth, height / minshrunkheight);     if (sampleratio >= 2) {       options.insamplesize = sampleratio;     }     log.v("bitmap sample size = %d", options.insamplesize);      bitmap ret = bitmapfactory.decodestream(bis, null, options);     log.d("sampled bitmap size = %d x %d", options.outwidth, options.outheight);     return ret;   } catch (ioexception e) {     log.e("error resizing bitmap inputstream.", e);   } {     util.ensureclosed(bis);   }   return null; } 

final scaling (calls lightweight scaling first)

public static bitmap createscaledbitmapfromstream(inputstream is, int maxwidth, int maxheight, bitmap.config config) {    // start grabbing bitmap file, sampling down little first if image huge.   bitmap tempbitmap = createlightweightscaledbitmapfromstream(is, maxwidth, maxheight, config);    bitmap outbitmap = tempbitmap;   int width = tempbitmap.getwidth();   int height = tempbitmap.getheight();    // find greatest ration difference, shrink both sides to.   float ratio = calculatebitmapscalefactor(width, height, maxwidth, maxheight);   if (ratio < 1.0f) { // don't blow small images, shrink bigger ones.     int newwidth = (int) (ratio * width);     int newheight = (int) (ratio * height);     log.v("scaling image further down %d x %d", newwidth, newheight);     outbitmap = bitmap.createscaledbitmap(tempbitmap, newwidth, newheight, true);     log.d("final bitmap dimensions: %d x %d", outbitmap.getwidth(), outbitmap.getheight());     tempbitmap.recycle();   }   return outbitmap; } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -