Android图片剪裁的实现

简介: MainActivity如下: package cn.testphotocrop;import android.net.Uri;import android.

MainActivity如下:

package cn.testphotocrop;
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;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
 * Demo描述:
 * 1 从系统图片库中选取照片进行显示
 * 2 从系统图片库中选取照片进行剪切然后显示
 * 
 * 延伸阅读:
 * 自定义图片剪裁
 * http://blog.csdn.net/liuhanhan512/article/details/8308525
 */
public class MainActivity extends Activity {
    private Button mSelectButton;
    private Button mCropButton;
    private ImageView mImageView;
    private final int SELECT_PHOTO=9527;
    private final int CROP_PHOTO=7;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mSelectButton=(Button) findViewById(R.id.selectorButton);
    	mSelectButton.setOnClickListener(new ClickListenerImpl());
    	mCropButton=(Button) findViewById(R.id.cropButton);
    	mCropButton.setOnClickListener(new ClickListenerImpl());
    	mImageView=(ImageView) findViewById(R.id.imageView);
    }
   private class ClickListenerImpl implements OnClickListener{
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.selectorButton:
				Intent intent_select = 
				new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
			    startActivityForResult(intent_select, SELECT_PHOTO);  
				break;
			case R.id.cropButton:
				Intent intent_crop = getImageCropIntent();
				startActivityForResult(intent_crop, CROP_PHOTO);  
				break;

			default:
				break;
		}
	}
   }
   
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode==SELECT_PHOTO) {
			try {
				Uri imageUri=data.getData();
				Bitmap bitmap=BitmapFactory.decodeStream
				(getContentResolver().openInputStream(imageUri), null, null);
				mImageView.setImageBitmap(bitmap);
			} catch (Exception e) {
				
			}
		}
		if (requestCode==CROP_PHOTO) {
			//注意:
			//此处getParcelableExtra("data")中的"data"标签应该与
			//getImageCropIntent()方法中的 intent.putExtra("return-data", true)处
			//"return-data"里的"data"保持一致
			Bitmap bitmap = data.getParcelableExtra("data");   
		    mImageView.setImageBitmap(bitmap);  
		}
	}

	private Intent getImageCropIntent() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		intent.setType("image/*");
		intent.putExtra("crop", "true");
		intent.putExtra("aspectX", 1);
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 80);
		intent.putExtra("outputY", 80);
		intent.putExtra("return-data", true);
		return intent;
	}  

	
}


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/selectorButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择照片"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dip"
    />
    
    <Button
        android:id="@+id/cropButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="剪裁照片"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dip"
    />
    <ImageView 
        android:id="@+id/imageView"
        android:layout_below="@id/cropButton"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
     />

</RelativeLayout>


 

相关文章
|
4月前
|
XML Java Android开发
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
Android Studio App开发之对图片进行简单加工(包括放缩,旋转等等 附源码)
45 0
|
4月前
|
XML Java Android开发
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
181 0
|
7月前
|
存储 编解码 Android开发
Android关于图片方向问题
Android关于图片方向问题
41 0
|
4月前
|
XML JSON Java
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
Android App开发即时通信中通过SocketIO在客户端与服务端间传输文本和图片的讲解及实战(超详细 附源码)
72 0
|
24天前
|
Android开发
Android保存图片到相册(适配android 10以下及以上)
Android保存图片到相册(适配android 10以下及以上)
22 1
|
6月前
|
SQL 人工智能 移动开发
Android etc1tool之png图片转换pkm 和 zipalign简介
etc1tool 是一种命令行实用程序,可用于将 PNG 图片编码为 ETC1 压缩标准格式(PKM),并将 ETC1 压缩图片解码回 PNG。
|
8月前
|
Java Android开发
Android 保存资源图片到相册最新写法适用于Android10.0及以上
Android 保存资源图片到相册最新写法适用于Android10.0及以上
587 0
|
8月前
|
SQL 数据库 Android开发
Android 访问系统相册选中图片,并返回该图片的路径
Android 访问系统相册选中图片,并返回该图片的路径
100 0
|
4月前
|
API Android开发
[Android]图片加载库Glide
[Android]图片加载库Glide
55 0
|
4月前
|
Android开发
[Android]制作9-Patch图片
[Android]制作9-Patch图片
42 0