ImageView CENTER_CROP dont work - android

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

Related

How to maintain aspect ratio of an Image inside an ImageView Android [duplicate]

This question already has answers here:
ImageView setScaleType not working
(2 answers)
Closed 3 years ago.
I have a LinearLayout in xml file and i created an ImageView in the java code. I had to create ImageView in java code instead of xml because I need to alternate an ImageView with a TextView in the same LinearLayout.
This is my LinearLayout in my xml file:
<LinearLayout
android:id="#+id/linearLay"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
And this is part of my java code:
public class MainActivity extends AppCompatActivity {
public ImageView questionImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLay);
questionImage = new ImageView(this);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
questionImage.setLayoutParams(layoutParams);
((LinearLayout) ll).addView(questionImage);
// imageview.setScaleType doesn't work
questionImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
questionImage.setBackgroundResource(R.drawable.imageA);
}
}
The result is that i can see the imageA fit in all my LyinearLayout but without keeping its aspect ratio.
The LinearLayout has been set with a MATCH_PARENT width and a height of 0dp due to its weight of 5 (i have others TextViews and Buttons displayed in the same activity).
What i would like to do is to display imageA in the center of my LinearLayout keeping its aspect ratio at maximum possible size(e.g. imageA = 250p x 250p, LinearLayout = 400p x 600p -> imageA resize to 400p x 400p and centered in the middle; e.g imageA = 250p x 250p, LinearLayout = 100p x 180p -> imageA resize to 100p x 100p and centered in the middle).
Why setScaleType doesn't work? Sholud i do in a different way? Could you please help me with some example?
Thanks in advance.
EDIT:
I resolved it by using questionImage.setImageResource(R.drawable.imageA);
If you want to keep the aspect ratio of your image, SET adjustViewBounds property to true of your ImageView.
You can set the property adjustViewBounds of the ImageView to true. Also, make sure that you set it as an src and not as background because setting it as background will make it scale and distort it.
setAdjustViewBounds
And in case of setting the src programmatically, you can use setImageDrawable

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.

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

Android, Change Image size in ImageView

For Android, How can I change the image size in an ImageView in a layout?
My image is jpeg format.
<ImageView
android:id="#+id/imageView1"
android:layout_margin="20dp"
android:src="#drawable/stop"
android:layout_width="50dp"
android:layout_height="50dp"/>
Just change width and height attribute.
You can also set Params using LayoutParams
ImageView yourImageView= (ImageView)findViewById(R.id.imageId);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
//width and height of your Image ,if it is inside Relative change the LinearLayout to RelativeLayout.
yourImageView.setLayoutParams(layoutParams);
There are a few ways to change the size of an image in an imageView in XML.
Scale the image by pulling around the edges of the imageView in the Graphical Layout (this is not really advised since the Graphical Layout sometimes adds undesired properties into the XML).
Apply a weight to the imageView (this requires the parent object to have specified a weightSum), causing the image to scale according to the screen size. This seems to be the most reliable way to gauge how your layout will look on many different sized screens.
Simply adjust the weight and height in the imageView (as others have mentioned).
<ImageView
android:id="#+id/imageViewId"
android:src="#drawable/icon"
android:layout_width="xxdp"
android:layout_height="yydp"/>
where xx is the width in pixels and yy is the height in pixels.
(give integer values as you desire the size to be)

Categories

Resources