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

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.