How to recycle images

2

I have declared a couple of images and I draw them with the canvas. My question is what would be the correct way to recycle or delete an image that I am no longer using or do not want to be seen to free memory.

Thank you.

public class Juego extends SurfaceView {

    Bitmap imagen1, imagen2, imagen3, imagen4, imagen5;
    Drawable imagen1_drawable, imagen2_drawable, imagen3_drawable, imagen4_drawable, imagen5_drawable;
    Paint paint;

    public Juego(Context context, AttributeSet attrs) {
        super(context, attrs);

        imagen1_drawable = context.getResources().getDrawable(R.drawable.imagen1);
        imagen2_drawable = context.getResources().getDrawable(R.drawable.imagen2);
        imagen3_drawable = context.getResources().getDrawable(R.drawable.imagen3);
        imagen4_drawable = context.getResources().getDrawable(R.drawable.imagen4);
        imagen5_drawable = context.getResources().getDrawable(R.drawable.imagen5);

        imagen1 = ((BitmapDrawable) imagen1_drawable).getBitmap();
        imagen2 = ((BitmapDrawable) imagen2_drawable).getBitmap();
        imagen3 = ((BitmapDrawable) imagen3_drawable).getBitmap();
        imagen4 = ((BitmapDrawable) imagen4_drawable).getBitmap();
        imagen5 = ((BitmapDrawable) imagen5_drawable).getBitmap();

        paint = new Paint();
    }

    public void onDraw(Canvas canvas){

        paint.setColor(Color.WHITE);
        canvas.drawRect(0,0, canvas.getWidth(), canvas.getHeight(), paint);

        canvas.drawBitmap(imagen1, 0, 0, paint);
        canvas.drawBitmap(imagen2, 50, 0, paint);
        canvas.drawBitmap(imagen3, 100, 0, paint);
        canvas.drawBitmap(imagen4, 150, 0, paint);
        canvas.drawBitmap(imagen5, 200, 0, paint);

        invalidate();
    }
}
    
asked by Zekirak 30.07.2018 в 16:31
source

0 answers