android 圖片疊加效果——兩種方法的簡介與內容 ,帶解決Immutable bitmap passed to Canvas constructor錯誤

來源:互聯網
上載者:User

標籤:

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

[java] view plaincopyprint?
  1. public void first(View v) {  
  2.   
  3.         // 防止出現Immutable bitmap passed to Canvas constructor錯誤  
  4.         Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),  
  5.                 R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);  
  6.         Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(  
  7.                 R.drawable.go)).getBitmap();  
  8.   
  9.         Bitmap newBitmap = null;  
  10.   
  11.         newBitmap = Bitmap.createBitmap(bitmap1);  
  12.         Canvas canvas = new Canvas(newBitmap);  
  13.         Paint paint = new Paint();  
  14.   
  15.         int w = bitmap1.getWidth();  
  16.         int h = bitmap1.getHeight();  
  17.   
  18.         int w_2 = bitmap2.getWidth();  
  19.         int h_2 = bitmap2.getHeight();  
  20.   
  21.         paint.setColor(Color.GRAY);  
  22.         paint.setAlpha(125);  
  23.         canvas.drawRect(0, 0, bitmap1.getWidth(), bitmap1.getHeight(), paint);  
  24.   
  25.         paint = new Paint();  
  26.         canvas.drawBitmap(bitmap2, Math.abs(w - w_2) / 2,  
  27.                 Math.abs(h - h_2) / 2, paint);  
  28.         canvas.save(Canvas.ALL_SAVE_FLAG);  
  29.         // 儲存新合成的圖片  
  30.         canvas.restore();  
  31.   
  32.         image.setImageBitmap(newBitmap);  
  33.     }  
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類,該類主要用來控制多個圖片的結合:

 

[java] view plaincopyprint?
  1. public void second(View v) {  
  2.   
  3.         Bitmap bitmap1 = ((BitmapDrawable) getResources().getDrawable(  
  4.                 R.drawable.apple)).getBitmap();  
  5.         Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(  
  6.                 R.drawable.go)).getBitmap();  
  7.   
  8.         Drawable[] array = new Drawable[2];  
  9.         array[0] = new BitmapDrawable(bitmap1);  
  10.         array[1] = new BitmapDrawable(bitmap2);  
  11.         LayerDrawable la = new LayerDrawable(array);  
  12.         // 其中第一個參數為層的索引號,後面的四個參數分別為left、top、right和bottom  
  13.         la.setLayerInset(0, 0, 0, 0, 0);  
  14.         la.setLayerInset(1, 20, 20, 20, 20);  
  15.         image.setImageDrawable(la);  
  16.     }  
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,控制每一層的位置

 裝載自:http://blog.csdn.net/gh102/article/details/6719624

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

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

android 圖片疊加效果——兩種方法的簡介與內容 ,帶解決Immutable bitmap passed to Canvas constructor錯誤

聯繫我們

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