第一个位图设置为填充整个画布。然后我添加了另一个使用以下方法从imageview创建的位图:
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};
}
答案 0 :(得分:3)
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;
}
}
答案 1 :(得分: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;
}