java - Android: How to stretch an image to the screen width while maintaining aspect ratio? -
i want download image (of unknown size, square) , display fills screen horizontally, , stretches vertically maintain aspect ratio of image, on screen size. here (non-working) code. stretches image horizontally, not vertically, squashed...
imageview mainimageview = new imageview(context); mainimageview.setimagebitmap(mainimage); //downloaded server mainimageview.setscaletype(scaletype.fit_xy); //mainimageview.setadjustviewbounds(true); //with line enabled, scales image down addview(mainimageview,new linearlayout.layoutparams( layoutparams.fill_parent, layoutparams.fill_parent));
i accomplished custom view. set layout_width="fill_parent" , layout_height="wrap_content", , point appropriate drawable:
public class banner extends view { private final drawable logo; public banner(context context) { super(context); logo = context.getresources().getdrawable(r.drawable.banner); setbackgrounddrawable(logo); } public banner(context context, attributeset attrs) { super(context, attrs); logo = context.getresources().getdrawable(r.drawable.banner); setbackgrounddrawable(logo); } public banner(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); logo = context.getresources().getdrawable(r.drawable.banner); setbackgrounddrawable(logo); } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int width = measurespec.getsize(widthmeasurespec); int height = width * logo.getintrinsicheight() / logo.getintrinsicwidth(); setmeasureddimension(width, height); } }
Comments
Post a Comment