android图像编辑和处理(四)

通过在Canvas对象上绘制一个位图对象,然后在相同的Canvas上绘制第二个图像来合成。区别在于绘制第二个图像时,需要再Paint对象上指定一个过渡模式(Xfermode)。

示例代码如下:

package com.example.testphotoedit;

import java.io.FileNotFoundException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Picturecomposite extends Activity implements OnClickListener {

	private Button bt1, bt2;
	private ImageView composite;
	private static final int PICKED_ONE = 1;
	private static final int PICKED_TWO = 2;
	private static boolean onePicked=false;
	private static boolean twoPicked=false;
	
	private Bitmap bmp1,bmp2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		bt1 = (Button) findViewById(R.id.picture_1);
		bt2 = (Button) findViewById(R.id.picture_2);
		composite = (ImageView) findViewById(R.id.composite_all);
		bt1.setOnClickListener(this);
		bt2.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int which = -1;
		switch (v.getId()) {
		case R.id.picture_1:
			which = PICKED_ONE;
			break;
		case R.id.picture_2:
			which = PICKED_TWO;
			break;
		}

		Intent intent = new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		startActivityForResult(intent, which);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Uri imageFileUri = data.getData();
			if(requestCode==PICKED_ONE){
				bmp1=loadBitmap(imageFileUri);
				onePicked=true;
			}else if(requestCode==PICKED_TWO){
				bmp2=loadBitmap(imageFileUri);
				twoPicked=true;
			}
		}
		//如果选择了两幅图像,且两个位图均已完成实例化,继续合成操作
		if(onePicked&&twoPicked){
			Bitmap alteredBitmap = Bitmap.createBitmap(bmp1.getWidth(),
					bmp1.getHeight(), bmp1.getConfig());//创建一个空的位图对象,大小和配置与第一个位图对象(bmp1)相同
			Canvas canvas=new Canvas(alteredBitmap);
			Paint  paint=new Paint();
			canvas.drawBitmap(bmp1, 0, 0, paint);
			paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));//实例化对象
			canvas.drawBitmap(bmp2, 0, 0, paint);
			composite.setImageBitmap(alteredBitmap);
		}
	}

	@SuppressWarnings("deprecation")
	private Bitmap loadBitmap(Uri imageFileUri) {
		Display currentDisply = getWindowManager().getDefaultDisplay();
		int dw = currentDisply.getWidth() / 2 - 100;
		int dh = currentDisply.getHeight() / 2 - 100;

		try {
			BitmapFactory.Options bmpFactory = new BitmapFactory.Options();
			bmpFactory.inJustDecodeBounds = true;// 加载图像的尺寸而非图像本身
			Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
					.openInputStream(imageFileUri), null, bmpFactory);

			int heightRatio = (int) Math
					.ceil(bmpFactory.outHeight / (float) dh);
			int widthRatio = (int) Math.ceil(bmpFactory.outWidth / (float) dw);

			if (heightRatio > 1 && widthRatio > 1) {
				if (heightRatio > widthRatio) {
					bmpFactory.inSampleSize = heightRatio;
				} else {
					bmpFactory.inSampleSize = widthRatio;
				}
			}

			bmpFactory.inJustDecodeBounds = false;// 加载真实的图像
			bmp = BitmapFactory.decodeStream(getContentResolver()
					.openInputStream(imageFileUri), null, bmpFactory);
			return bmp;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}

activity_main.xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button 
        android:id="@+id/picture_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="choose Picture1"/>
    <Button 
        android:id="@+id/picture_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/picture_1"
        android:text="choose Picture2"/>
    <ImageView 
        android:id="@+id/composite_all"
        android:layout_below="@+id/picture_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:contentDescription="@null"/>
</RelativeLayout>


运行结果如下:




 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值