Android屏幕截图,得到截图存储在SD卡上。
所截取的图,实时显示在屏幕上。用户点击图片――消失;用户不点击,指定时间后――图片自动消失。
感性认识:
抛砖引玉,详见注释:
@Overridepublic 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; }}
本文出自 “” 博客,请务必保留此出处