ImageView实现图片的剪切

(用真机测试比较好,模拟器虽说可以不过还是不好测试的)

选取图片,然后选取剪切图片(裁剪后的图片可以明显的看出与原图不同)


layout.xml布局代码如下

<LinearLayout 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="wrap_content"
        android:layout_height="wrap_content"
        android:text="选取图片" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="剪切后的图片" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

在MainActivity.java下,

首先定义必要的变量,这里有两个静态的整形变量,主要用于意图的返回的标志;

设置按钮的监听器:当单机选取图片按钮的时候呢,可以提取手机的图片并且进行选择图片的功能,这里自动搜说手机的图库;声明一个意图,   Intent intent1 = new Intent(
     Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//就是说从内容提供者当中的媒体存储的图片的途经路径去提取,然后传递一下startActivityForResult(intent1, IMAGE_SELECT);

当单击剪切按钮时,这里也声明一个意图,这里新建一个方法,Intent intent2 = getImageClipIntent();具体内容在下面的代码中。

这一块已经可以实现一部分的功能了(获取手机图库的图片)



然后再回到  onActivityResult 方法当中,去处理图片能按照手机的屏幕大小显示。这里有一个比较重要的匿名内部类,BitmapFactory.Options 就是用来实现对图片的剪切,在BitmapFactory.Options 下,有一个inJustDecodeBounds,这个API比较复杂(API意思:如果设置为true,译码器将返回Null,但是允许调用者来查询图而无需为其像素分配内存。PS:这里我也不是好懂)(具体到代码当中)


package com.example.ll_imageview_cat;

import android.net.Uri;
import android.os.Bundle;
import android.R.anim;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Spannable.Factory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener{

	private Button button1 = null;
	private Button button2 = null;
	private ImageView imageView = null;

	// 两个静态的整形变量,主要用于意图的返回的标志
	private static final int IMAGE_SELECT = 1;
	private static final int IMAGE_CUT = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.button2);
		imageView = (ImageView) findViewById(R.id.image);

		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		 
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.button1:
			// 如何提取手机的图片。并且进行选择图片的功能
			// 什么一个Intent 意图
			Intent intent1 = new Intent(
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			//上面意图的意思呢,就是说从内容提供者当中的媒体存储的图片的途经路径去提取
			startActivityForResult(intent1, IMAGE_SELECT);
			break;
		case R.id.button2:
			Intent intent2 = getImageClipIntent();
			startActivityForResult(intent2, IMAGE_CUT);
			break;
		}
	}

	private Intent getImageClipIntent() {
		// TODO Auto-generated method stub
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		//实现对图片的裁剪,必须要设置图片的属性和大小
		intent.setType("image/*"); //可以获取任意的图片类型
		intent.putExtra("crop", "true"); //滑动选中图片的区域
		intent.putExtra("aspectX", 1); //表示剪切框的比例1:1的效果
		intent.putExtra("aspectY", 1);
		intent.putExtra("outputX", 80);//指定输出图片的大小
		intent.putExtra("return-data", true);
		return intent;
	}
		
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			//处理图片按照手机的屏幕大小显示
			if(requestCode == IMAGE_SELECT){
				Uri uri = data.getData();  // 获得图片的路径
				int dw = getWindowManager().getDefaultDisplay().getWidth();//获得屏幕的狂赌
				int dh = getWindowManager().getDefaultDisplay().getHeight()/2; //高度除以2,
				try{
					//实现对图片的剪切的类,是一个匿名内部类
					 BitmapFactory.Options factory = new BitmapFactory.Options();
					 factory.inJustDecodeBounds  = true; //如果设置为true,允许查询图片不是按照像素分配的内存
					 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, factory);
					//对图片的宽度和高度对应手机的屏幕进行匹配
					 
					 int hRatio = (int)Math.ceil(factory.outHeight/(float)dh);
					 //如果大于1,表示图片的高度大于手机屏幕的高度
					 
					 int wRatio = (int)Math.ceil(factory.outWidth/(float)dw);
					 //如果大于1,表示图片的宽度大于手机屏幕的高度
					 
					//缩放到1/radio的尺寸和1/radio^2的像素
					 if(hRatio > 1 || wRatio > 1){
						 if(hRatio > wRatio){
							 factory.inSampleSize = hRatio;
							 }else {
								 factory.inSampleSize = wRatio;
							 }
					 }
					 factory.inJustDecodeBounds = false;
					 //使用BitmapFactory对图片进行适合屏幕的操作
					 bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri),null,factory);
					 imageView.setImageBitmap(bitmap);
 				}catch (Exception e){
					
				}
			}else if(requestCode == IMAGE_CUT){
				Bitmap bitmap = data.getParcelableExtra("data");
				imageView.setImageBitmap(bitmap);
			}
		}
	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}



}







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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值