In Android, FIT_XY crops my image slightly - android

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

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

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.

Android, match the picture with dimension of the screen

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);

Resize image to fit into LinearLayout

I need to dynamically add some images to a LinearLayout based on what the user selects but I cannot figure out how to resize the images before adding them. I have tried the setWidth() and setHeight() methods but it seems to do nothing. I would like to set them to a certain dp.
Thanks to anyone looking at this.
ImageView image = new ImageView(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.CENTER);
image.setImageResource(R.drawable.icon1);
LinearLayout layout = (LinearLayout) findViewById(R.id.iconLayout);
layout.addView(image, cart.size()-1, params);
problem:
ImageView.ScaleType.CENTER
What it is doing is that it will scale your image but it will only fit on either x or y axis.
from documentation:
Compute a scale that will maintain the original src aspect ratio,
but will also ensure that src fits entirely inside dst. At least
one axis (X or Y) will fit exactly. The result is centered inside dst.
solution:
You can use the ImageView.ScaleType.FILL to scale your image in x and y axis depends on the height and width of your ImageView.
image.setScaleType(ImageView.ScaleType.FILL);
OK. I figured it out thanks to being steered in the correct direction. I was able to use FIT_CENTER and that done it.

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);

Categories

Resources