I've started an app where I'm getting images from a RESTful server in response to the user clicking on another component on the screen. I've set it up so that it gets the data back, creates a byte array and displays that on the screen - all good.
However I've learnt that this may have memory leaks and the best way to load images is with glide. I've got the simple glide working but it has problems. Specifically when I reload an image onto an image view the component shrinks to a height of zero and then resizes again. How can I say to Glide - keep the component the same size during the transition? Crossfade doesn't work and I can't see anything that can do this.
EDIT
The code with the glide is as follows:
Glide.with(this)
.load(Base64.decode(image, Base64.DEFAULT))
.into((ImageView) findViewById(R.id.cutoutImageView));
The image variable is a string containing the images retrieved from a RESTful service.
The XML defining the imageview is as follows:
<ImageView
android:id="#+id/cutoutImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/annotatedImageView"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:background="#color/colorPrimary"
android:padding="1dp"/>
Note that I can't hardcode the size of the image view because I don't know what size image I'll be getting from the RESTful service until I get it. I do know that once I get one image then the subsequent images will all be the same size though.
Another issue is that glide wants to scale the iamge - I don't I want to show the image at the size I get it. How do I do that?
I'm using this news api to pull the json data into my app and render it in my articlerecycler adapter class.
From the api there is a json value urlToImage that contains the thumbnail that I need to render. Currently I have a working list of 20 or so articles displaying the author, title, description, etc.
What is the best way to show the thumbnails as well?
Is using ImageView widget the best way to handle this?
For example,
<ImageView
android:id="#+id/thumbnailIv"
android:layout_width="94dp"
android:layout_height="93dp"
android:layout_weight="1"
android:contentDescription="#string/urlToImage"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/placeholder_image" />
I've found through other posts that picasso is a good way to lazy load images, specifically thumbnails.
// load the image with Picasso
Picasso.get()
.load("") // load the image
.into(thumbnailIv) // select the ImageVi
}
Yes use ImageView and use library like Glide or picasso to load image into ImageView from the url
GlideApp.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.DATA) //optional
.placeholder(R.drawable.default_image) //optional
.error(R.drawable.default_image) //optional
.into(holder.imageNews);
With ImageView you can control ScaleType another useful property is android:adjustViewBounds="true" which will maintain aspect ratio . If you use use linear/relative and set image as background then you cannot control these properties
I used picasso as image library for my application. Images were loading a bit slowly so i decided to remove placeholder and it started loading faster.
How come if placeholder is not there its loading faster. But problem is if I remove placeholder. Before loading image it squeezes that area and once image loads it takes proper area filled with image.
My Imageview code below:
<com.wallie.imagview.SquareImageView
android:id="#+id/imageone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/border_image_view"
android:padding="1dp" />
Picasso code i used earlier (loads image a bit slower ):
Picasso.with(context).load(url)
.placeholder(R.drawable.image_loading)
.into(imageView);
Picasso code i used now (loads image faster):
Picasso.with(context).load(url)
.into(imageView);
But problem is it squeezes space for image without placeholder and my spinner for all images get in one place.
I'm developing an app that display a listview with different images that are edited when the user click on them.
This is the code of my row.xml file.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="80dp"
android:layout_height="80dp"/>
<TextView
android:id="#+id/textView1"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="TextView"
android:textSize="25sp"
/>
I'm using only one set of images, stored in the folder res/drawable/drawable-nodpi (Because I need them to keep its original size in order to edit them as bitmaps).
The problem is that those images are to big too be displayed in a single activity and therefore, when I create the listview it use a lot of memory and the list is scrolls with some lag. How can I display those images in the list without this problem? Should I create another set of smaller images to use them in the list? I've read post when the app save the images to the SD card, or the cache memory, but actually i'm not sure about the solution that fit best with my specific application.
Thanks in advance
You can use the following link below to make your list. Here they free the image cache once it is not seen. Using this you can put many image sin your list :
https://github.com/thest1/LazyList
Hope this helps you.
Try this..
Android-Universal-Image-Loader libriary. This project aims to provide a reusable instrument for asynchronous image loading, caching and displaying. It is originally based on Fedor Vlasov's project and has been vastly refactored and improved since then.
https://github.com/nostra13/Android-Universal-Image-Loader
and for drawable resources use this..
String imageUri = "drawable://" + R.drawable.image; // from drawables (only images, non-9patch)
I think these ways may help:
Put these images in server, then use Universal image loader to load it. Universal image loader have solved this problem. you can try this project https://github.com/dodola/Android-Universal-Image-Loader
If you have to put these images in res, I think you can implement you own image loader. In your Image loader, you have to cache the images in memory by use softreference. This is another problem.
I suggest you to use Aquery to download images. Just download the jarfile from this link
How to use :
AQuery _aq = new AQuery(youractivity context);
//fetch and set the image from internet, cache with file and memory
aq.id(R.id.your_imageview_object).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
As you all see in attachment, android imageview got distortion after being completely downloaded with using coding of following link Load images from web and caching.
here is my imageview tag and original image resolution is 850x315.
<ImageView
android:paddingTop="10dip"
android:paddingBottom="5dip"
android:id="#+id/idThumb"
android:layout_width="match_parent"
android:layout_height="70dp"
android:contentDescription="#string/app_name"
android:scaleType="centerCrop"
android:src="#drawable/no_image" />
Please check me why this ImageView cannot fetch original resolution of image from URL and got blur.!
gotta use WebView instead of ImageView will fulfilled what I want.