截图这功能有时候很有必要,例如头像的圆角处理,不同长宽的图片,圆角处理后圆角的幅度不一样,唯有统一所有头像图片的长宽,不统一也行,那就要经过很多判断和计算(我活这么大了还没见过腾讯qq上传头像不裁剪处理的)!
应该都能看得懂了,关键的我都注释了,布局我就不贴出来了,就一个Button和一个ImageView
应该都能看得懂了,关键的我都注释了,布局我就不贴出来了,就一个Button和一个ImageView
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package
com.example.screenshot;
import
java.io.File;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.DialogInterface;
import
android.content.Intent;
import
android.database.Cursor;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.net.Uri;
import
android.os.Bundle;
import
android.provider.MediaStore;
import
android.util.Log;
import
android.view.Menu;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.ArrayAdapter;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.ListAdapter;
public
class
MainActivity
extends
Activity
implements
OnClickListener{
Button cutBtn;
ImageView imageView;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cutBtn=(Button)findViewById(R.id.cutBtn);
imageView=(ImageView)findViewById(R.id.myImage);
cutBtn.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.cutBtn:
AlertDialog dialog;
String[] items = {
"本地相册"
,
"相机"
};
ListAdapter itemlist =
new
ArrayAdapter<String>(
this
,
android.R.layout.simple_dropdown_item_1line, items);
AlertDialog.Builder builder =
new
AlertDialog.Builder(
this
);
builder.setAdapter(itemlist,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
item) {
if
(item ==
0
) {
Intent intent =
new
Intent();
intent.setType(
"image/*"
);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,
100
);
}
else
{
Intent intent =
new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,
101
);
}
}
});
dialog = builder.create();
dialog.show();
break
;
default
:
break
;
}
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
switch
(requestCode) {
case
100
:
startPhotoZoom(data.getData());
break
;
case
101
:
startPhotoZoom(data.getData());
break
;
case
102
:
if
(data !=
null
){
Uri uri= Uri.parse(data.getAction());
Cursor cursor =
this
.getContentResolver().query(uri,
null
,
null
,
null
,
null
);
cursor.moveToFirst();
String path = cursor.getString(
1
);
Bitmap bitmap=
new
BitmapFactory().decodeFile(path);
imageView.setImageBitmap(bitmap);
}
break
;
default
:
break
;
}
}
/**
* 裁剪图片方法实现
* @param uri
*/
public
void
startPhotoZoom(Uri uri) {
Intent intent =
new
Intent(
"com.android.camera.action.CROP"
);
intent.setDataAndType(uri,
"image/*"
);
//下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra(
"crop"
,
"true"
);
// aspectX aspectY 是宽高的比例
intent.putExtra(
"aspectX"
,
1
);
intent.putExtra(
"aspectY"
,
1
);
// outputX outputY 是裁剪图片宽高
intent.putExtra(
"outputX"
,
150
);
intent.putExtra(
"outputY"
,
150
);
startActivityForResult(intent,
102
);
}
}
|