【轉】六、android圖片特效處理之圖片疊加

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

這篇將講到圖片特效處理的圖片疊加效果。跟前面一樣是對像素點進行處理,可參照前面的android影像處理系列之七--圖片塗鴉,浮水印-圖片疊加和android影像處理系列之六--給圖片添加邊框(下)-圖片疊加兩篇文章,此篇所講的與之前有一點區別。疊加原理是兩張圖片的像素點按透明度疊加,不會進行顏色過濾。疊加圖片可以是JPG格式,跟前在一樣,最好是放大assets目錄。下面看:

+=

代碼:

 

/**     * 圖片效果疊加     * @param bmp 限制了尺寸大小的Bitmap     * @return     */    private Bitmap overlay(Bitmap bmp)    {        long start = System.currentTimeMillis();        int width = bmp.getWidth();        int height = bmp.getHeight();        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);                // 對邊框圖片進行縮放        Bitmap overlay = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.rainbow_overlay);        int w = overlay.getWidth();        int h = overlay.getHeight();        float scaleX = width * 1F / w;        float scaleY = height * 1F / h;        Matrix matrix = new Matrix();        matrix.postScale(scaleX, scaleY);                Bitmap overlayCopy = Bitmap.createBitmap(overlay, 0, 0, w, h, matrix, true);                int pixColor = 0;        int layColor = 0;                int pixR = 0;        int pixG = 0;        int pixB = 0;        int pixA = 0;                int newR = 0;        int newG = 0;        int newB = 0;        int newA = 0;                int layR = 0;        int layG = 0;        int layB = 0;        int layA = 0;                final float alpha = 0.5F;                int[] srcPixels = new int[width * height];        int[] layPixels = new int[width * height];        bmp.getPixels(srcPixels, 0, width, 0, 0, width, height);        overlayCopy.getPixels(layPixels, 0, width, 0, 0, width, height);                int pos = 0;        for (int i = 0; i < height; i++)        {            for (int k = 0; k < width; k++)            {                pos = i * width + k;                pixColor = srcPixels[pos];                layColor = layPixels[pos];                                pixR = Color.red(pixColor);                pixG = Color.green(pixColor);                pixB = Color.blue(pixColor);                pixA = Color.alpha(pixColor);                                layR = Color.red(layColor);                layG = Color.green(layColor);                layB = Color.blue(layColor);                layA = Color.alpha(layColor);                                newR = (int) (pixR * alpha + layR * (1 - alpha));                newG = (int) (pixG * alpha + layG * (1 - alpha));                newB = (int) (pixB * alpha + layB * (1 - alpha));                layA = (int) (pixA * alpha + layA * (1 - alpha));                                newR = Math.min(255, Math.max(0, newR));                newG = Math.min(255, Math.max(0, newG));                newB = Math.min(255, Math.max(0, newB));                newA = Math.min(255, Math.max(0, layA));                                srcPixels[pos] = Color.argb(newA, newR, newG, newB);            }        }                bitmap.setPixels(srcPixels, 0, width, 0, 0, width, height);        long end = System.currentTimeMillis();        Log.d("may", "overlayAmeliorate used time="+(end - start));        return bitmap;    }

疊加的邊框圖片還是大點比較好,也要控制被疊加圖片大小。alpha變數值可以根據需求修改,建議還是大於0.5比較好,不然原圖會看不清楚。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.