在android中合并两个位图

时间:2015-07-28 12:03:44

标签: android bitmap

我正在处理图像合并应用程序我正在使用此代码。

Bitmap.Config config = bm1.getConfig();

if (config == null) {
config = Bitmap.Config.ARGB_8888;
}

newBitmap = Bitmap.createBitmap(w, h, config);

Canvas newCanvas = new Canvas(newBitmap);

newCanvas.drawBitmap(bm1, 0, 0, null);

Paint paint = new Paint();

paint.setAlpha(128);

newCanvas.drawBitmap(bm2, 0, 0, paint);

对于Merge但它给我的结果如此 result 我有形象 image 1image 2

我需要这个结果result_required 可能吗 ?如何 ?我尝试使用alpha但它会生成完整的图像。 我尝试使用alpha但它适用于完整图像。

2 个答案:

答案 0 :(得分:0)

您可以使用以下内容:

https://developer.android.com/reference/android/graphics/PorterDuff.Mode.html

可在此处找到不同模式的详细信息和结果:

http://ssp.impulsetrain.com/porterduff.html

只需选择所需的算法并在位图上使用它。

答案 1 :(得分:0)

两天后,我的朋友找到了这样做的方法 我正在使用这个功能

  private Bitmap ProcessingBitmap() {
    Bitmap bm1 = null;
    Bitmap bm2 = null;
    Bitmap newBitmap = null;
    try {
        bm1 = myBitmap;
        bm2 = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.bc01), bm1.getWidth(), bm1.getHeight(), true);
        int w;
        if (bm1.getWidth() >= bm2.getWidth()) {
            w = bm1.getWidth();
        } else {
            w = bm2.getWidth();
        }
        int h;
        if (bm1.getHeight() >= bm2.getHeight()) {
            h = bm1.getHeight();
        } else {
            h = bm2.getHeight();
        }
        Bitmap.Config config = bm1.getConfig();
        if (config == null) {
            config = Bitmap.Config.ARGB_8888;
        }
        newBitmap = Bitmap.createBitmap(w, h, config);
        Canvas newCanvas = new Canvas(newBitmap);

        //define half/half area
        Rect rect1Half = new Rect(0, 0, ((bm1.getWidth() * 100) / 100),
                bm1.getHeight());
        Rect rect2Half = new Rect(((bm2.getWidth() * value) / 100) + 1,
                0, bm2.getWidth(), bm2.getHeight());

        newCanvas.drawBitmap(bm1, rect1Half, rect1Half, null);
        Paint paint = new Paint();

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
        // paint.setAlpha(valueAlpha);
        paint.setMaskFilter(new BlurMaskFilter(50, BlurMaskFilter.Blur.NORMAL));


        newCanvas.drawBitmap(bm2, rect2Half, rect2Half, paint);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    image.setImageBitmap(newBitmap);
    return newBitmap;
}
  

paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));        paint.setMaskFilter(new BlurMaskFilter(50,BlurMaskFilter.Blur.NORMAL));

是半径为50的blur blurfilter的主要代码。