How to handle variable height image in RecyclerView? - android

I am making a chat application. For that, if the message is of image type, I want to wrap the height and fix the width. But the problem is that since image takes some time to load, there is a shift in chat messages until the image is loaded and so there is a jerk. How do I handle this?
<android.support.constraint.ConstraintLayout
android:id="#+id/parent_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/send_message_box"
android:maxWidth="228dp">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/iv_message"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_width="200dp"
android:layout_height="0dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:padding="11dp"
android:visibility="gone" />
</android.support.constraint.ConstraintLayout>
This is the image layout I am using.

You could fix the height and display a placeholder image till the image gets loaded. You get this functionality of displaying a placeholder image(till the image gets loaded) with Picasso, check this https://square.github.io/picasso/

You can set up an initial height using a default image while your images are getting downloaded. After the image is downloaded, it will take the height of the image if you set the height as wrap_content. Hence you need to modify your ImageView like the following.
<ImageView
android:id="#+id/iv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#mipmap/ic_image_loader_placeholder" />
Please modify the ImageView as per your requirement.
You might also look at this Github project where I have implemented a similar idea. I had to call an API of Flickr and load the images into the RecyclerView. Hope that helps!

Related

Android: Loading image using Glide leaves whitespace on both ends

I am loading image from server using Glide. The image size is 600 X 400px. Now my image view is as follows
<ImageView
android:id="#+id/imageView76"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="#dimen/margin_eight"
android:layout_marginTop="#dimen/margin_eight"
android:layout_marginEnd="#dimen/margin_eight"
android:adjustViewBounds="true"
app:bindImageUrl="#{newsModel.images}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
This is how I am loading image:
Glide.with(view.context).asBitmap().load(imageUrl).apply(RequestOptions().optionalCenterInside()).into(view)
But the result is like this:
As show, white space appears on both ends of image. How can I make this image use full width of imageview, while maintaining aspect ratio
androd:scaleType=“fit_xy”
you this add to imageviewthis attribute
It will help you the image full the whole ImageView

How to take some part of image out of container

I have a requirement to place an image at the end of layout, and it will be 20% out of the screen boundary, I am unable to find out any way to achieve this, the only possible way is to set image view width to 1000dp, the some part of image is out of the screen, but this does not seem to be a valid approach
I want to achieve something like following.
Try using Horizontal/vertical bias in ConstraintLayout. Something like:
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="104dp"
android:src="#drawable/ic_person_black"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.2"
app:layout_constraintTop_toTopOf="parent" />
In above example imageview is cropped 20% from right

Way to zoom image in ImageView which has certain size?

There's an ImageView with certain size (100dp, 100dp). And there's an image which size is 1000-1000px.
That's how it looks right now:
1
What i want is:
2
So image wont come out of ImageView's borders and will be sort of zoomed to center in some value, that i can change. Can it be done in some way? Maybe i need something different from ImageView? At the moment, i use adapter for recyclerview, which loads list of items that contain an image that needs to be kinda zoomed.
<ImageView
android:id="#+id/cart_item_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#drawable/min" />
just add android:scaleType="centerCrop"
here is the code look like:
<ImageView
android:id="#+id/cart_item_image"
android:layout_margin="5dp"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop" />

Place Circular ImageView on the Edge of another Circular ImageView

I wan to place Small ImageView on the anchor of CircularView just like below image
My Code is
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.hey.heypickup.UI.UICircularImage xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/contct_imgpic"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_margin="5dp"
android:layout_weight="0.04"
android:padding="10dp"
app:line_color="#color/colorPrimary"
app:line_color_press="#color/green"
android:src="#drawable/ph_1"
app:line_width="1dp"
app:padding="0dp"/>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="bottom|end"
android:src="#drawable/ic_phone_black"
/>
</FrameLayout>
Result of above code:
but the Second imageview not appearing at the achor of circularImageView?
Can we achieve this without using floating action button ?
if yes then how ?
Since your second image is overlapping the first image,
I think it's because your ImageView has a wrap_content height and width. You should make it a fix size, and try it out because if your ic_dialog_email image is large enough then it will take entire space and might overlap the first image.
Would you make height and width fix and try again? For example, make it 15dp each and take a look. Also remove that margin from ImageView along with that change.
You'll just need to trial and check the exact size your need for the second image along with required margin to position itself correctly as per your UI requirements

android - loaded image's height it bigger than actual image

I am loading an image from the web with a AsyncTask class and set it to a imageview in my code.
// loading of the image into the ImageView
new DownloadImageTask(MyImageView).execute("ImageURL");
the problem is, every time I load an image, the height of it is different, even though the width and height of the ImageView is warp_content.
I added sort of a border line with the Background and padding to see the actual height of it.
Here is the xml
<ImageView
android:id="#+id/ivBigRecipeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:layout_toRightOf="#+id/ingredientsTitle"
android:background="#52D017"
android:contentDescription="food"
android:padding="1dp" />
in here are the pics
sometimes this happens:
and sometimes this happens:
Any ideas? I want to just have it exactly the actual size of it
thanks!
Try to add android:adjustViewBounds="true" to your ImageView, and it should work.

Categories

Resources