叠加2张图片

时间:2014-03-27 10:16:04

标签: android bitmap drawing overlay

我正在尝试将2张图片叠加在其他图像上。 我从其他班级得到的一张图片:

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

另一张图片来自drawable:

Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.topshow);

然后我得到叠加层:

Bitmap overLay = (overlay(bitmap,icon));

叠加功能:

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, new Matrix(), null);
    canvas.drawBitmap(bmp2, 0, 0, null);
    return bmOverlay;
}

并将叠加层保存为jpg:

overLay.compress(Bitmap.CompressFormat.JPEG, 80,new FileOutputStream(file_name + ".jpg"));

问题是我看起来不那么好的形象。在叠加图像的边界上是干涉,它看起来质量很差。

两张图片均为288x384。它们在叠加前都看起来很好。你有什么建议我的?

1 个答案:

答案 0 :(得分:2)

试试这段代码。我希望它能帮到你。

            Bitmap bMap = null;
            Bitmap tempbMap = null;
            tempbMap = Constants.wholeBitmap;

            bMap = Bitmap.createScaledBitmap(tempbMap, 320, 320, true);

            int BORDER_WIDTH = 0;
            int BORDER_COLOR = Color.parseColor("#00000000");
            Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH,
                                             bMap.getHeight() + 2 * BORDER_WIDTH,
                                             bMap.getConfig());
            Canvas c = new Canvas(res);
            Paint p = new Paint();

            p.setColor(BORDER_COLOR);
            c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);
            p = new Paint(Paint.FILTER_BITMAP_FLAG);
            c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);

            Bitmap bMapFinal = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);

            Bitmap bitmapOverlay = BitmapFactory.decodeResource(getResources(), R.drawable.overlay_3_large);

            int width = 293;
            int height = 55; 

            int w = (int) (bMapFinal.getWidth()/2);
            int h = (int) ((w * height) / width);

            Bitmap scaled = Bitmap.createScaledBitmap(bitmapOverlay, w, h, true);

            c.drawBitmap(scaled, bMapFinal.getWidth() - scaled.getWidth(), bMapFinal.getHeight() - scaled.getHeight(), p);

            Bitmap bMapFinal_new = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);

            ivImageMain.setImageBitmap(null);
            ivImageMain.destroyDrawingCache();

            Constants.finalBitmapForShare = bMapFinal_new;

            ivImageMain.setImageBitmap(bMapFinal_new);

谢谢。