I have a chat application like whatsapp. I want to show several images that i have in the internal storage. So the only thing that i know for that image is the path that i can find it.
However, in the xml layout of the image_item i must set the width and height of the imageView.
<ImageView
android:id="#+id/rcvImage"
android:layout_width="280dp"
android:layout_height="280dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="32dp"
android:adjustViewBounds="true"
android:maxWidth="320dp"
android:maxHeight="320dp"
android:minWidth="150dp"
android:minHeight="150dp"
android:scaleType="centerCrop"
android:src="#drawable/default_img"
tools:ignore="ContentDescription" />
How can i set the values so i can have portrait and landscape images, but without making lags in the recyclerview scrolling
You can set wrap_content and also adjustViewBounds
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/test"/>
Also, scaleType can help with the image resize
android:scaleType="fitStart"
https://developer.android.com/reference/android/widget/ImageView.ScaleType
Related
I am developing a chat Android application. Inside the chatRecyclerView i want to show Images along with all other views. However, since i dont know the size of each image i cannot pre-define inside the xml the width-height of the img. If i define those with wrap_content, wrap_content, this makes the recycler scroll without smoothness and also the image to show very big or very small.
I came up with a solution like the below.
<ImageView
android:id="#+id/img_rcv_msg_image_landscape"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="32dp"
android:adjustViewBounds="true"
android:maxWidth="320dp"
android:maxHeight="320dp"
android:minWidth="150dp"
android:minHeight="150dp"
android:scaleType="centerCrop"
android:src="#drawable/error_image"
app:layout_constraintBottom_toBottomOf="#+id/img_frame"
app:layout_constraintEnd_toEndOf="#+id/img_frame"
app:layout_constraintStart_toStartOf="#+id/img_frame"
app:layout_constraintTop_toBottomOf="#+id/img_rcvName"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/img_rcv_msg_image_portrait"
android:layout_width="wrap_content"
android:layout_height="280dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="32dp"
android:adjustViewBounds="true"
android:maxWidth="320dp"
android:maxHeight="320dp"
android:minWidth="150dp"
android:minHeight="150dp"
android:scaleType="centerCrop"
android:src="#drawable/error_image"
app:layout_constraintBottom_toBottomOf="#+id/img_frame"
app:layout_constraintEnd_toEndOf="#+id/img_frame"
app:layout_constraintStart_toStartOf="#+id/img_frame"
app:layout_constraintTop_toBottomOf="#+id/img_rcvName"
tools:ignore="ContentDescription" />
I have 2 imageViews. One for the LandScape and one for the Portrait mode. The only data that i have in the ViewHolder is the File of the Image and i dont know which one is the proper image to show.
val file = messageItem.messageItemToFile(itemView.context, it) ?: return#let
//Hide both
imageViewLand.visibility = View.GONE
imageViewPortrait.visibility = View.GONE
Glide
.with(itemView.context)
.load(file)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(
if (file_width > file_height) imageViewLand else imgeViewPortrait)
)
Is there a better solution to come up with??
I've an imageview with an icon on background, instead of generating all diferent sizes of icons for diferent sizes of screen i'm using only a big one and resizing it to fit the size I want.
If I set the layout_height to any value android automatically resize the width to keep aspect ratio (what is good) but the placeholder keeps the original width and the new resized icon is centered in the space it was supposed to fit
To demonstrate the situation i took this print
both left and right icons have the same dimension in original file
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:foregroundGravity="left"
android:src="#drawable/ic_left_thumb" />
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_right_thumb" />
setting only the imageview height and is it possible to make it resizes keeping gravity on left? (as in the print bellow)
It is quite simple.
Set your parent layout height as desired and its children height to "match_parent"
You can use "dimen.xml" files to control different sized screen dimensions, for now i have put a static value of "64dp" android:layout_height="64dp"
<RelativeLayout
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="64dp">
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_left_thumb" />
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:src="#drawable/ic_right_thumb" />
</RelativeLayout>
The answer was pretty easy... there is an property on ImageView that tells android how to deal when it needs to scale the image:
android:scaleType
so setting it properly i've
<ImageView
android:id="#+id/leftThumb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:src="#drawable/ic_left_thumb"
/>
<ImageView
android:id="#+id/rightThumb"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitEnd"
android:layout_alignParentEnd="true"
android:src="#drawable/ic_right_thumb" />
In this way, instead of fitting the icon at the center of the spaceholder, it's is going to adjust to the edges
I am trying to create a custom top view using anImageView where buttons stays always at the same position and scale in proportion of the resolution of the device. I have been trying to put my elements inside a RelativeLayout using margins without any success the button always move from it position and its not scaling.
How could achieve this?
This is the example I am trying to get for all resolutions:
and here my XML:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:srcCompat="#drawable/settings_red_btn"
android:id="#+id/settings_btn"
android:contentDescription="settings btn"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="9dp"
android:layout_marginTop="11dp"
android:layout_width="43dp"
android:layout_height="43dp" />
<ImageView
android:layout_width="match_parent"
app:srcCompat="#drawable/top"
android:id="#+id/imageView17"
android:scaleType="fitCenter"
android:layout_height="wrap_content"
android:cropToPadding="false"
android:adjustViewBounds="true" />
</RelativeLayout>
You have 2 options here :
You need to have the same padding right in the images themselves resources settings_red_btn & top. That way, you set to them alignParentRight = true, and they will align with the same padding.
You need to specify padding right for imageView17, according to screen resolutions. This is done by defining dimens.xml file on all resolutions. There are many blogs and stackoverflow hits on this. You can start with : http://android4beginners.com/2013/07/appendix-c-everything-about-sizes-and-dimensions-in-android/
that is my problem in relative-layout:
i have some image views that some part of each imageview is out of
display with negative margin.
i design this layout in a display dimension that shown in left side
and that is OK.
but in other display demensions some parts of views that i dont want
to be shown will be shown [like right image]. (or in smaller
displays some parts not shown).
so. i want just wanted box of layout will be visible in different
display sizes and be scaled according to display dimensions.
Sorry i know the problem is involved ;)
i need just visible part of layout scaled in various display sizes, also want views be separate to animate them.
that is one arrows sample code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="4000dp"
android:layout_height="300dp"
android:rotation="15"
android:src="#drawable/rectangle"
android:layout_row="0"
android:layout_column="0"
android:id="#+id/img_rct_header"
android:layout_marginBottom="108dp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_marginLeft="-100dp"
android:layout_marginTop="-160dp"
android:layout_marginRight="-120dp"
android:clipChildren="false"
android:scaleType="fitXY" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="135dp"
android:rotation="195"
android:src="#drawable/arrow_tall"
android:id="#+id/img_arw_in_sore"
android:scaleType="fitXY"
android:layout_marginTop="815dp"
android:clipChildren="false"
android:layout_marginRight="-8450dp"
android:layout_marginLeft="25dp"
android:visibility="gone" />
<ImageView
android:layout_width="500dp"
android:layout_height="150dp"
android:rotation="195"
android:src="#drawable/arrow_svg"
android:id="#+id/img_arw_in_tafsir"
android:scaleType="fitXY"
android:layout_marginBottom="-50dp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_marginLeft="161dp"
android:layout_marginTop="360dp"
android:layout_marginRight="-1150dp"
android:clipChildren="false"/>
<ImageView
android:layout_width="500dp"
android:layout_height="150dp"
android:rotation="195"
android:src="#drawable/arrow_svg"
android:scaleType="fitXY"
android:id="#+id/img_arw_out_tafsir"
android:layout_marginBottom="0dp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_marginLeft="-272dp"
android:layout_marginTop="240dp"
android:layout_marginRight="55dp"
android:clipChildren="false"/>
<ImageView
android:layout_width="4000dp"
android:layout_height="320dp"
android:rotation="15"
android:src="#drawable/rectangle"
android:layout_row="0"
android:layout_column="0"
android:id="#+id/img_rct_footer"
android:layout_marginBottom="-800dp"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_marginLeft="-100dp"
android:layout_marginTop="550dp"
android:layout_marginRight="-120dp"
android:clipChildren="false"
android:scaleType="fitXY" />
</RelativeLayout>
thanks for your help ;)
Your layout is pretty complex. If all you want is to have this as a background that will look the same in different screen sizes, I'd suggest that you draw that background separately, save it as a high quality png, and then put it in one ImageView, without all the hassle of margins and rotation.
You must set the background of layout to that picture in order not to show it in dp.
The dp size is like the pixel of screen and its getting smaller and bigger in other devices try using the value match_parent instead of 4000dp
The best way is to use apps like picsart or picsay. Just put all the pictures in one picture edit the picture then make that the background of the whole layout with these values
layout_width="match_parent"
layout_height="match_parent"
but if you want to animate them you must add the layout to another layout
and give them layout_weight
My designer keeps adding a little padding around my image.
I want the image the stick on the top and to fill out the width of the screen.
Here's a screenshot from the designer:
This is my XML code:
<ImageView
android:id="#+id/markertap_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/header_bootsverleih_xhdpi" />
It looks like the image is larger than the screen width, so it's getting scaled down after sizing. Try adding android:adjustViewBounds="true" to your ImageView.
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:id="#+id/imageView"
android:cropToPadding="false"
android:scaleType="fitXY" />
Experienced the same when I used an ImageView for Splashscreen, this^ worked for me.
you might want to add it your image view only this.
It fixed my problem.
android:paddingVertical="0dp"
android:paddingHorizontal="0dp"