-
Android控件自帶View緩存功能
首先
v.getRootView() == getWindow().getDecorView()//return true rootView.setDrawingCacheEnabled(true);//設(shè)置允許緩存 view.buildDrawingCache();//創(chuàng)建緩存 Bitmap bitmap = view.getDrawingCache();//獲取緩存
但是描扯,此時(shí)通過得到rootview可以得到bitmap 形葬,但是通過某個(gè)控件獲取比如scrollview 可能bitmap返回null (google允許緩存最大cache為屏幕像素width * height * 4,超過不構(gòu)建)
- slove
通過限定寬高(局限是無法得到長(zhǎng)圖)
view.measure(View.MeasureSpec.makeMeasureSpec(
0,View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(
0,View.MeasueSpec.UNSPECIFIED));
// view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());//設(shè)置擺放寬高
通過view.draw()來繪制到畫布保存圖片(此時(shí)長(zhǎng)圖就可以得到啦~~??)
public Bitmap loadBitmapFromView(View v, boolean isParemt) {
if (v == null) {
return null;
}
Bitmap screenshot;
screenshot = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
// c.translate(-v.getScrollX(), -v.getScrollY()); //添加截取當(dāng)前位置開始
v.draw(c);
return screenshot;
}
-
最后保存圖片
private boolean saveBitmap(Bitmap bitmap, String name) { File file = new File(FileUtils.DEFAULT_PHOTODIR, name + ".jpg"); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } try { boolean isCreated = file.createNewFile(); Log.e("file", isCreated ? "創(chuàng)建成功" : "create fail"); FileOutputStream fos = new FileOutputStream(file); boolean compress = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); Log.e("file", compress + "圖片保存結(jié)果"); fos.flush(); fos.close(); return true; } catch (IOException e) { e.printStackTrace(); } return false; }
長(zhǎng)圖大功告成击敌!
···系統(tǒng)命令截圖
-
調(diào)用系統(tǒng)截圖
需要cmd指令 adb shell screencap -p storage/emulated/0/DCIM/Camera/testView.jpg(調(diào)用此命令時(shí)费封,系統(tǒng)自動(dòng)截取當(dāng)前屏幕焕妙,app調(diào)用需要系統(tǒng)權(quán)限)Runtime. getRuntime().exec("screencap -p " + mSavedPath); //不過app獲取系統(tǒng)cmd權(quán)限非常麻煩