I would like to set the width and height for a rotated ImageView programmatically. I tried this code:
imageView.setRotation(rotation);
layoutParams.height = 890;//width;
layoutParams.width = 1900;//height;
imageView.setLayoutParams(layoutParams);
imageView.requestLayout();
imageView.invalidate();
The result is that the width are 1200px (this is maximum width of the device) and the height is 890px. The problem seems to be that the rotation is not finished yet. How can I handle this to wait for the rotation?
Related
Can I set value for the width of Android ImageView in XML without changing its original initial ratio?
I solved this problem in java but I can't do the same in Android XML.
What I tried so far:
int width = (inital width);
int height = (inital height);
int fixedWidth = Resources.getSystem().getDisplayMetrics().widthPixels >> 1;
int fixedHeight = (fixedWidth * height ) / (width); // 50% width of screen
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(fixedWidth, fixedHeight);
imageView.setLayoutParams(layoutParams);
Add attribute in your imageView
android:adjustViewBounds="true"
and wrap-content your height, this way your height will be in aspect ratio
I have to set background image of ImageView and after time to set imageSource of this ImageView.
So what I did is to set image view background with setBackgroundImage().
Also I set
adjustViewBounds = true and scaleType = "fitCenter", and height = 55dp and width = wrap content.
So I expected at first time(when I set only set background) to have this scenario - the view will be with the same ration and will fit the smallest - width or height (of course if height is larger than 55dp, it will be scaled). But the result is scalled image (when the image should be larger than 55 dp i.e. height is scaled). The image is in RelativeLayout. Unfortunately, I cannot post the whole code.
Thanks in advance.
You have to subclass that ImageView and override onLayout() to keep width/height ratio like so:
#Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
double ratio = ...
int width = right - left;
int height = ratio * width;
ViewGroup.LayoutParams params = this.getLayoutParams();
params.height = height;
params.width = width;
this.setLayoutParams(params);
this.setMeasuredDimension(width, height);
super.onLayout(changed, left, top, right, top + height);
}
I have gridview which contains 2 columns. The width is set to fill available space.
I need height to be set to specific size according to width, because in every screen the width dimension is different, I can't set height in xml as constant. I need to set ratio, so height should be about 1.5 * width.
Thanks
You can set Height, but you want to get Width First.
You told 2 columns of grid so understand phone half of your phone width
Display display = getWindowManager().getDefaultDisplay();
int imgWidth = display.getWidth() / 2;
int imgHeight = (int)(imgWidth * 1.5f);
You get height of that.Now you can set your Grid View Width and Height
LayoutParams lp = HERE_YOUR_VIEW.getLayoutParams();
lp.width = imgWidth;
lp.height = imgHeight ;
HERE_YOUR_VIEW.setLayoutParams(lp);
Hi i'm working on an application that will be loading images and i'm looking to scale each image to it's largest possible size for instance if the image is a landscape image (if width is larger than height) i would like to stretch the width to fill the width of the phone and scale height to keep it's aspect ratio. If the height is larger than the width i.e. a portrait image the image should be scaled to fit the height of the phone and width should then adjust to keep the aspect ratio i've had a bit of trouble getting this to work here's what i've done so far though any help would be greatly appreciated.
final ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setBackgroundResource(android.R.color.black);
//TODO need to get actual size of drawable not view size which is 0
ViewTreeObserver vto = i.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int w = i.getMeasuredWidth();
int h = i.getMeasuredHeight();
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
if(w <= h){
//TODO need to think about landscape
h = height - convertDpToPixel(50, context);
w = w*(width);
}else{
h = h*(height);
w = width;
}
//TODO set imageview to w and h
return true;
}
});
for get Display width , height
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
For Resize Imageview
ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);
Increase either the height or the width to their maximum, depending on which is already greater.
Now, let's say you're working with a landscape image:
You've already expanded the width to it's maximum size, the display's width
The side you expanded is the width of the image, so assuming the display width = width and the image's width = w:
width / w = r
So r would be the ratio between the display width and the image width. Now you need to use this to increase your height.
Simply multiply the height by the ratio, and set the image's new height to the resulting number.
If you end up with a portait image... well, I think you'll be able to figure it out. It's pretty much the same thing.
After all that, I think it would just be a matter of positioning the image in the center of the screen.
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 Height= 1/2 Screen Height.
Width = Height*Aspect Ratio (Just trying to keep the aspect ratio the same)
I found something that was:
Display myDisplay = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width =myDisplay.getWidth();
int height=myDisplay.getHeight();
But how would I change image height in java? or even XML if possible? I can't seem to find a working answer.
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 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 my math, this should be right, though
int newWidth = Math.floor((orgWidth * newHeight) / orgHeight);
//Use RelativeLayout.LayoutParams if your parent is a RelativeLayout
LinearLayout.LayoutParams params = new LinearLayout.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.