Image ratio check at runtime (Performance issue) - android - android

I have an image picker application that selects images from gallery. If the image doesn't have a particular width/height ratio I display a small warning symbol on top of the image.
In my XML View File I have two ImageView views. It is below:
view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp" >
<ImageView
android:id="#+id/imgQueue"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:id="#+id/wrongimagesign"
android:src="#drawable/error"
android:layout_alignTop="#+id/imgQueue"
android:layout_alignRight="#+id/imgQueue"
android:layout_alignEnd="#+id/imgQueue"
android:visibility="invisible"/>
</RelativeLayout>
The images are displayed in a gridview so in my adapter i have this code:
ImageLoader.getInstance().displayImage("file://" + image.cardPath, imgQueue);
**Bitmap bitmap = ImageLoader.getInstance().loadImageSync("file://" + image.cardPath, Utility.displayImageOptions);**
wrongImageSign.setVisibility(imageRatioIsBad(bitmap) ? View.VISIBLE : View.INVISIBLE);
The problem is that since i am loading the bitmap the performance is very slow (it takes too long to load the image and display the warning sign). Is there a way to improve the performance.

Related

ImageView inside recyclerView width and height layout

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

ImageView inside recyclerView proper dimensions

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??

Displaying Multiple Image of different size in recycler view

I have a typical recycler adapter set up. Some of the items in the adapter have images, sometimes just one image, maximum 12. I want to display images, like the one below, depending on the number of images in each item:
Currently, I used different layout files to show a different set of images. But the problem is images are of varied sizes and I have defined set for 4 images only I am using GLIDE Library to display images.
Code for if images are 4, They will resize to bigger image size
<?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="wrap_content"
android:layout_weight="3"
android:minHeight="#dimen/dp_200"
android:orientation="horizontal">
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="#string/content_desc"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/image5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="#string/content_desc"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/image6"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="true"
android:contentDescription="#string/content_desc"
android:scaleType="fitXY" />
</LinearLayout>
I am looking for any libraries available which should work with Target API 29. The previous solution is not working with newer Target API.
I think you can use flexboxlayoutmanager, you can check you sample here
Use a horizontal Recycler View inside vertical Recycler View.By this width of your images will also vary accordingly.

Android overlay an image/wrap image inside another image

In Android, preferably in XML is it possible to overlay an image around another image?
I have an image which is a blue circle and My app retrieves an icon from an API. After the icon has been retrieved it is displayed via an ImageView.
The XML for the ImageView is:
<ImageView
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="100dp"
android:id="#+id/weather_icon"
android:scaleType="fitCenter"/>
Now I would like to "wrap" an image around this image. The XML for the image I want around the retrieved image is:
<ImageView
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="#drawable/image_wrap"/>

Scale background image and foreground image at same ratio in Android

I have a centered background image with a pattern that I am showing using an ImageView. I want it to fill the width and height of all the different screen sizes/densities, but maintain its aspect ratio. I think I have that part figured out, but then I have a foreground image (in a ToggleButton) that is smaller and centered and needs to scale at the same rate of the background image. This is important because the button image has graphical elements that need to align perfectly with the background. How do I get it to scale correctly? Here's what I've got so far:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relmain"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
android:contentDescription="#string/bg_desc"
android:src="#drawable/bg_img" />
<ToggleButton
android:id="#+id/button"
android:background="#drawable/btn_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_centerInParent="true"
android:textOff=""
android:textOn=""
android:onClick="toggleLight" />
</RelativeLayout>
Thanks for taking a look!
I think you should do it like this according to my skills
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
android:contentDescription="#string/bg_desc"
android:background="#drawable/bg_img" />
May be it will look as you want.

Categories

Resources