Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The first bitmap is set to fill the entire canvas. I then add another bitmap that has been created from an imageview using:

Java
tattoo.buildDrawingCache();
Bitmap bit2 = tattoo.getDrawingCache();


I then want to add this bitmap over the other keeping it's same scale, rotation, and translation relative to the other bitmap. My issue is that, while the scale and rotation seem fine, the translation shifts to one side or the other.

What I have tried:

Java
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {

    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, 0,0 , null);

    Matrix matrix = new Matrix();
    matrix.setScale(tattoo.getScaleX() / imageView.getScaleX(), tattoo.getScaleY() / imageView.getScaleY());

    int[] tattooCoords = getRelativeCoords(tattoo);
    int[] imageViewCoords = getRelativeCoords(imageView);
    matrix.setTranslate(tattooCoords[0] - imageViewCoords[0], tattooCoords[1] - imageViewCoords[1]);
    matrix.postRotate(tattoo.getRotation(), tattoo.getX() + tattoo.getWidth() / 2,
            tattoo.getY() + tattoo.getHeight() / 2);
    canvas.drawBitmap(bmp2, matrix, null);

    bmp1.recycle();
    bmp2.recycle();

    return bmOverlay;

}

private static int[] getRelativeCoords(View v){
    View parent = v.getRootView();
    int[] viewLocation = new int[2];
    v.getLocationInWindow(viewLocation);

    int[] rootLocation = new int[2];
    parent.getLocationInWindow(rootLocation);

    int relativeLeft = viewLocation[0] - rootLocation[0];
    int relativeTop  = viewLocation[1] - rootLocation[1];

    return new int[]{relativeLeft, relativeTop};
}
Posted
Comments
Member 12846241 11-Nov-16 22:43pm    
I should add that both images have an multitouch listener that allows the first image to be zoomed and translated, and the second image to be zoomed, translated and rotated.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900