imageview - Android Changing image size depending on Screen Size? -
so need change size of image depending on area of screen. image have half of screen height, because otherwise overlaps text.
so height= 1/2 screen height. width = height*aspect ratio (just trying keep aspect ratio same)
i found was:
display mydisplay = ((windowmanager) getsystemservice(context.window_service)).getdefaultdisplay(); int width =mydisplay.getwidth(); int height=mydisplay.getheight();
but how change image height in java? or xml if possible? can't seem find working answer.
you can layoutparams
in code. unfortunately there's no way specify percentages through xml (not directly, can mess around weights, that's not going help, , won't keep aspect ratio), should work you:
//assuming layout in linearlayout root linearlayout layout = (linearlayout)findviewbyid(r.id.rootlayout); imageview image = new imageview(this); image.setimageresource(r.drawable.image); int newheight = getwindowmanager().getdefaultdisplay().getheight() / 2; int orgwidth = image.getdrawable().getintrinsicwidth(); int orgheight = image.getdrawable().getintrinsicheight(); //double check math, should right, though int newwidth = math.floor((orgwidth * newheight) / orgheight); //use relativelayout.layoutparams if parent relativelayout linearlayout.layoutparams params = new linearlayout.layoutparams( newwidth, newheight); image.setlayoutparams(params); image.setscaletype(imageview.scaletype.center_crop); layout.addview(image);
might overcomplicated, maybe there's easier way? i'd first try, though.
Comments
Post a Comment