2016-11-11 2 views
0

Первый битмап установлен, чтобы заполнить весь холст. Затем я добавить еще один точечный рисунок, который был создан из ImageView с помощью:Как объединить два растровых изображения в Android

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

Затем я хочу, чтобы добавить растровое изображение поверх другого держать это же масштаб, поворот и перевод относительно другого растрового изображения. Моя проблема в том, что, хотя масштаб и поворот кажутся прекрасными, перевод сдвигается в одну сторону или другую.

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}; 
} 

ответ

2
public static Bitmap combineBitmap(Bitmap background, Bitmap foreground) { 
    Bitmap result; 
    try { 
     if (background == null) { 
      return null; 
     } 
     int bgWidth = background.getWidth(); 
     int bgHeight = background.getHeight(); 
     int fgWidth = foreground.getWidth(); 
     int fgHeight = foreground.getHeight(); 
     result = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888); 
     Canvas cv = new Canvas(result); 
     cv.drawBitmap(background, 0, 0, null); 
     cv.drawBitmap(foreground, (bgWidth - fgWidth)/2, (bgHeight - fgHeight)/2, null); 
     cv.save(Canvas.ALL_SAVE_FLAG); 
     cv.restore(); 
     return result; 
    } catch (Exception e) { 
     return null; 
    } 
} 
0

Проверьте, есть ли у обоих изображений одинаковые изображения имеют одинаковую высоту и ширину, а затем проверить для каждого пикселя равно.

boolean imagesEqualCheck(Image img1, Image img2) 
{ 
if (img1.getHeight() != img2.getHeight()) return false; 
if (img1.getWidth() != img2.getWidth()) return false; 

for (int y = 0; y < img1.getHeight(); ++y) 
    for (int x = 0; x < img1.getWidth(); ++x) 
     if (img1.getPixel(x, y) != img2.getPixel(x, y)) return false; 
return true; 
} 
Смежные вопросы