Search code examples
androidbitmapandroid-bitmapandroid-imagecreatebitmap

Android: Create image from two bitmaps, bitmap under bitmap


What I want: Create image with two bitmap, under first bitmap put second bitmap.


At this moment I use this code

public static Bitmap combineImages(Bitmap background, Bitmap foreground, float disFromTheTopPercent) {

        int width = background.getWidth(), height = background.getHeight();
        Bitmap cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas comboImage = new Canvas(cs);
        background = Bitmap.createScaledBitmap(background, width, height, true);
        comboImage.drawBitmap(background, 0, 0, null);

        int top = (int) (disFromTheTopPercent * height);
        int left = 0;

        comboImage.drawBitmap(foreground, left, top, null);

        return cs;
    }

Bad is that it associated actually with height, weight, and dpi from my smartfon.

Its different when I use smartfone with 5 inch screen and 6 inch screen, regardless diferent screen this must looke same.

Visual presentation

Thanks for help!


Solution

  • Try this code:

    public static Bitmap combineImages(Bitmap c, Bitmap s) {
        Bitmap cs;
        int width, height;
    
        width = s.getWidth();
        height = c.getHeight() + s.getHeight();
    
        cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
        Canvas comboImage = new Canvas(cs);
    
        comboImage.drawBitmap(c, 0f, 0f, null);
        comboImage.drawBitmap(s, 0f, c.getHeight(), null);
    
        return cs;
    }