Android屏幕截圖,得到得截圖存儲在SD卡上。
所截取的圖,實時顯示在屏幕上。用戶點擊圖片——消失;用戶不點擊,指定時間後——圖片自動消失。
感性認識:
拋磚引玉,詳見注釋:
@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;
}
}
更多Fedora相關信息見Fedora 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=5