android 圖片疊加效果

來源:互聯網
上載者:User
android 圖片疊加效果——兩種方法

轉自:http://www.cnblogs.com/not-code/archive/2011/08/18/2145126.html

第一種:

第二種:

 

第一種是通過canvas畫出來的效果:

public void first(View v) {        // 防止出現Immutable bitmap passed to Canvas constructor錯誤        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),                R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);        Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(                R.drawable.go)).getBitmap();        Bitmap newBitmap = null;        newBitmap = Bitmap.createBitmap(bitmap1);        Canvas canvas = new Canvas(newBitmap);        Paint paint = new Paint();        int w = bitmap1.getWidth();        int h = bitmap1.getHeight();        int w_2 = bitmap2.getWidth();        int h_2 = bitmap2.getHeight();        paint.setColor(Color.GRAY);        paint.setAlpha(125);        canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint);        paint = new Paint();        canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,                Math.abs(h - h_2) / 2, paint);        canvas.save(Canvas.ALL_SAVE_FLAG);        // 儲存新合成的圖片        canvas.restore();        image.setImageBitmap(newBitmap);    }

 

Canvas canvas = new Canvas(newBitmap); 當以newBitmap建立Canvas時,所以操作都已經在該圖上實現了。

該例子可以想做是播放器開始播放的效果,計算好中間位置,先覆蓋一層透明灰色的正方形,然後在中間畫上自己播放的按鈕。

 

第二種是使用系統的LayerDrawable類,該類主要用來控制多個圖片的結合:

 

public void second(View v) {        Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(                R.drawable.apple)).getBitmap();        Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(                R.drawable.go)).getBitmap();        Drawable[] array = new Drawable[2];        array[0] = new BitmapDrawable(bitmap1);        array[1] = new BitmapDrawable(bitmap2);        LayerDrawable la = new LayerDrawable(array);        // 其中第一個參數為層的索引號,後面的四個參數分別為left、top、right和bottom        la.setLayerInset(0, 0, 0, 0, 0);        la.setLayerInset(1, 20, 20, 20, 20);        image.setImageDrawable(la);    }

關聯陣列array,控制每一層的位置

 

注意:上面防止出現Immutable bitmap passed to Canvas constructor錯誤

原因是如果不用copy的方法,直接引用會對資源檔進行修改,而android是不允許在代碼裡修改res檔案裡的圖片

我也不知道為什麼花費一個小時測試代碼和寫這篇文章,或許是為了以後資料的查看,或許是為了驗證自己心中的那個效果,或許是心態突然變得平淡的緣故,就如我現在在聽著four season in one day這個心情。

項目代碼:http://files.cnblogs.com/not-code/picture_overlay.zip

原文地址:http://www.cnblogs.com/not-code/archive/2011/08/18/2145126.html

本文為原創,如需轉載,請註明作者和出處,謝謝!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.