Scale image in RecycleView - android

I want to scale down the images I show in a RecyclerView via Picasso, this is how the layout looks like:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#+id/header_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a90180"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp">
<TextView
android:id="#+id/header_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="22sp"
android:gravity="center_horizontal" />
</FrameLayout>
<ImageView
android:id="#+id/model_image_iv"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="centerCrop"
android:layout_marginTop="5dp"/>
</RelativeLayout>
and this is how I load the image in the adapter:
Picasso.with(parentActivity)
.load(BaseApplication.getImageBaseUrl()+image)
.into(holder.imageView);
The image is shown but it doesn't keep the aspect ratio.
I've tried removing scaleType="centerCrop" and using adjustViewBounds="true" instead, with the same result.
Then I used Picasso's .fit().centerCrop() and .resize(x,y).centerCrop() as suggested in this and this SO posts with no luck either.
Does anyone have a hint on this?

Try using this instead of ImageView in your layout.it might work.

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

ImageView with spacing even with `wrap_content`

Can someone help me? My ImageView gets huge spacing for no apparent reason. Xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragment_container_3"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_marginTop="50dp"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="6dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardElevation="10dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginBottom="0dp"
android:padding="0dp"
android:src="#drawable/list_cli2"
android:contentDescription="123" />
<TextView
android:id="#+id/totalCli"
android:layout_width="300dp"
android:paddingBottom="10dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textColor="#fff"
android:layout_marginTop="10dp"
android:text="Total de Clientes: Buscando..."
android:textAllCaps="false"
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<com.github.mikephil.charting.charts.PieChart
android:id="#+id/cliChar"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
...
</RelativeLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|right"
android:layout_marginRight="18dp"
android:layout_marginBottom="60dp"
android:clickable="true"
app:backgroundTint="#android:color/background_dark"
app:fabSize="normal"
app:srcCompat="#drawable/refresh"/>
</FrameLayout>
Stays like this:
If I set the android: layout_height = "" with a low value, type 200dp, the spacing goes away, but in this case, on smaller devices, the view will be out of date. Already tried:
Placing the image as the background of a LinearLayout but the spacing was both above and below.
Placing the ImageView inside the LinearLayout however has done the same thing as above.
Does anyone know how to solve it?
If I put it outside the ScrollView, ImageView is centered in the middle of the screen
Add this to your imageview:
android:scaleType="centerCrop"
The scale type property will tell how the image should fit within the available space. Center crop will make it fill the whole space, but as a trade off the image will seem zoomed. There are other scale types you can try here.
It looks like your list_cli2 drawable already has the white space. Check it by open it with the image editor and see if the white space is there.

white border depending of size of the image

I want to make a ImageView of specyfic height (1/3 of the screen) with white border and the image will be resize according to the height (to keep the proportion) for example. It will be perfect if also I will be able to add TextView below the image. I want to obtain smth like that :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#color/backgroundContent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/white"
android:gravity="center"
>
<ImageView
android:contentDescription="#string/pic"
android:id="#+id/pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:padding="5dp"/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressPic"
android:layout_centerInParent="true"
android:visibility="gone"/>
</RelativeLayout>
edit: Im downloading images form url using Glide library if that matter

How to get rid of blank spaces around an ImageView inside a CardView

Below screenshot, I am inserting an image, using ImageView, into a CardView.
The problem is, I seem can't get rid of the empty space below the Image. (highlighted by light blue)
I tried a solution that works, but it requires me to explicitly specify the image height, which I try not to, because, the source can be various, and I need the image to flexibly scale up/down (with same aspect ratio) depending on the card width.
Here's the screenshot:
And here's the code snippet:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="7dp">
<TextView
android:id="#+id/caption_text"
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:scaleType="fitStart"
android:layout_below="#id/caption_text"
android:src="#drawable/food"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
So, is there any way to make the CardView automatically wrap around the image, (without leaving any empty space) and without specifying the image height explicitly?
Change the <RelativeLayout> height to wrap_content. Currently, it has a circular dependency in height.
[EDIT]
We need to set the property android:adjustViewBounds="true" for the image to scale with the source. Its false by default!
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="7dp">
<TextView
android:id="#+id/caption_text"
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:adjustViewBounds="true"
android:layout_below="#id/caption_text"
android:src="#drawable/food"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Try changing the scaleType to android:scaleType="centerCrop", or some other scaleType.
See here:
https://developer.android.com/reference/android/widget/ImageView.ScaleType.html
I am using RoundedImageView plugin
<com.makeramen.roundedimageview.RoundedImageView
....
....
android:adjustViewBounds="true"/>
This worked for me.

Android - Layout not in uniform

I have a horizontal list of images in my android application. But the problem is some of the images seems to not follow the layout I've defined in my xml. Like this:
my layouts look like this:
mainlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back_shop_list"
android:orientation="horizontal">
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/shop_hlist_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:scrollX="1dp"
android:scrollY="1dp"
android:overScrollMode="always" >
</com.devsmart.android.ui.HorizontalListView>
</LinearLayout>
then my listlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:translationY="200dp" >
<FrameLayout
android:id="#+id/framelayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp" >
<ImageView
android:id="#+id/img"
android:clickable="true"
android:adjustViewBounds="true"
android:layout_width="360dp"
android:layout_height="360dp" />
<ProgressBar
android:id="#+id/img_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
android:translationY="150dp"
android:translationX="140dp" />
</FrameLayout>
<TextView
android:id="#+id/labelText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:translationX="10dp"
android:textColor="#dc6800"
android:textSize="16sp"
android:textStyle="bold" />
<!-- Description label -->
<TextView
android:id="#+id/desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:translationX="10dp"
android:textColor="#acacac"
android:paddingBottom="2dip">
</TextView>
</LinearLayout>
My guess is that the size of my image is lesser than what my ImageView layout_width and height. Or I dont really know what causes this problem.
But any help will do, please. Thanks.
EDIT: What I really wanted is that if the image is like image 4 and image 5, it should not have those spaces. I've tried getting the size of the image from my ImageLoader then set the layout parameters height and width in there while images are loaded. But it looked worse.
Are there other ways in which i can dynamically adjust my ImageView if ever the images are like image 4 and 5?
If actually you want to make the image occupy the desired space
try adding this image View property to your ImageView
You can try others I usually use fitXY out of the predefined list you will get there in XML Layout
android:scaleType="fitXY"
so as to make this property work we also need
android:adjustViewBounds="true"
that you have already added in your case
If actually you want to avoid on blank spaces and let the image occupy as much space as it want
In my layout I am using two image views to show a demo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_numbers"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#+id/tv_numbers"
android:layout_below="#+id/tv_numbers"
android:layout_marginTop="5dip"
android:layout_marginRight="10dip"
android:orientation="vertical">
<ImageView
android:id="#+id/textView1"
android:layout_width="200dip"
android:layout_height="200dip"
android:src="#drawable/desert"/>
<ImageView
android:id="#+id/textView2"
android:layout_width="200dip"
android:layout_height="200dip"
android:background="#drawable/desert"/>
</LinearLayout>
ImageView 1 is assigning image as Source using android:src="#drawable/desert"
and below is the result as you can notice that it is showing WhiteSpace arround it.
ImageView2 is assigning image as Background using android:background="#drawable/desert"
and below is the result as you can notice that it is showing no WhiteSpace arround it.
Or try using these attributes to make it possible
android:maxHeight=""
android:maxWidth=""
android:minHeight=""
android:minWidth=""

Categories

Resources