android imageView scale - android

I have an ImageView which is populated from database (the image comes from database). in the database i have images both portrait and landscape!! how can i adjust the imageView to scale it self according to the image width and height. I have tried many ways but no result! any help please!!!!
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/slideshow"
android:orientation="horizontal"
android:padding="0dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginRight="5dp"
android:id="#+id/slide1"
android:src="#drawable/ddd"
/>

Try this code. adjustViewBounds attribute makes the ImageView the same size as image that you put in it.
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
If you need specific width or height change the wrap_content value.

Related

How to fit image into the imageview perfectly without losing image quality android

Im trying to fit images perfectly in the imageView but the image dosent fit properly when i use centerCrop with adjustViewBounds but when i use fitXY with adjustViewBounds the image fits perfectly to the imageView but now the image quality gets worst, My imageView height and width are match_parent and yes i also used fitCenter but didnt worked
Screenshot//currently im using centerCrop with adjustViewBounds
Here is my code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
android:weightSum="5">
<com.google.android.material.card.MaterialCardView
android:id="#+id/Card_View"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_weight="4"
app:shapeAppearanceOverlay="#style/RoundedCornerHome">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/imagePostHome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:contentDescription="#string/todo"
app:shapeAppearanceOverlay="#style/RoundedCornerHome" />
<include
android:id="#+id/imagepost_buttons"
layout="#layout/imagepost_buttons" />
</FrameLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="5dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:background="#color/grey"
app:cardBackgroundColor="#color/grey"
app:shapeAppearanceOverlay="#style/RoundedCornerHome">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/material_cardview_snipet"
layout="#layout/material_cardview_snipet" />
</FrameLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>
for those who want to know what is layout material_cardview_snipet ,here is the code of it
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginTop="5dp"
android:layout_marginEnd="25dp"
android:src="#drawable/ic_baseline_attach_money_24"
tools:ignore="RtlHardcoded"
android:contentDescription="#string/todo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Ian Somerhalder"
android:textColor="#color/white"
android:layout_marginTop="5dp"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="SmallSp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
tools:ignore="RtlHardcoded"
android:textSize="17sp"
android:layout_marginLeft="10dp"
android:textColor="#color/white"
android:text=".............."/>
</FrameLayout>
If you're image size I mean height and width ratio don't match with your image view height and width ratio glide will fit the image to center of your image view and a part of the image cuts if the ratios doesn't match.
If you force images to fit the image view even the ratios doesn't matches it will be compressed to fit either in x-axis or in y-axis. So your image don't look as original.
So use your image view attributes like this
android:layout_width="match_parent"
android:layout_height="wrap_content"
Here width is fixed and hight will be calculated according to the image height.
You can resize your image in firebase, No need to code for it
https://firebase.google.com/products/extensions/storage-resize-images
You can use this cool extension of firebase. it Resizes images uploaded to Cloud Storage to a specified size, and optionally keeps or deletes the original image.
it Requires a blaze plan, for that use the trial version of it or if you are a student you can use your .edu id and get it from google.
If you are fetching images from a server or even from drawable use the glide library to set the image into an image view.
Google also recommends the Glide library to deal with images in android.
It's very easy to understand quickly. See Doc

Prevent Image from scaling even though the imageView height = "match_parent"

I have this linear layout,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:background="#color/white"
android:layout_marginTop="1dp"
android:weightSum="1" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:gravity="left|center"
android:padding="5dp"
android:text="Policy No."
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.2"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Now i want the image to not scale but the ImageView to match the height of the parent, the TextView height is set to wrap content and that cant be changed. Also, i can't set the imageView height as wrap_content, since it'll change the height of the overall Linear Layout and i can't afford that.
![Image of problem]http://imgur.com/gaef828
Now what i want is to have the image's size remain the same as it is the second row, even though the first row's height has increased.
Thanks in advance, any help would be appreciated.
add :
android:scaleType="centerInside"
to your ImageView
To perform no scaling, use
android:scaleType="center"
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
To get what you want, you can do this:
First give your imageviews same fixed height so they won't scale:
android:layout_height="25dp"
Than put gravity to center_vertical:
android:layout_gravity="center_vertical"
So now you have for each imageview:
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight="0.2"
android:src="#drawable/ic_launcher"
android:layout_gravity="center_vertical" />
It now no longer matters how may lines your textview has, your imageview will stay the same!
Ouput:
Hope this will help you with your problem.

LinearLayout above ImageView not getting displayin Fragment?

I have ImageView which needs to be shown in full height available. However at top i have created LinearLayout which contains Text and Image. I want it to display at top but also want to image to fit screen. I have linear layout at bottom and its working fine.
This is my code for LinearLayout which i want to show at top. I have hidden actionbar
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/black"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ImageView
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:src="#drawable/back_button" />
<TextView
android:id="#+id/titleofscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing"
android:textColor="#22c064"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageShown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</FrameLayout>
If i dont set imageView scale type to android:scaleType="fitXY" then it works but image doesnt fit screen.
I want to display image to fit between above bar which contains Text and below it has some icons. But if i make image scaletype ='fitXY' it doesnt display above text. Following is screenshot but i want image to occupy space
If you want the image to be below the actionbar you should not use the FrameLayout as a parent. You either should use vertical LinearLayout with weight or `RelativeLayout". Try following code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/actionbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#android:color/black"
android:gravity="top"
android:orientation="horizontal"
android:visibility="visible" >
<ImageView
android:id="#+id/backbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:paddingLeft="10dp"
android:src="#drawable/back_button" />
<TextView
android:id="#+id/titleofscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Testing"
android:textColor="#22c064"
android:textSize="18sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageShown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/actionbar"
android:layout_alignParentBottom="true"
android:scaleType="fitCenter"
/>
</RelativeLayout>
Also I doubt that you use fitXY as android:scaleType, because it doesn't maintain the aspect ratio of the image - so your image will be deformed (stretched). In your case you should rather use fitCenter, but it depends on how exactly you want to scale your image - you can also play with centerInside or centerCrop.

Android: ImageView scaleType, fitXy Doesn't Work

I used the below code, in the layout. I used the scale type property and set it to "fitXy", to stretch the src image to fill the ImageView. But it doesn't work and the source image is not stretched to fill the full imageview size. I'm an absolute beginner in android and please help me with this.
Below, is the code i used.
<FrameLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp" >
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/blue_calander"
android:contentDescription="#string/cd_new_exam"
android:scaleType="fitXY"/>
<!-- some more code -->
</FrameLayout>
Thank you in advance.
Update your layout like this
<FrameLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#ff00" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="#string/cd_new_exam"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
</FrameLayout>
You had given margin
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"

Set android background to center crop, but right aligned?

I have a background image that I would like to 1) fill the vertical space, regardless of whether the image's width is greater than the width of the parent, and 2) align the right edge of the image with the right edge of the parent.
Currently scaleType="centerCrop" is giving me (1) but not (2); it centers the image horizontally. Does anyone know how to get it to right-align? Thanks!
EDIT: Tried adding android:layout_alignParentRight="true" to both the relative layout and image view, with no effect.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_gravity="center" >
<ImageView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<LinearLayout
android:id="#+id/fg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
...
You can use
android:layout_alignParentRight="true"
for the Imageview
add these attributes
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
<ImageView
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/backg_carbon" >
</ImageView>
I think your issue is that the viewbounds do not adjust to the size of the actual image and fill up the width for some reason. Best thing is to check if setting
android:adjustViewBounds to true helps (with the gravity to right of course).
<ImageView android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true" //This is the added line.
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>

Categories

Resources