Android, Java, Creating a thumbnail keeping aspect ratio -
Android, Java, Creating a thumbnail keeping aspect ratio -
i'm trying create thumbnail of height maintain aspect ratio. i'm using code below problem arises when if image little image generated not fill thumbnail area. imageuri path image.
bitmapfactory.options o = new bitmapfactory.options(); o.injustdecodebounds = true; bitmapfactory.decodefile(imageuri, o); final int required_size=70; int width_tmp=o.outwidth, height_tmp=o.outheight; int scale=4; while(true){ if(width_tmp/2<required_size || height_tmp/2<required_size) break; width_tmp/=2; height_tmp/=2; scale++; } bitmapfactory.options o2 = new bitmapfactory.options(); o2.insamplesize=scale; bitmap bitmap = bitmapfactory.decodefile(imageuri, o2);
your code scaling bitmap have @ to the lowest degree 1/4 of width or heigth. if original image big plenty smaller.
i assume display image in imageview (your thumbnail area?. if image not fill imageview have configure imageview resize image correctly. if imageview , image display have different aspect ratios, way create image fill imageview distort image.
what do: utilize bitmapfactory decode image size thats larger, size want thumbnail have. improve utilize powers of 2 scaling parameter, that. , set android:scaletype parameter of imageview create image display like:
public static bitmap decodebitmap(uri bitmapuri, contentresolver resolver, int width, int height) throws ioexception{ inputstream = resolver.openinputstream(bitmapuri); bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodestream(is,null,options); is.close(); int ratio = math.min(options.outwidth/width, options.outheight/height); int samplesize = integer.highestonebit((int)math.floor(ratio)); if(samplesize == 0){ samplesize = 1; } log.d(rsblbitmapfactory.class, "sample size: " + samplesize); options = new bitmapfactory.options(); options.insamplesize = samplesize; = resolver.openinputstream(bitmapuri); bitmap b = bitmapfactory.decodestream(is,null,options); is.close(); homecoming b; } <imageview android:scaletype="fitxy"></imageview> java android imageview
Comments
Post a Comment