欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程之截屏實(shí)現(xiàn)方法(包括scrollview與listview)

 更新時(shí)間:2015年11月20日 14:45:25   作者:lee0oo0  
這篇文章主要介紹了Android編程之截屏實(shí)現(xiàn)方法,包括截取scrollview與listview屏幕的相關(guān)技巧,以及截屏圖片的生成與保存技巧,需要的朋友可以參考下

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

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();
  // 獲取狀態(tài)欄高度
  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();
  // 去掉標(biāo)題欄
  // 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對(duì)象轉(zhuǎn)換成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實(shí)際高度
  for (int i = 0; i < scrollView.getChildCount(); i++) {
   h += scrollView.getChildAt(i).getHeight();
   scrollView.getChildAt(i).setBackgroundResource(R.drawable.bg3);
  }
  Log.d(TAG, "實(shí)際高度:" + h);
  Log.d(TAG, " 高度:" + scrollView.getHeight());
  // 創(chuàng)建對(duì)應(yīng)大小的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實(shí)際高度
  for (int i = 0; i < listView.getChildCount(); i++) {
   h += listView.getChildAt(i).getHeight();
  }
  Log.d(TAG, "實(shí)際高度:" + h);
  Log.d(TAG, "list 高度:" + listView.getHeight());
  // 創(chuàng)建對(duì)應(yīng)大小的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;
 }
}

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Android獲取超級(jí)管理員權(quán)限的實(shí)現(xiàn)

    Android獲取超級(jí)管理員權(quán)限的實(shí)現(xiàn)

    這篇文章主要介紹了Android獲取超級(jí)管理員權(quán)限的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android GPS定位測試(附效果圖和示例)

    Android GPS定位測試(附效果圖和示例)

    本人做了GPS相關(guān)的嵌入式軟件已經(jīng)幾年了,所以說起要做個(gè)測試GPS定位模塊的程序,第一反應(yīng)就是串口讀取GPS模塊的數(shù)據(jù),然后解析GPS的NMEA格式數(shù)據(jù)
    2013-07-07
  • Android打開WebView黑屏閃爍問題排查

    Android打開WebView黑屏閃爍問題排查

    這篇文章主要介紹了Android打開WebView黑屏閃爍問題排查,文章通過詳細(xì)的代碼示例和圖文介紹WebView黑屏閃爍的問題,感興趣的小伙伴可以跟著小編一起來學(xué)習(xí)
    2023-05-05
  • Android Studio如何修改字體的大小

    Android Studio如何修改字體的大小

    這篇文章主要介紹了Android Studio如何修改字體的大小的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Android中ListView Item布局優(yōu)化技巧

    Android中ListView Item布局優(yōu)化技巧

    這篇文章主要介紹了Android中ListView Item布局優(yōu)化技巧,以實(shí)例形式分析了ListView Item布局的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 直接拿來用的Android刮獎(jiǎng)控件

    直接拿來用的Android刮獎(jiǎng)控件

    這篇文章主要為大家分享了可以直接拿來用的Android刮獎(jiǎng)控件,非常棒的刮獎(jiǎng)控件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android開發(fā)學(xué)習(xí)筆記 淺談WebView

    Android開發(fā)學(xué)習(xí)筆記 淺談WebView

    WebView(網(wǎng)絡(luò)視圖)能加載顯示網(wǎng)頁,可以將其視為一個(gè)瀏覽器。它使用了WebKit渲染引擎加載顯示網(wǎng)頁,實(shí)現(xiàn)WebView有以下兩種不同的方法
    2014-11-11
  • 利用Flutter實(shí)現(xiàn)背景圖片毛玻璃效果實(shí)例

    利用Flutter實(shí)現(xiàn)背景圖片毛玻璃效果實(shí)例

    Flutter沒有單獨(dú)的模糊處理容器,需要部件層層疊加實(shí)現(xiàn)模糊效果,下面這篇文章主要給大家介紹了關(guān)于利用Flutter實(shí)現(xiàn)背景圖片毛玻璃效果的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂效果

    Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂效果

    這篇文章主要介紹了Android使用Canvas對(duì)象實(shí)現(xiàn)刮刮樂效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-04-04
  • Android 完全退出的實(shí)例詳解

    Android 完全退出的實(shí)例詳解

    這篇文章主要介紹了Android 完全退出的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10

最新評(píng)論