Search code examples
androidandroid-image

How to show a combined image of two resource png images in android


My application shows a gridview list with some devices. Each devices has four states. I have a different types of devices images and four state images. I want to combine device images with state image and display in gridview. The images are stores in application resource. Images are shown using ImageView.

How i can do this ?

image1

enter image description here

image2

enter image description here

the output will be

enter image description here

edited new images

enter image description here enter image description here enter image description here


Solution

  • Try this works great with me

    public Bitmap combineImages(int boxDrawableId , int closeDrawableId) {
    
            Bitmap box = BitmapFactory.decodeResource(getResources(),boxDrawableId);
            Bitmap close = BitmapFactory.decodeResource(getResources(), closeDrawableId);
    
            Bitmap bitmapCreate = Bitmap.createBitmap(box.getWidth(), box.getHeight(), Bitmap.Config.ARGB_8888);
    
            Canvas comboImage = new Canvas(bitmapCreate);
    
            comboImage.drawBitmap(box, 0, 0, null);
            comboImage.drawBitmap(close, box.getWidth()-close.getWidth(), box.getHeight()-close.getHeight(), null);
            if (box != null) {
                try {
                    box.recycle();
                    close.recycle();
                    box = null;
                    close = null;
                } catch (Throwable e) {
                }
            }
            return bitmapCreate;
        }
    

    How to call?

    imgView.setImageBitmap(combineImages(R.drawable.box,R.drawable.close));
    

    Output

    enter image description here