图片的合成
1 package cn.itcast.bitmapcopy; 2 3 import java.io.FileNotFoundException; 4 5 import android.app.Activity; 6 import android.content.Intent; 7 import android.graphics.Bitmap; 8 import android.graphics.BitmapFactory; 9 import android.graphics.Canvas; 10 import android.graphics.ColorMatrix; 11 import android.graphics.ColorMatrixColorFilter; 12 import android.graphics.Matrix; 13 import android.graphics.Paint; 14 import android.graphics.PorterDuffXfermode; 15 import android.net.Uri; 16 import android.os.Bundle; 17 import android.view.View; 18 import android.widget.ImageView; 19 20 public class DemoActivity extends Activity { 21 private ImageView iv; 22 23 @Override 24 public void onCreate(Bundle savedInstanceState) { 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.main); 27 iv = (ImageView) findViewById(R.id.iv); 28 29 } 30 31 32 public void selectImage(View view){ 33 Intent intent = new Intent(); 34 intent.setAction("android.intent.action.PICK"); 35 intent.addCategory("android.intent.category.DEFAULT"); 36 intent.setType("image/*"); 37 startActivityForResult(intent, 0); 38 } 39 40 @Override 41 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 42 try { 43 if (data != null) { 44 // 得到点击图片的uri 45 Uri uri = data.getData(); 46 //原图 47 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 48 49 //原图的拷贝 50 Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig()); 51 //以拷贝图片的为模板创建一个画布 52 Canvas canvas = new Canvas(alterBitmap); 53 Matrix matrix = new Matrix(); 54 55 Paint paint = new Paint(); 56 57 Bitmap logo = BitmapFactory.decodeResource(getResources(), R.raw.ic_launcher); 58 59 60 canvas.drawBitmap(bitmap, matrix, paint); 61 //指定了两个图片合成的方式 62 paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_OVER)); 63 64 canvas.drawBitmap(logo, matrix, paint); 65 66 67 iv.setImageBitmap(alterBitmap); 68 } 69 } catch (FileNotFoundException e) { 70 e.printStackTrace(); 71 } 72 super.onActivityResult(requestCode, resultCode, data); 73 } 74 }
效果就是两张图片合成一张图片