Android图片的合成

简介: mainActivity如下: package c.c; import android.app.Activity; import android.

mainActivity如下:

package c.c;

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.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/**
 * 需求描述:
 * 从相册中选取两张图片合成一张图片
 * 
 * 小结:
 * 1 依据图片的Uri加载图片
 * mBitmap1=BitmapFactory.decodeStream
 * (getContentResolver().openInputStream(photoFileUri));
 * 为什么使用getContentResolver呢?因为在相册本身是一个内容提供者
 * 2 注意构造方法:
 * Canvas canvas=new Canvas(bitmap);
 * 为什么要传入一个bitmap呢?
 * 我们画东西肯定是在画布上画.
 * 但是在这里传入一个bitmap
 * 即该bitmap是一个载体,相当于把bitmap作为了画布.
 * 当然本质上是:把东西画在了canvas里的bitmap上
 * 也可以这么想:
 * 我们最后不是要用mImageView.setImageBitmap(参数是一个Bitmap)来显示
 * 一张图片么?所以在构造Canvas的时候传入了一个Bitmap.然后把两张图片
 * 画到了该Bitmap上.最后该Bitmap就是两张照片的合成图.
 * 所以当然要把两张图片画到一个Bitmap上
 */
public class MainActivity extends Activity {
	private static int REQUESTCODE=0;
	private final static int PICK_FIRST_PHOTO=1;
	private final static int PICK_SECOND_PHOTO=2;
	private boolean isPickedFirstPhoto=false;
	private boolean isPickedSecondPhoto=false;
    private Button mButton1;
    private Button mButton2;
    private Bitmap mBitmap1;
    private Bitmap mBitmap2;
    private ImageView mImageView;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    private void init(){
    	mButton1=(Button) findViewById(R.id.button1);
    	mButton1.setOnClickListener(new ButtonOnClickListenerImpl());
    	mButton2=(Button) findViewById(R.id.button2);
    	mButton2.setOnClickListener(new ButtonOnClickListenerImpl());
    	mImageView=(ImageView) findViewById(R.id.imageView);
    }
    private class ButtonOnClickListenerImpl implements OnClickListener{
		public void onClick(View v) {
			if (v.getId()==R.id.button1) {
				REQUESTCODE=PICK_FIRST_PHOTO;
			} else {
				REQUESTCODE=PICK_SECOND_PHOTO;
			}
			Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(intent, REQUESTCODE);
		}
    	
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if (resultCode==RESULT_OK) {
    		Uri photoFileUri=data.getData();
			if (requestCode==PICK_FIRST_PHOTO) {
				try {
					mBitmap1=BitmapFactory.decodeStream
					(getContentResolver().openInputStream(photoFileUri));
					isPickedFirstPhoto=true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (requestCode==PICK_SECOND_PHOTO) {
				try {
					mBitmap2=BitmapFactory.decodeStream
					(getContentResolver().openInputStream(photoFileUri));
					isPickedSecondPhoto=true;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (isPickedFirstPhoto&&isPickedSecondPhoto) {
				Bitmap bitmap=
				Bitmap.createBitmap(mBitmap1.getWidth(), mBitmap1.getHeight(), mBitmap1.getConfig());
				Canvas canvas=new Canvas(bitmap);
				Paint paint=new Paint();
				canvas.drawBitmap(mBitmap1, 0, 0, paint);
				paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.MULTIPLY));
				canvas.drawBitmap(mBitmap2, 0, 0,paint);
				mImageView.setImageBitmap(bitmap);
				
			}

		}
    }
}


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/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="选择第一张图片"
         />
    <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button1"
        android:text="选择第二张图片"
     />
    <ImageView 
      android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/button2"
     />

</RelativeLayout>


 

相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
87 0
|
6天前
|
存储 缓存 编解码
Android经典面试题之图片Bitmap怎么做优化
本文介绍了图片相关的内存优化方法,包括分辨率适配、图片压缩与缓存。文中详细讲解了如何根据不同分辨率放置图片资源,避免图片拉伸变形;并通过示例代码展示了使用`BitmapFactory.Options`进行图片压缩的具体步骤。此外,还介绍了Glide等第三方库如何利用LRU算法实现高效图片缓存。
37 20
Android经典面试题之图片Bitmap怎么做优化
|
4月前
|
Android开发
Android通过手势(多点)缩放和拖拽图片
Android通过手势(多点)缩放和拖拽图片
42 4
|
4月前
|
Java Android开发
android 下载图片的问题
android 下载图片的问题
35 3
|
1月前
|
数据处理 开发工具 数据安全/隐私保护
Android平台RTMP推送|轻量级RTSP服务|GB28181接入之文字、png图片水印的精进之路
本文探讨了Android平台上推流模块中添加文字与PNG水印的技术演进。自2015年起,为了满足应急指挥及安防领域的需求,逐步发展出三代水印技术:第一代为静态文字与图像水印;第二代实现了动态更新水印内容的能力,例如实时位置与时间信息;至第三代,则优化了数据传输效率,直接使用Bitmap对象传递水印数据至JNI层,减少了内存拷贝次数。这些迭代不仅提升了用户体验和技术效率,也体现了开发者追求极致与不断创新的精神。
|
1月前
|
自然语言处理 定位技术 API
Android经典实战之如何获取图片的经纬度以及如何根据经纬度获取对应的地点名称
本文介绍如何在Android中从图片提取地理位置信息并转换为地址。首先利用`ExifInterface`获取图片内的经纬度,然后通过`Geocoder`将经纬度转为地址。注意操作需在子线程进行且考虑多语言支持。
110 4
|
22天前
|
存储 监控 数据库
Android经典实战之OkDownload的文件分段下载及合成原理
本文介绍了 OkDownload,一个高效的 Android 下载引擎,支持多线程下载、断点续传等功能。文章详细描述了文件分段下载及合成原理,包括任务创建、断点续传、并行下载等步骤,并展示了如何通过多种机制保证下载的稳定性和完整性。
25 0
|
1月前
|
XML 前端开发 Android开发
Android经典实战之Kotlin中实现圆角图片和圆形图片
本文介绍两种实现圆角图像视图的方法。第一种是通过自定义Kotlin `AppCompatImageView`,重写`onDraw`方法使用`Canvas`和`Path`进行圆角剪裁。第二种利用Android Material库中的`ShapeableImageView`,简单配置即可实现圆角效果。两种方法均易于实现且提供动态调整圆角半径的功能。
29 0
|
3月前
|
JSON 编解码 Apache
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
43 1
|
3月前
|
Java Android开发
18. 【Android教程】图片控件 ImageView
18. 【Android教程】图片控件 ImageView
50 4