I have a question about how I can work with images of good resolution, since putting them makes the application slow, I would like to know if there is a way to work with an acceptable resolution without affecting the speed of the application. For example, a gallery of images that, as I scroll, the application slows down. Thanks in advance.
public class AdapterAlbum extends RecyclerView.Adapter<AdapterAlbum.ViewHolderClase> {
public class ViewHolderClase extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolderClase(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.cover);
}
}
@Override
public ViewHolderClase onCreateViewHolder(ViewGroup parent, int viewType) {
View viewItem = LayoutInflater.from(parent.getContext()).inflate(R.layout.album, null);
return new ViewHolderClase(viewItem);
}
@Override
public void onBindViewHolder(final ViewHolderClase holder, int position) {
holder.imageView.setBackgroundResource(Activity.imagenes.get(position));
}
@Override
public int getItemCount() {
return Activity.imagenes.size();
}
} well if the design where I load the images is simple, I should point out that my images are approximately 43.4k because they have a little more resolution
Exercise:
public class Actividad {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
static ArrayList<Integer> imagenes;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
imagenes = new ArrayList<>();
imagenes.addAll(Arrays.asList(R.drawable.cold22, R.drawable.cover3, R.drawable.coverr,
R.drawable.cover6, R.drawable.cold3, R.drawable.cover4, R.drawable.cover87, R.drawable.cover12));
contentView = inflater.inflate(R.layout.activ, null);
return contentView;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onCreated(Bundle savedInstanceState) {
super.onCreated(savedInstanceState);
recyclerView = (RecyclerView) contentView.findViewById(R.id.reyclerViewTres);
layoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(layoutManager);
adapter = new AdapterAlbum();
recyclerView.setAdapter(adapter);
}
}
How to avoid Out of memory in Android application?
This is a good question, what is the size that the images should have in an application so that it does not consume a lot of memory and therefore be slow?
Actually the answer is that they must be of an acceptable resolution but a small weight in kbs (something similar to images used in web pages), this to avoid mainly within the application processing and manipulation in memory of the image and bandwidth consumption. band that might be unnecessary.
How to make your image light? Well, you can work with byte compression .
Here is a tutorial: https://sites.google.com/site/ticvalcarcel/optimizacion-de-imagenes-para-internet/tamano-y-peso-de-las-imagenes
The Android SDK has a way to optimize image loading, which involves taking a smaller image format via the BitmapFactory class :
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Other recommendations considering a previous optimization of the images are the use of two libraries mainly,
GLIDE and PICASSO , whose implementation is simple and very similar.
There are other options but doing a benchmark in my opinion are the best options.
Regarding your problem:
You must ensure that the image is evicted from
ImageView
so that heGarbage Collector
can remove it from memory, this as soon as it is no longer visible on the screen, because if they are accumulating you could have after a certain time aJava.Lang.OutOfMemoryError
, you can do it with:either
I advise you to use one of the 3 famous frameworks for these tasks, and remember that many of these tasks require certain permissions. My recommendation is Glide for images and Volley for everything else.
Load the image when it is going to be used and delete it when it is not, if you load images but do not delete them, your application will consume more and more memory. On the other hand, I advise you to use images with a resolution according to the application, and if it is not feasible, resize them.
I use the library
Glide
to load images in aviewImage
, it takes care of resizing them appropriately for the device and saving them in the cache to speed up the process later. Try to see if you are still having a performance issue.