-
Android屏幕截图,得到截图存储在SD卡上。
-
所截取的图,实时显示在屏幕上。用户点击图片——消失;用户不点击,指定时间后——图片自动消失。
感性认识:
抛砖引玉,详见注释:
1
2
3
4
5
6
7
8
9
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
|
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.about_me:
//获取屏幕
View targetView = mActivity.getWindow().getDecorView();
targetView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
//截全屏
targetView.setDrawingCacheEnabled(
true
);
Bitmap fullBitmap = Bitmap.createBitmap(targetView.getDrawingCache());
/**获取状态栏高度
Rect frame = new Rect();
mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;*/
DisplayMetrics dm = mActivity.getResources().getDisplayMetrics();
/** 去掉标题栏
Bitmap bitmap = Bitmap.createBitmap(fullBitmap, 0, statusBarHeight, dm.widthPixels, dm.heightPixels - statusBarHeight);
targetView.destroyDrawingCache();*/
OutputStream output =
null
;
try
{
if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String sdPath = Environment.getExternalStorageDirectory().getPath();
//sd卡路径
String pngPath = sdPath +
"/Remind/"
;
//截图存放路径
File pngDir =
new
File(pngPath);
if
(!pngDir.exists()) {
pngDir.mkdir();
//make路径
}
String pngName = pngPath + System.currentTimeMillis() +
".png"
;
output =
new
FileOutputStream(pngName);
if
(output !=
null
) {
fullBitmap.compress(Bitmap.CompressFormat.PNG,
100
, output);
//100%高清大图
output.flush();
output.close();
}
/** WindowManager */
LayoutInflater inflater = LayoutInflater.from(mActivity);
screenShot = inflater.inflate(R.layout.screen_shot,
null
);
wmManager =(WindowManager) mActivity.getSystemService(mActivity.WINDOW_SERVICE);
WindowManager.LayoutParams wmParams =
new
WindowManager.LayoutParams();
ImageView shotImg = (ImageView) screenShot.findViewById(R.id.screen_shot_img);
shotImg.setImageBitmap(targetView.getDrawingCache());
/**
*以下都是WindowManager.LayoutParams的相关属性
*/
wmParams.type=WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
//这里是关键
wmParams.format=PixelFormat.RGBA_8888;
wmParams.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
wmParams.alpha =
0
.8f;
//透明度
//WindowManager位置
wmParams.width = dm.widthPixels *
1
/
2
;
wmParams.height = dm.heightPixels *
1
/
2
;
wmParams.x = dm.widthPixels *
1
/
2
;
wmParams.y = - (dm.heightPixels *
1
/
5
);
//添加View
wmManager.addView(screenShot, wmParams);
isRemoved =
false
;
//计时器
timer =
new
Timer();
//点击图片,消失。
shotImg.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg0) {
if
(wmManager !=
null
&& !isRemoved) {
wmManager.removeView(screenShot);
isRemoved =
true
;
if
(timer !=
null
) {
timer.cancel();
}
}
}
});
//定时任务,显示3秒后自动消失。
timer.schedule(
new
TimerTask() {
@Override
public
void
run() {
if
(wmManager !=
null
&& !isRemoved) {
wmManager.removeView(screenShot);
isRemoved =
true
;
}
}
},
3000
);
}
}
catch
(Exception e) {
e.printStackTrace();
}
break
;
default
:
break
;
}
}
|
本文转自chainli 51CTO博客,原文链接:http://blog.51cto.com/lichen/1218567,如需转载请自行联系原作者