I have an image, where the width is bigger than height. I want to fit the image in height with cropping the width. It should save ascpectRatio. The image should fit the image view.
I tried using android:scaleType="centerCrop"properties in XML but i did not get the same.
I tried
android:scaleType="fitXY"
android:adjustViewBounds="true"
Could you help me, please?
Below is the XML file.
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="riskmatix.be.bouwcampusvirtualtour.home.HomeActivity">
<ImageView
android:id="#+id/background_image"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/day"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
try this,
you need to set the width and height to match_parent
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ati"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
Related
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
I have an imageview in Linearlayout I want its width to be fill_parent. I want its height to be whatever the width ends up being. For example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:scaleType="centerCrop"
android:layout_margin="2dp"
tools:ignore="Suspicious0dp" />
</LinearLayout>
But it doesn't show the images in the android layout..
If I force change height of the imageview to 100dp in the code, it works, but I wonder why the height of image doesn't match to width even I wrote constraintDimensionRatio 1:1..
Thank you
You need to use ConstraintLayout as parent and set the dimension you want to make primary and another one to 0dp. app:layout_constraintDimensionRatio="H,1:1" means change height and make it 1:1 with width.
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try adding weight to your imageview then you will get the height, otherwise you have to user wrap_content as height.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_margin="2dp"
tools:ignore="Suspicious0dp" />
</LinearLayout>
<!--or if you don't want to use weight then user height = "wrap_content"-->
When we have a recyclerView with images i want to have the image (from which i dont know the dimentions of it) to have a max width and a max height as a percentage of the screen
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img_rcv_msg_image"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#+id/img_guideline2"
app:layout_constraintHorizontal_bias="0.0"
android:scaleType="centerInside"
android:background="#drawable/image_received_placeholder"
app:layout_constraintStart_toStartOf="#+id/guideline6"
app:layout_constraintTop_toBottomOf="parent"
app:srcCompat="#android:drawable/ic_menu_report_image"
tools:ignore="ContentDescription" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/img_guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.75" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
So i have constrainted the width to be between the two guidelines on the horizontal direction. How can i do this in the vertical also?
try app:layout_constraintHeight_percent="0.2" or app:layout_constraintDimensionRatio="1:1"
In this case you can now the width, because of the guidelines, but you cant do the same for heigth because you cant know if the height of the screen will be proportional on every device.
So you want to keep the ratio between width and height, use:
app:layout_constraintDimensionRatio="1:1"
Play wity the 1:1 proportion and then you will be able to adjust it and should scale to each screen size accordingly.
I have an image of size 519px X 420px that I want to place at the top of my android screen. Now there is some empty white space between my image and the header that I want to remove at all.
XML code:
<ImageView
android:src="#drawable/sitemakers_laptops"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Please see above screenshot and help me update my XML in such a way so that empty space between blue header and the image get completely vanished.
Thanks in advance!
Complete XML code as requested:
<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"
tools:context="in.sitemakers.sitemakers.AboutFragment">
<ImageView
android:src="#drawable/sitemakers_laptops"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="wrap_content" />
</FrameLayout>
Try this ImageView.ScaleType
Options for scaling the bounds of an image to the bounds of this view.
Use android:scaleType="fitXY"
Scale the image using FILL.
<ImageView
android:src="#drawable/sitemakers_laptops"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="420dp" />
You can use android:scaleType="fitXY"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:src="#drawable/sitemakers_laptops"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="wrap_content" />
</LinearLayout>
Updated XML code for posting image just after header. We have to give static height of the image along with android scaleType.
<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"
tools:context="in.sitemakers.sitemakers.AboutFragment">
<ImageView
android:src="#drawable/sitemakers_laptops"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="420dp" />
</FrameLayout>
Try this ImageView.ScaleType
Options for scaling the bounds of an image to the bounds of this view.
use android:scaleType="fitXY"
<ImageView
android:src="#drawable/sitemakers_laptops"
android:layout_width="match_parent"
android:scaleType="fitXY"
android:layout_height="wrap_content" />
So I got a problem scaling my image to fit my home screen appropriately.
Here are some screenshots.
Height problem
Width problem
I want to achieve something like this
XML Item code
<?xml version="1.0" encoding="utf-8"?>
<com.inthessaloniki.cityguide.view.SelectorRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_poi_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#drawable/selector_clickable_item_bg_inverse">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</RelativeLayout>
</com.inthessaloniki.cityguide.view.SelectorRelativeLayout>
I want the images to match the parent width but scale their heigth accordingly.
The scaleType="fitXY" forces image to fix to screen and does not keep aspect ratio.
Try this instead
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
In the end I found out a solution.
Here it is in case someone had the same problem.
I used scaleY and padding to fit the images accordingly.
<ImageView
android:id="#+id/fragment_poi_list_item_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="7dp"
android:paddingBottom="7dp"
android:scaleType="fitXY"
android:scaleY="1.25"/>