在實際應用中,經常需要對圖片進行處理,包括壓縮、截圖等等,其實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() {
String name = System.currentTimeMillis() + ".jpg";
return name;
}
}
布局文件:
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/main_btn_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相冊" />
<Button
android:id="@+id/main_btn_camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="相機" />
</LinearLayout>
<ImageView
android:id="@+id/main_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp" />
</LinearLayout>
加上權限:
[html]
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>