第一種是經過canvas畫出來的效果:java
- 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);
- }
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時,因此操做都已經在該圖上實現了。android
該例子能夠想作是播放器開始播放的效果,計算好中間位置,先覆蓋一層透明灰色的正方形,而後在中間畫上本身播放的按鈕。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,控制每一層的位置app
裝載自:http://blog.csdn.net/gh102/article/details/6719624spa
注意:上面防止出現Immutable bitmap passed to Canvas constructor錯誤.net
緣由是若是不用copy的方法,直接引用會對資源文件進行修改,而android是不容許在代碼裏修改res文件裏的圖片rest