1. 程式人生 > >android圖片疊加方法

android圖片疊加方法

Android有時候會遇到需要兩張圖片疊加起來的效果,現記錄如下:

/*
*此方法分別傳入兩個bitmap物件,一個為底圖(背景圖background),
*另一個則位於其上面(前景圖foreground),若上面的bitmap是不透明的話,
*就會完全遮住下面的bitmap,那麼儲存為圖片後,就只能看到位於上面的bitmap的資訊,
*圖片的大小為兩個bitmap疊加的大小。
*/ 
   private Bitmap toConformBitmap(Bitmap background, Bitmap foreground) {
        if( background == null ) {
            return null;
        }

        int bgWidth = background.getWidth();
        int bgHeight = background.getHeight();
        //create the new blank bitmap 建立一個新的和SRC長度寬度一樣的點陣圖
        Bitmap newbmp = Bitmap.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(newbmp);
        //draw bg into
        //在 0,0座標開始畫入bg
        cv.drawBitmap(background, 0, 0, null);
        //draw fg into
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        //在 0,0座標開始畫入fg ,可以從任意位置畫入
        cv.drawBitmap(foreground, 0, 1000, paint);
        //save all clip
        paint.setXfermode(null);
        return newbmp;
    }
/*
*儲存bitmap為一張圖片:
*/
private String saveBitmap(Bitmap bitmap) {
        String  imagePath = getApplication().getFilesDir().getAbsolutePath() + "/temp.png";
        
        File file = new File(imagePath);
        if(file.exists()) {
            file.delete();
        }
        try{
            FileOutputStream out = new FileOutputStream(file);
            if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
                out.flush();
                out.close();
            }     
        } catch (Exception e) {
            Toast.makeText(this, "儲存失敗, 1).show();
            e.printStackTrace();
        }
        return imagePath;

    }
另外有從其他部落格上看到的方法,包括上下疊加,左右拼接和上下拼接。
    /*
     * 把兩個點陣圖覆蓋合成為一個位圖,以底層點陣圖的長寬為基準
     * @param backBitmap 在底部的點陣圖
     * @param frontBitmap 蓋在上面的點陣圖
     * @return
     */
    public static Bitmap mergeBitmap(Bitmap backBitmap, Bitmap frontBitmap) {

        if (backBitmap == null || backBitmap.isRecycled() 
                || frontBitmap == null || frontBitmap.isRecycled()) {
            Log.e(TAG, "backBitmap=" + backBitmap + ";frontBitmap=" + frontBitmap);
            return null;
        }
        Bitmap bitmap = backBitmap.copy(Config.ARGB_8888, true);
        Canvas canvas = new Canvas(bitmap);
        Rect baseRect  = new Rect(0, 0, backBitmap.getWidth(), backBitmap.getHeight());
        Rect frontRect = new Rect(0, 0, frontBitmap.getWidth(), frontBitmap.getHeight());
        canvas.drawBitmap(frontBitmap, frontRect, baseRect, null);
        return bitmap;
    }

    /**
     * 把兩個點陣圖覆蓋合成為一個位圖,左右拼接
     * @param leftBitmap 
     * @param rightBitmap 
     * @param isBaseMax 是否以寬度大的點陣圖為準,true則小圖等比拉伸,false則大圖等比壓縮
     * @return
     */
    public static Bitmap mergeBitmap_LR(Bitmap leftBitmap, Bitmap rightBitmap, boolean isBaseMax) {

        if (leftBitmap == null || leftBitmap.isRecycled() 
                || rightBitmap == null || rightBitmap.isRecycled()) {
            JDLog.logError(TAG, "leftBitmap=" + leftBitmap + ";rightBitmap=" + rightBitmap);
            return null;
        }
        int height = 0; // 拼接後的高度,按照引數取大或取小
        if (isBaseMax) {
            height = leftBitmap.getHeight() > rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();
        } else {
            height = leftBitmap.getHeight() < rightBitmap.getHeight() ? leftBitmap.getHeight() : rightBitmap.getHeight();
        }

        // 縮放之後的bitmap
        Bitmap tempBitmapL = leftBitmap;
        Bitmap tempBitmapR = rightBitmap;

        if (leftBitmap.getHeight() != height) {
            tempBitmapL = Bitmap.createScaledBitmap(leftBitmap, (int)(leftBitmap.getWidth()*1f/leftBitmap.getHeight()*height), height, false);
        } else if (rightBitmap.getHeight() != height) {
            tempBitmapR = Bitmap.createScaledBitmap(rightBitmap, (int)(rightBitmap.getWidth()*1f/rightBitmap.getHeight()*height), height, false);
        }

        // 拼接後的寬度
        int width = tempBitmapL.getWidth() + tempBitmapR.getWidth();

        // 定義輸出的bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        // 縮放後兩個bitmap需要繪製的引數
        Rect leftRect = new Rect(0, 0, tempBitmapL.getWidth(), tempBitmapL.getHeight());
        Rect rightRect  = new Rect(0, 0, tempBitmapR.getWidth(), tempBitmapR.getHeight());

        // 右邊圖需要繪製的位置,往右邊偏移左邊圖的寬度,高度是相同的
        Rect rightRectT  = new Rect(tempBitmapL.getWidth(), 0, width, height);

        canvas.drawBitmap(tempBitmapL, leftRect, leftRect, null);
        canvas.drawBitmap(tempBitmapR, rightRect, rightRectT, null);
        return bitmap;
    }


    /**
     * 把兩個點陣圖覆蓋合成為一個位圖,上下拼接
     * @param leftBitmap 
     * @param rightBitmap 
     * @param isBaseMax 是否以高度大的點陣圖為準,true則小圖等比拉伸,false則大圖等比壓縮
     * @return
     */
    public static Bitmap mergeBitmap_TB(Bitmap topBitmap, Bitmap bottomBitmap, boolean isBaseMax) {

        if (topBitmap == null || topBitmap.isRecycled() 
                || bottomBitmap == null || bottomBitmap.isRecycled()) {
            JDLog.logError(TAG, "topBitmap=" + topBitmap + ";bottomBitmap=" + bottomBitmap);
            return null;
        }
        int width = 0;
        if (isBaseMax) {
            width = topBitmap.getWidth() > bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
        } else {
            width = topBitmap.getWidth() < bottomBitmap.getWidth() ? topBitmap.getWidth() : bottomBitmap.getWidth();
        }
        Bitmap tempBitmapT = topBitmap;
        Bitmap tempBitmapB = bottomBitmap;

        if (topBitmap.getWidth() != width) {
            tempBitmapT = Bitmap.createScaledBitmap(topBitmap, width, (int)(topBitmap.getHeight()*1f/topBitmap.getWidth()*width), false);
        } else if (bottomBitmap.getWidth() != width) {
            tempBitmapB = Bitmap.createScaledBitmap(bottomBitmap, width, (int)(bottomBitmap.getHeight()*1f/bottomBitmap.getWidth()*width), false);
        }

        int height = tempBitmapT.getHeight() + tempBitmapB.getHeight();

        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);

        Rect topRect = new Rect(0, 0, tempBitmapT.getWidth(), tempBitmapT.getHeight());
        Rect bottomRect  = new Rect(0, 0, tempBitmapB.getWidth(), tempBitmapB.getHeight());

        Rect bottomRectT  = new Rect(0, tempBitmapT.getHeight(), width, height);

        canvas.drawBitmap(tempBitmapT, topRect, topRect, null);
        canvas.drawBitmap(tempBitmapB, bottomRect, bottomRectT, null);
        return bitmap;
    }
此方法只是兩張圖片的簡單疊加拼接,而我們可能還需要一些更加複雜的疊加效果,所以android提供了16種疊加效果,請看下一篇!