android - How to set max-height of ImageView at runtime -
how set max height of imageview @ runtime?
for example, have image url
, has width greater screen width. want set imageview height screen's width
, make imageview square.
i can't find perfect solution this. appreciated deeply.
it doesn't matter size of image, code below size perfect ratio, make max width without making resolution suck.if dont want max width modify dimensions code. ll need picasso.
private dimensions getscreendimensions() { displaymetrics metrics = new displaymetrics(); windowmanager wm = (windowmanager) context.getsystemservice(context.window_service); display display = wm.getdefaultdisplay(); display.getmetrics(metrics); final int widthscreen = metrics.widthpixels; int heightscreen = metrics.heightpixels; dimensions screendimen = new dimensions(metrics.widthpixels,metrics.heightpixels); // custom class return screendimen; } private bitmap makecalc(dimensions screendimen, dimensions imagedimen,bitmap bmp) { float widthratio = screendimen.getwidth() / imagedimen.getwidth(); float scaledheight = imagedimen.getheight() * widthratio; imagebmp = bitmap.createscaledbitmap(bmp, (int)screendimen.getwidth(), (int) scaledheight, true); //image.setimagebitmap(resized); return imagebmp; } public class dimensions { float width; float height; public dimensions(float width, float height) { this.width = width; this.height = height; } public float getheight() { return height; } public void setheight(float height) { this.height = height; } public float getwidth() { return width; } public void setwidth(float width) { this.width = width; } } target target = new target() { @override public void onbitmaploaded(bitmap bitmap, picasso.loadedfrom from) { int width = bitmap.getwidth(); // original width int height = bitmap.getheight(); // original heigh log.w(tag, "onbitmaploaded: " + width + " " + height); bitmap newbmp = makecalc(getscreendimensions(),new dimensions(width,height),bitmap); imageview.setimagebitmap(newbmp); } @override public void onbitmapfailed(drawable errordrawable) { log.w(tag, "onbitmapfailed: "+errordrawable ); } @override public void onprepareload(drawable placeholderdrawable) { log.w(tag, "onprepareload: " ); } }; picasso.with(this).load(yoururl).into(target); imageview.settag(target);
Comments
Post a Comment