Android Changing Image Size Depending On Screen Size?
So I need to change the size of an image depending on the area of the screen. The image will have to be half of the screen height, because otherwise it overlaps some text. So Heig
Solution 1:
You can do this with LayoutParams
in code. Unfortunately there's no way to specify percentages through XML (not directly, you can mess around with weights, but that's not always going to help, and it won't keep your aspect ratio), but this should work for you:
//assuming your layout is in a LinearLayout as its rootLinearLayoutlayout= (LinearLayout)findViewById(R.id.rootlayout);
ImageViewimage=newImageView(this);
image.setImageResource(R.drawable.image);
intnewHeight= getWindowManager().getDefaultDisplay().getHeight() / 2;
intorgWidth= image.getDrawable().getIntrinsicWidth();
intorgHeight= image.getDrawable().getIntrinsicHeight();
//double check my math, this should be right, thoughintnewWidth= Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(
newWidth, newHeight);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER_CROP);
layout.addView(image);
Might be overcomplicated, maybe there's an easier way? This is what I'd first try, though.
Post a Comment for "Android Changing Image Size Depending On Screen Size?"