How to properly scale an imageview within parent layout? - android

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

Related

Programmatically change height in constraint layout?

I have a simple layout: a fixed height TextView with three ImageView objects anchored to the bottom of the view.
How can I programmatically change the height of each of the ImageViews so they vary within some range (10dp, 16dp below the TextView)? Concretely, my activity gets a list of percentages (between 0 and 1) representing how much of that vertical space each of those "bars" should take up.
I've tried the following without success:
// No visible difference
imgView.setMinHeight(newHeight);
// No change to height, but horizontal constrained was messed up
imgView.setLayoutParams(new LayoutParam(imgView.getWidth(), newHeight);
If by "height" you mean the absolute top-to-bottom dimension of the ImageView, you can do the following to double the height of an ImageView. You can set the height to the value that you want.
ImageView iv = (ImageView) findViewById(R.id.imageView);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) iv.getLayoutParams();
lp.height = lp.height * 2;
iv.setLayoutParams(lp);
See ConstraintLayout.LayoutParams.
But, if by "height" you mean the vertical position of the ImageView, you can do the following to change the ImageView's vertical bias (assuming that you are using ConstraintLaytout):
ConstraintSet cs = new ConstraintSet();
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);
cs.clone(layout);
cs.setVerticalBias(R.id.imageView, 0.25f);
cs.applyTo(layout);
See setVerticalBias.
There are other ways to move an ImageView about the layout, but which you use would depend on how your layout is set up.

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 ImageView: Replace Image (Drawable) While keeping View Size

In Short
I have an ImageView with size set to wrap_content.
The image has a drawable already set, and everything looks great.
Now, I want to change the drawable, while forcing the ImageView to keep it's size.
Explanation
Well, the easy answer is of course to set the width & height to a fixed size.
But in my case, I am copying an ImageView + layout from a Google Map to add my custom map button. It works well, because I have an icon with same size.
Now I want to change the icon for a moment (loading icon btw), but because the Drawable source is larger, it changes the ImageView size accordingly.
ScaleType doesn't work as the width and height are not fixed.
What I've Tried
1. Setting the new Drawable bounds using getBounds on the old one.
2. setRight() on the ImageView with the right bound of the existing map button (the original one which doesn't change).
(left is already fixed, it's inside a RelativeLayout).
Some (working) Code of how I created the new button
ImageView newBtn = new ImageView(context);
newBtn.setAdjustViewBounds(false);
newBtn.setBackgroundResource(R.drawable.map_btn_background);
newBtn.setScaleType(ScaleType.CENTER_INSIDE);
newBtn.setImageResource(imageResId);
newBtn.setClickable(true);
RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) locationBtn.getLayoutParams();
rlp = copyRelativeLayoutParams(rlp);
//add to Google's map button layout
btnContainer.addView(newBtn, rlp);
Some (not working) Code of how I change the drawable.
This is the point were I want the ImageView to keep it's size.
perfectlyMadeNewMapButtonImageView.setImageDrawable(mLoadingDrawable);
//mLoadingDrawable.setBounds(
// perfectlyMadeNewMapButtonImageView.getDrawable().getBounds());
// not working

ImageView CENTER_CROP dont work

Welcome all.
i have a LinearLayout that must show a ImageView. The imageView must fit all the width of the LinearLayout, so i used match_parent for the width, and it must keep the aspect ratio, so i used wrap_content for the height and setAdjustViewBounds to true. But something is not working because the width of the ImageView is correct (it fits the width of the linear layout) but the height of the imageView is wrong (the image is being cutted in the upper and the lower part)
I tryed with all the scale types (fit_center, fit_xy, center_crop, center_inside etc...) and i have problems with all of them (for example, if i use fit_center, the image is does not fit the full width of the ImageView, i can see two black spaces on left and right of the image, also i tried using FIT_CENTER without setAdjustViewBounds and i have the same problem)
HERE YOU HAVE AN SNAPSHOT: http://i.stack.imgur.com/8hrKF.png
this is my code:
LinearLayout onlineHolder = this.holders.get(i);
onlineHolder.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
onlineHolder.removeAllViews();
ImageView imgv = new ImageView(onlineHolder.getContext());
imgv.setLayoutParams(
new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
imgv.setAdjustViewBounds(true);
imgv.setScaleType(ImageView.ScaleType.CENTER_CROP);
Bitmap b=res.getCachedBitmapWithWidth(this.width); //obtenemos el recurso cacheado con el width correspondiente
imageElement.setBitmap(b);
imgv.setImageBitmap(b);
onlineHolder.addView(imgv);
Thanks
EDIT: it only works if i specify the width and the height of the imageview with absolute numbers. For example, if i put 400 (400 is the 50% of the screen, the layout width) width and 583 height, it works, but if i put MATCH_PARENT for width and WRAP CONTENT for height... it not works... why?
I have this one in XML and it works in the way you want. take a look.
<ImageView
android:id="#+id/attachedImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true" />
But if you want to do it on the runtime, I suggest to create an XML this way and inflate the View on the runTime and add it to your parent layout. That will work for sure.
EDIT : How to inflate the ImageView.
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.your_layout_contains_image_view, null, false);
ImageView imageView = view.findViewById(R.id.attachedImage);

Add ImageView in RelativeLayout with coordinates

How to add ImageView in RelativeLayout with some coordinates, which is bigger than RelativeLayout Size?
Trouble is, when I add ImageView with padding left bigger then layout width, and use in that layout scrollTo(-100,0) (for example), I can't see nothing, my ImageView disappeared, or be smaller(when padding left a little less than layout width).
How can I turn off ImageView auto-scaling or something?

Categories

Resources