在实际应用中,经常需要对图片进行处理,包括压缩、截图等等,其实android系统提供了一个可以截图的activity,我们只需调用它就行了,下面示例完成一个通过相机照相或相册选取图片后对其截图的功能,其实知道图片的路劲都可以调用截图activity,代码如下:
测试activity:
[java]
package com.home.testclipimage;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class TestClipImageActivity extends Activity implements OnClickListener {
private Button gallaryBtn;
private Button cameraBtn;
private ImageView showImage;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallaryBtn = (Button) findViewById(R.id.main_btn_gallery);
cameraBtn = (Button) findViewById(R.id.main_btn_camera);
gallaryBtn.setOnClickListener(this);
cameraBtn.setOnClickListener(this);
showImage = (ImageView) findViewById(R.id.main_image);
}
@Override
public void onClick(View v) {
if (v == gallaryBtn) {
// 跳转至相册界面
Intent intent = new Intent(Intent.ACTION_PICK,
Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}
if (v == cameraBtn) {
if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
Toast.makeText(this, "请插入SD卡", Toast.LENGTH_SHORT).show();
return;
}
// 跳转至相机界面
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
getPhotoName());
// 指定相机拍照后相片的存储位置
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 1);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 系统相册返回
if (requestCode == 0 && resultCode == Activity.RESULT_OK
&& data != null) {
startPhotoZoom(data.getData(), 150);// 截图
}
// 系统相机返回
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
startPhotoZoom(Uri.fromFile(file), 150);// 截图
}
// 截图后返回
if (requestCode == 2 && data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
Bitmap bitmap = bundle.getParcelable("data");
showImage.setBackgroundDrawable(new BitmapDrawable(bitmap));
}
}
}
/**
* 跳转至系统截图界面进行截图
*
* @param data
* @param size
*/
private void startPhotoZoom(Uri data, int size) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(data, "image/*");
// crop为true时表示显示的view可以剪裁
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);
}
/**
* 产生照片名称
*
* @return
*/
private String getPhotoName() {
补充:移动开发 , Android ,