I am trying to set an Image that I am downloading from the web. I am using a library to help me do this called ion. The problem is that I need to resize the image to fit my screen and keep the aspect ration. The height seems to be working fine however I want to set the width of the bitmap to fit the whole width of the devicewhile keeping the aspect ratio. But it is not working and the image is cut short on both sides...
<ImageView
android:id="#+id/imageView"
android:layout_width="fill_parent"
android:layout_height="#dimen/_height" />
Code:
DisplayMetrics metrics = getResources().getDisplayMetrics();
mWidth = metrics.widthPixels;
mHieght = (int) getResources().getDimension(R.dimen._height);
Ion.with(imageView).resize(mWidth, mHieght).load(url)
Edit:
This is with android:scaleType="centerCrop"
ImageView has scale type feature. Check ImageView.ScaleType. I think centerInside or fitCenter is what you are looking for.
Update
Sorry didn't realize you have a fixed height at first. centerCrop should be the answer.
If you have a known max size for your images, you can set the width of your layout equal to the largest width of your images set, then take all centered. This worked for me:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="1920px"
android:layout_height="fill_parent"
android:layout_gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/your_image" />
</RelativeLayout>
</FrameLayout>
Where 1920px is the largest width of your images set.
The result is acceptable, EDIT: Image is perfectly centered.
I achieved this using "paddingLeft" and "paddingRight" on parent layout; while apply a negative value to padding and margins to the child layout / ImageView. "android:clipToPadding" flag must be false to prevent the clipping.
<LinearLayout
...
android:paddingLeft="10sp"
android:paddingRight="10sp"
android:clipToPadding="false"
...>
<ImageView
android:layout_marginLeft="-10sp"
android:layout_marginRight="-10sp"
android:paddingRight="-10sp"
android:paddingLeft="-10sp"
...>
Related
I have an ImageView in GridLayout that is showing an image loaded from Url. GridLayout has 2 columns and it is all stretched to the entire screen. I need an entire image to be visible without any changes to the aspect ratio. Since the screen width can vary on many devices, I need the height to be calculated from the width of ImageView (which needs to fill the GridLayout) and from the aspect ratio of the downloaded image.
My idea was to set the width of ImageView to match_parent and height to wrap_content and then tell Glide that I wanna the height to be calculated to fit the aspect ratio of the image, but I am struggling to do that.
So far I have this in the layout:
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter" />
And this in adapter:
Glide.with(viewHolder.imageView.getContext())
.load(item.getImageUrl())
.apply(new RequestOptions().error(R.color.black_one))
.into(viewHolder.imageView);
I found the solution in this article. Using a ConstraintLayout there is an option to use app:layout_constraintDimensionRatio parameter. You need to set one of the dimensions to 0dp and then you just choose whatever ratio you want and that is it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/top_image"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
I belive what you are looking for is to load images like instagram feed does, dynamic height with fixed width, without cropping the content. Then set the height to wrap content, width to match parent and set adjustViewBounds to true.
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:minHeight="200dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
The android:adjustViewBounds parameter does the magic of setting the imageview height dynamically.
I would like to fix the height of imageview but according to different devices and dpi's.
<ImageView
android:id="#+id/imgFood"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/ic_no_img" />
As of now I fixed the height to 200dp, but I want to keep it ratio of 1:2 instead of fix size. I know android works on dp but I am unable to figure out how can i keep the ratio for all devices. Width will be match parent (fit device width) but height needs to be changed accordingly.
Any idea/suggestion?
You can set your height dynamically like this.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
ll.getLayoutParams().height = width/2;
ll.getLayoutParams().width = width;
ll.requestLayout();
You can set height or width ratio of views inside LinearLayout using the layout_weight property, for example:
<LinearLayout
android:weightSum="3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<View
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<View
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
This will cause the inner Views of the LinearLayout to take one and two thirds of its width, respectively.
Otherwise, if you have to use a different layout, there is currently no way to do this in .xml files, and the option is to fall back to setting the size programmatically, like in Hussnain Azam's answer (you don't always have to call for size of the display though).
I am trying to resize an image by keeping the aspect ratio. It should be just large enough to fill the screen width and if necessary some of the image should be off-screen.
here is a picture of what I want
I tried
android:adjustViewBounds
but when the image is larger than screen it fits image into imageView and not filling the width.
And I also don't want to use :
android:scaleType="centerCrop"
Here is my complete code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageBackgroundAboutFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/about_us_background"/>
</RelativeLayout>
Use android:scaleType="fitXY" with android:adjustViewBounds
UPDATE
I think you should do some trial and errors :
How to scale an Image in ImageView to keep the aspect ratio
Scale Image to fill ImageView width and keep aspect ratio
So many possible fix, but i dont really sure which one will work for you (my answer working for me but not for you - for example).
updtae your imageview like this..
remove scaleType and adjustViewBounds.. and make layout_height as match_parent
<ImageView
android:id="#+id/imageBackgroundAboutFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/about_us_background"/>
I am using this ImageLoader Library for my ListView. It by default decodes the file to a 70px bitmap. But then I changed it to 1000, and the image I get is proper. The only problem is displaying the image in the ImageView.
The ImageView in the ListItem is like this:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerInside" />
This displays an image which is centered in the ImageView, with proper aspect ration(no stretching etc..) and has empty spaces on all sides. The empty spaces might be because of the lower dimensions of the image.
I want the image to take complete width of the screen, and maintain the aspect ration. How do I get it?
I tried:
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
But then the image is stretched horizontally and completely out of aspect ration. Looks like its taking the height same as the actual image height but width as fill_parent.
Thank You
Try this:
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/name"
/>
Make android:layout_width="match_parent".
Set height to some arbit but legit value to begin with like android:layout_height="0dp".
And for height, since there is no appropriate scaleType to get the aspect ratio the way you desire (i believe - confirm that developer.android.com/reference/android/widget/…), what you will have to do is in the java file, get the image's width and then set the height according to aspect ratio you need (0.6 in your case).
Try adding this
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:adjustViewBounds="true"
/>
I defined two ImageViews and set the background like this:
int tmpID = findViewById(R.id.mypng)
ImageView tmpIV.setBackgroundResource(tmpID);
And this is the xml with the Image views:
<RelativeLayout
android:id="#+id/myDisplay"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp" >
<ImageView
android:id="#+id/b1"
android:layout_width="300dp"
android:layout_height="match_parent"
android:contentDescription="#string/anyStringValue"
android:scaleType="fitCenter" />
<ImageView
android:id="#+id/b2"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/b1"
android:contentDescription="#string/anyStringValue"
android:scaleType="fitCenter" />
Now all that should happen is that when I set the background of the imageviews, the png adjusts uniformly its size to the parents 100dp height while the width should scale uniformly - down or up.
I tried to set the width smaller and larger. When its set to 300dp for example then it stretches the png width to 300 and 100 height while it should only stretch it's width so far that the height reaches 100.
I thought "fitCenter" would do that. I also tried all the other attribute values, but without luck.
Thanks!
android:scaleType only take care about the android:src why don't you use that param?? do you really want use background and don't set a source?
Maybe yoy can have one RelativeLayout with two ImageView for each imageView, one with the "background scaled" and the der with the real source.