国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程之截屏實現方法(包括scrollview與listview)

2020-04-11 11:15:37
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程之截屏實現方法。分享給大家供大家參考,具體如下:

public class ScreenShot { // 獲取指定Activity的截屏,保存到png文件 public static Bitmap takeScreenShot(Activity activity) {  // View是你需要截圖的View  View view = activity.getWindow().getDecorView();  view.setDrawingCacheEnabled(true);  view.buildDrawingCache();  Bitmap b1 = view.getDrawingCache();  // 獲取狀態欄高度  Rect frame = new Rect();  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);  int statusBarHeight = frame.top;  System.out.println(statusBarHeight);  // 獲取屏幕長和高  int width = activity.getWindowManager().getDefaultDisplay().getWidth();  int height = activity.getWindowManager().getDefaultDisplay()    .getHeight();  // 去掉標題欄  // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);  Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height    - statusBarHeight);  view.destroyDrawingCache();  savePic(b, "/sdcard/screen_test.png");  return b; } // 保存到sdcard public static void savePic(Bitmap b, String strFileName) {  FileOutputStream fos = null;  try {   fos = new FileOutputStream(strFileName);   if (null != fos) {    b.compress(Bitmap.CompressFormat.PNG, 90, fos);    fos.flush();    fos.close();   }  } catch (FileNotFoundException e) {   e.printStackTrace();  } catch (IOException e) {   e.printStackTrace();  } } /**  * 把View對象轉換成bitmap  * */ public static Bitmap convertViewToBitmap(View view) {  view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));  view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  view.buildDrawingCache();  Bitmap bitmap = view.getDrawingCache();  if (bitmap != null) {   System.out.println("這不是nullde1");   Log.d("nullde1", "nullde1");  } else {   System.out.println("這nullnulllnulnlul");  }  return bitmap; } // 程序入口1 public static void shoot(Activity a) {  ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/sdcard/screen_test.png"); } // 程序入口2 public static void shootView(View view) {  ScreenShot.savePic(ScreenShot.convertViewToBitmap(view),    "sdcard/xx.png"); } public static Bitmap getViewBitmap(View v) {  v.clearFocus();  v.setPressed(false);  boolean willNotCache = v.willNotCacheDrawing();  v.setWillNotCacheDrawing(false);  // Reset the drawing cache background color to fully transparent  // for the duration of this operation  int color = v.getDrawingCacheBackgroundColor();  v.setDrawingCacheBackgroundColor(0);  if (color != 0) {   v.destroyDrawingCache();  }  v.buildDrawingCache();  Bitmap cacheBitmap = v.getDrawingCache();  if (cacheBitmap == null) {   Log.e("TTTTTTTTActivity", "failed getViewBitmap(" + v + ")",     new RuntimeException());   return null;  }  Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);  // Restore the view  v.destroyDrawingCache();  v.setWillNotCacheDrawing(willNotCache);  v.setDrawingCacheBackgroundColor(color);  return bitmap; } /**  * 截取scrollview的屏幕  * **/ public static Bitmap getBitmapByView(ScrollView scrollView) {  int h = 0;  Bitmap bitmap = null;  // 獲取listView實際高度  for (int i = 0; i < scrollView.getChildCount(); i++) {   h += scrollView.getChildAt(i).getHeight();   scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);  }  Log.d(TAG, "實際高度:" + h);  Log.d(TAG, " 高度:" + scrollView.getHeight());  // 創建對應大小的bitmap  bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,    Bitmap.Config.ARGB_8888);  final Canvas canvas = new Canvas(bitmap);  scrollView.draw(canvas);  // 測試輸出  FileOutputStream out = null;  try {   out = new FileOutputStream("/sdcard/screen_test.png");  } catch (FileNotFoundException e) {   e.printStackTrace();  }  try {   if (null != out) {    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);    out.flush();    out.close();   }  } catch (IOException e) {   // TODO: handle exception  }  return bitmap; } private static String TAG = "Listview and ScrollView item 截圖:"; /**  * 截圖listview  * **/ public static Bitmap getbBitmap(ListView listView) {  int h = 0;  Bitmap bitmap = null;  // 獲取listView實際高度  for (int i = 0; i < listView.getChildCount(); i++) {   h += listView.getChildAt(i).getHeight();  }  Log.d(TAG, "實際高度:" + h);  Log.d(TAG, "list 高度:" + listView.getHeight());  // 創建對應大小的bitmap  bitmap = Bitmap.createBitmap(listView.getWidth(), h,    Bitmap.Config.ARGB_8888);  final Canvas canvas = new Canvas(bitmap);  listView.draw(canvas);  // 測試輸出  FileOutputStream out = null;  try {   out = new FileOutputStream("/sdcard/screen_test.png");  } catch (FileNotFoundException e) {   e.printStackTrace();  }  try {   if (null != out) {    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);    out.flush();    out.close();   }  } catch (IOException e) {   // TODO: handle exception  }  return bitmap; }}

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 肃宁县| 吉林省| 滨州市| 金坛市| 丰镇市| 景泰县| 漳平市| 西峡县| 文昌市| 琼结县| 灵川县| 青川县| 广东省| 区。| 贵港市| 原阳县| 天台县| 德江县| 吉林省| 西林县| 永昌县| 凤山县| 札达县| 蓝山县| 镇安县| 紫云| 连南| 红桥区| 曲阜市| 井研县| 沂南县| 秭归县| 三河市| 连州市| 上虞市| 日喀则市| 明星| 磐安县| 岐山县| 宜州市| 定陶县|