android 自定义裁切,android 设置头像以及裁剪功能

这是一个Android应用的MainActivity代码示例,展示了如何实现从相册选择或使用相机拍照设置头像的功能。代码包括点击事件监听、启动相册和相机应用、裁剪图片并显示在ImageView上等步骤。
摘要由CSDN通过智能技术生成

复制内容到剪贴板

代码:package com.xzw.picture;

import java.io.File;

import com.xzw.utils.Tools;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.content.Intent;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.view.View;

import android.view.Window;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.Toast;

/**

*

* @author XuZhiwei (xuzw13@gmail.com)

* Create at 2012-8-17 上午10:14:40

*/

public class MainActivity extends Activity {

/*组件*/

private RelativeLayout switchAvatar;

private ImageView faceImage;

private String[] items = new String[] { "选择本地图片", "拍照" };

/*头像名称*/

private static final String IMAGE_FILE_NAME = "faceImage.jpg";

/* 请求码*/

private static final int IMAGE_REQUEST_CODE = 0;

private static final int CAMERA_REQUEST_CODE = 1;

private static final int RESULT_REQUEST_CODE = 2;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题

setContentView(R.layout.main);

switchAvatar = (RelativeLayout) findViewById(R.id.switch_face_rl);

faceImage = (ImageView) findViewById(R.id.face);

//设置事件监听

switchAvatar.setOnClickListener(listener);

}

private View.OnClickListener listener = new View.OnClickListener() {

@Override

public void onClick(View v) {

showDialog();

}

};

/**

* 显示选择对话框

*/

private void showDialog() {

new AlertDialog.Builder(this)

.setTitle("设置头像")

.setItems(items, new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

switch (which) {

case 0:

Intent intentFromGallery = new Intent();

intentFromGallery.setType("image/*"); // 设置文件类型

intentFromGallery

.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(intentFromGallery,

IMAGE_REQUEST_CODE);

break;

case 1:

Intent intentFromCapture = new Intent(

MediaStore.ACTION_IMAGE_CAPTURE);

// 判断存储卡是否可以用,可用进行存储

if (Tools.hasSdcard()) {

intentFromCapture.putExtra(

MediaStore.EXTRA_OUTPUT,

Uri.fromFile(new File(Environment

.getExternalStorageDirectory(),

IMAGE_FILE_NAME)));

}

startActivityForResult(intentFromCapture,

CAMERA_REQUEST_CODE);

break;

}

}

})

.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();

}

}).show();

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case IMAGE_REQUEST_CODE:

startPhotoZoom(data.getData());

break;

case CAMERA_REQUEST_CODE:

if (Tools.hasSdcard()) {

File tempFile = new File(

Environment.getExternalStorageDirectory()

+ IMAGE_FILE_NAME);

startPhotoZoom(Uri.fromFile(tempFile));

} else {

Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!",

Toast.LENGTH_LONG).show();

}

break;

case RESULT_REQUEST_CODE:

if (data != null) {

getImageToView(data);

}

break;

}

super.onActivityResult(requestCode, resultCode, data);

}

/**

* 裁剪图片方法实现

*

* @param uri

*/

public void startPhotoZoom(Uri uri) {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, "image/*");

// 设置裁剪

intent.putExtra("crop", "true");

// aspectX aspectY 是宽高的比例

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

// outputX outputY 是裁剪图片宽高

intent.putExtra("outputX", 320);

intent.putExtra("outputY", 320);

intent.putExtra("return-data", true);

startActivityForResult(intent, 2);

}

/**

* 保存裁剪之后的图片数据

*

* @param picdata

*/

private void getImageToView(Intent data) {

Bundle extras = data.getExtras();

if (extras != null) {

Bitmap photo = extras.getParcelable("data");

Drawable drawable = new BitmapDrawable(photo);

faceImage.setImageDrawable(drawable);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值