Android, match the picture with dimension of the screen - android

I have a picture of fixed height (60 px by 40 px).
I am trying to position the image by programmatically by adding margins to the image so that the image aligns to the center of the view
How I can get it done correctly to work for all different size and devices?

Make the image have a width and height of wrap content and a layout_gravity of centerHorizontal

You can make image params to match parent and then add margins of your choice.
ImageView image = new ImageView(this);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.setMargins(left, top, right, bottom);
image.setLayoutParams(params);

Related

How to properly scale an imageview within parent layout?

In my App. I have a RelativeLayout containing an ImageView.
The RelativeLayout is set to MATCH_PARENT horizontally and WRAP_CONTENT vertically.
The ImageView is set to WRAP_CONTENT both horizontally and vertically. The image Drawable is a 24x24 Vector icon.
Because the icon is too small to be seen, I want to double its size. That's where the issue is:
// Parent RelativeLayout
RelativeLayout titleLayout = new RelativeLayout(MyApplication.getContext());
titleLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//Icon
RelativeLayout.LayoutParams information_icon_layout = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
information_icon_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
ImageView information_icon = new ImageView(MyApplication.getContext());
information_icon.setLayoutParams(information_icon_layout);
information_icon.setPadding(
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_left),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_top),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_right),
getResources().getDimensionPixelSize(R.dimen.schedule_element_title_layout_padding_bottom)
);
information_icon.setImageDrawable(MyApplication.getContext().getResources().getDrawable(R.drawable.ic_info_outline_black_24dp));
information_icon.setScaleX(2);
information_icon.setScaleY(2);
information_icon.setAdjustViewBounds(true);
The icon scales as expected, but the parent RelativeLayout does not seem to take into account this change, and still calculates its vertical size using the original size of the Icon (24x24).
As a result, the icon is scaling outside of the parent, and only part of it is visible.
My question: How can I tell the parent RelativeLayout to take into account the scaled size of the icon?
Use scaletype property of an imageview it has main 3 property
center,
centercrop,
centerinside,
And if you want to scratch the image fully it also has property as fit center,fit end,fit start,fitxy.You can use this property in xml as well as java.
here is the link
scaletyper

Can't fit ImageView on screen

How to fit image fully on screen? I need to have WRAP_CONTENT, not MATCH_PARENT
im.setImageResource(resource);
im.setAdjustViewBounds(true);
im.setScaleType(ImageView.ScaleType.CENTER_CROP);
im.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
This codes places the image on upper part so bottom is grey.
Remove
im.setAdjustViewBounds(true);
It adjusts bounds to preserve the aspect ratio the drawable are you displaying.

Android - Match parent height, adjust width to maintain aspect ratio in horizontal scroll view?

I have a horizontal scroll view that contains an ImageView followed by a horizontal LinearLayout (with child views). When I receive the ImageView from an API, I want it to be able to match parent in terms of height, then expand in terms of width to maintain aspect ratio. How should I configure the ImageView to make this happen?
Thanks!
Have you tried to simply specify match_parent for height and wrap_content for width? The parent with the same values.
Set the height of the ImageView to match_parent so it matches the height of the parent. Then just calculate the width based on the aspect ratio you're trying to achieve, e.g. -- 16:9 = height/0.5625. -- and set the width to that.
EDIT:
To get the target height, then calculate the target width based on that value, you would first get the height of the parent layout, then calculate the width, then set the LayoutParams of your ImageView, then add the view. Something like this...
RelativeLayout parentLayout = (RelativeLayout) findViewById(R.id.parent_layout);
int height = parentLayout.getHeight();
int targetWidth = (int)Math.round(height / 0.5625); //example for 16:9 ratio
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new ViewGroup.LayoutParams(
targetWidth,
ViewGroup.LayoutParams.MATCH_PARENT
));
parentLayout.addView(imageView);

In Android, FIT_XY crops my image slightly

I have a (evoluted version of a) circle in a png, with a big border, and when displayed in an ImageView with FIT_XY it it cropped and appears a little squared, any idea ?
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new LayoutParams(80, 80));
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.myimg);
img.setLayoutParams(new LayoutParams(80, 80));
img.setScaleType(ImageView.ScaleType.FIT_CENTER); // tried FIT_XY too
img.setAdjustViewBounds(true);
layout.addView(img);
Try to use fixed size (specify directly height and width) and fit_center scale type or try to use adjustviewbounds attribute.
Edit:
Try to use fixed size (specify directly height and width) and fit_center scale type or try to use adjustviewbounds attribute. (OP indicated this did not work in their case)
Try adding a layout_margin to the image. If the image content fits tight to the image bounds, borders and such on the parent could get in the way.
Make sure that the LayoutParams match the parent Layout (in this case RelativeLayout.LayoutParams). Not sure if this is an issue, but I have been bitten by this in the past.
RelativeLayout layout = (RelativeLayout)findViewById(R.id.test); // <-- Parent
ImageView img = new ImageView(this);
img.setImageResource(R.drawable.cow);
img.setLayoutParams(new RelativeLayout.LayoutParams(400, 400)); // <-- Use Parent layout type
img.setScaleType(ImageView.ScaleType.FIT_CENTER);
layout.addView(img);
new RelativeLayout.LayoutParams(400, 400) uses pixels and not dip. If you are actually wanting your layout sizing to be in dip see this post: Using layoutparams in relativeLayout with dp

create dyanamic rule

I am trying to dynamically resize an image which is in the relative layout using the code
int height = v.getHeight();
int width = v.getWidth();
height += 50;
width += 50;
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(height, width);
layout.setMargins(200, 200, 200, 200);
layout.addRule(RelativeLayout.CENTER_IN_PARENT);
v.setLayoutParams(layout);
Here v is a view (a imageView in this case)
The rule works perfectly. The image gets placed in the center but what i want to get is the image should be placed where it is.
That is if the image is at the location (100,100) it's size should be increased and the image should be placed at the same location or the location where i need to place it.
Can anyone suggest me please.
What you are asking, can't be done simply by creating rules.
You need to do the extra work for such alignment.
1) use addRule to align parent left & align parent top, then set the left and top margin to align the image to any position you want 100,100 or 200, 50 etc. after that you can resize the image without changing position.
2) if you keep the image aligned CENTER_IN_PARENT then the image resize is a bit typical.
you need to increase the left and top padding according to the increased size to align the image in center.
suppose image size is 100x100 and new size is 150x150
now you need to set layout parameters to width x height to 200x200 and set the left and top padding of image to 50.
I hope its clear

Categories

Resources