slow application when use setImageBitmap in recyclerview android - android

I'm using BitmapFactory.decodeFile to set image in imageview in recyclerview it work but slow app when i scroll so i'm using picasso library to load images for my app but it doesn't load image
MyAdapter
#Override
public void onBindViewHolder(MyAdapter.ViewMolder holder, final int position) {
holder.textView3.setText(personArrayList.get(position).name);
int mod=Integer.parseInt(personArrayList.get(position).how);
mod=mod-(mod%5);
holder.textView4.setText(mod+"");
if(!personArrayList.get(position).pathImage.isEmpty()) {
Picasso.with(holder.imageView5.getContext())
.load(personArrayList.get(position).pathImage)
.fit()
.into(holder.imageView5);
}
/*
it is work but slow
holder.imageView5.setImageBitmap(
BitmapFactory.decodeFile(personArrayList.get(position).pathImage));
*/
}

Related

Loading lots to small size images into auto scrolling list

I am trying to implement trickplay for android TV app. I have list of local file urls to show in auto scrolling RecylerView when user press and hold FF/RW buttons.
I am not using glide for this case because it mostly shows empty images as the scroll speed is so fast
To show images faster I am creating List<Drawable> by creating Drawable
// image cacher
for (int i = 0; i < length; i++) {
cachedImageList.add(Drawable.createFromPath(getPath(i)));
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.getImageCardView().setImageDrawable(cachedImageList.get(position));
}
I am also clearing the Drawable reference as soon as view is recycled
public void onViewRecycled(final MyViewHolder holder) {
holder.getImageCardView().setImageDrawable(null);
}
I am seeing a memory spike when I load the Drawable into list. Even after calling cachedImageList.clear() memory does not return to normal.
What should be the best way to load lots of small Drawable into very fast scrolling RecyclView?
Thanks

Picasso load images with horizontal lines

I have used Picasso for loading images in my Gallery page with Recyclerview and load images from Server. It works great on all devices good,except Samsung S4 device.In S4 device the images are corrupted and it displays some horizontal lines on all images.What is the problem in my code.Also I think on same S4 device it works fine before updating my supports library versions from 22.2.1 to 23.3.0.
GalleryAdapter.java
#Override
public void onBindViewHolder(GalleryViewHolder galleryViewHolder, final int position) {
// Load images from Assets using Picasso library.
Picasso picasso = Picasso.with(context);
//picasso.setLoggingEnabled(true);
//picasso.setIndicatorsEnabled(true);
if(AppUtilities.isNetworkConnected(context)){
picasso.load(imagesArrayList.get(position).getUrl())
.placeholder(android.R.color.darker_gray)
.fit()
.centerCrop()
.memoryPolicy(MemoryPolicy.NO_CACHE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(galleryViewHolder.galleryImageView);
}
else{
picasso.load(imagesArrayList.get(position).getUrl())
.placeholder(android.R.color.darker_gray)
.fit()
.centerCrop()
.into(galleryViewHolder.galleryImageView);
}
}
public class GalleryViewHolder extends RecyclerView.ViewHolder {
protected ImageView galleryImageView;
public GalleryViewHolder(View itemView) {
super(itemView);
this.galleryImageView = (ImageView) itemView.findViewById(R.id.gallery_imageview);
}
}
Try this, before setting the image just set it to null like this:
galleryViewHolder.galleryImageView.setImageDrawable(null);

Picasso loads image with triangle in corner of image

I'm using picasso library for loading images from server into my application. my problem is when image loaded it has a triangle in top-left corner of image with color(like blue,green,red).
this is my code for loading image:
public static void loadDynamicImage(final String url, final Context context, final ImageView imageView, final int width, final int height){
Picasso.with(context).load(url)
.networkPolicy(NetworkPolicy.OFFLINE)
.resize(width,height)
.onlyScaleDown()
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context).load(url).resize(width,height).onlyScaleDown().into(imageView);
}
});
}
the image shown is :
You have enabled debug indicators on your Picasso instance (see official website). Look for setIndicatorsEnabled(true) in your code and remove it.
You have setIndicatorsEnabled set to true
Picasso picasso = Picasso.with(this);
picasso.setIndicatorsEnabled(false); //Or remove picasso.setIndicatorsEnabled(true);
Check this: Is there any way from which we can detect images are loading from cache in picasso?

PhotoView with Picasso: Some images appears smaller on listview

I am using PhotoView with Picasso and when I load images into ListView
1) It makes some images smaller.
2) not all images are loaded on launch, when i scroll down an then up then all the images are loaded.
Without PhotoView images loads just fine. I tried so many ImageView zooming libraries but PhotoView works better and I have also tried it with Glide the result is same.
Is there anyway I could force PhotoView to fit to ImageView attributes (which are Match_Parent)?
Here is my Adapter
#Override
public void onBindViewHolder( final ViewHolder holder, int position) {
Picasso.with(context)
.load((Integer) data.get(position))
.resize(530,999)
.onlyScaleDown()
.centerInside()
.into(holder.image);
holder.mAttacher = new PhotoViewAttacher(holder.image);
holder.image.setTag(holder);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public PhotoView image;
public PhotoViewAttacher mAttacher;
public ViewHolder(View view) {
super(view);
image = (PhotoView) view.findViewById(R.id.imagview);
I am loading images from resources/drawable not from any url
Unfortunately, there is an issue at Picasso, more details at issue #364
I switched to another library Glide
You can change your attacher scale type:
holder.mAttacher = new PhotoViewAttacher(holder.image);
holder.mAttacher.setScaleType(ImageView.ScaleType.CENTER_CROP);
CENTER_CROP: Scale the image uniformly (maintain the image's aspect ratio) so that
both dimensions (width and height) of the image will be equal to or
larger than the corresponding dimension of the view (minus padding).
The image is then centered in the view.

Android ListView Bitmap Optimization

I have a ListView that display Bitmap pictures. These Bitmaps get to be quite large. When i scroll on the ListView it seems to be very heavy. What techniques can i use to optimize the ListView ? This may cover compressing the Bitmap in memory, or ways to enhance the List View memory management ?
First, read this,
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
It talks about the view holder pattern, and loading images in a threads. also, read this,
http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
This talks about how to build an efficient memory cache for bitmaps.
If that's not enough, another technique you can employ is to avoid loading images until scrolling stops. This prevents the listview from loading all of the images if the user say flings to the bottom of the list. Basically, something like this,
pagerList.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState != OnScrollListener.SCROLL_STATE_IDLE) {
return;
}
// load images for adapter views between first and first+count.
// depending on your memory requirements, you can pre-load additional
// images before first and after first+count to give a better
// user exp
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
first = firstVisibleItem;
count = visibleItemCount;
}
});
This requires that you keep a handle to the ImageView for each item in your adapter, so you can find it later and set the appropriate bitmap into it. This could be as simple as keeping an array of image views in your adapter, where the index == the position in the list view.
I mostly use LruCache to optimize list and load images from cache LruCache
add this in getView in BaseAdapter class
#Override
public View getView(.....
..... . .
Bitmap image = getBitmapFromMemCache(name);
if (image == null)
{
image = decodeSampledBitmapFromUri(image_path.get(position), 64,64);
}
else
{
Log.i("loding. ", "from cache "+name);
}
// Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(details), 64, 64);
holder.img.setImageBitmap(image);
addBitmapToMemoryCache(name, image);

Categories

Resources