最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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解析JSON數(shù)據(jù)的方法分析

    Android解析JSON數(shù)據(jù)的方法分析

    這篇文章主要介紹了Android解析JSON數(shù)據(jù)的方法,結(jié)合實(shí)例形式演示了Android解析json格式數(shù)據(jù)的原理與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android ListView自動(dòng)生成列表?xiàng)l目的實(shí)例

    Android ListView自動(dòng)生成列表?xiàng)l目的實(shí)例

    下面小編就為大家分享一篇Android ListView自動(dòng)生成列表?xiàng)l目的實(shí)例,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • android開發(fā)去除標(biāo)題欄的方法

    android開發(fā)去除標(biāo)題欄的方法

    這篇文章主要介紹了android開發(fā)去除標(biāo)題欄的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-04-04
  • Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法

    Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法

    這篇文章主要介紹了Android使用GPS獲取用戶地理位置并監(jiān)聽位置變化的方法,實(shí)例分析了Android編程中GPS定位的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2015-12-12
  • 淺談Android IPC機(jī)制之Binder的工作機(jī)制

    淺談Android IPC機(jī)制之Binder的工作機(jī)制

    IPC機(jī)制即為跨進(jìn)程通信,是inter-Process Communication的縮寫。是指兩個(gè)進(jìn)程之間進(jìn)行通信。在說進(jìn)程通信之前,我們的弄明白什么是線程,什么是進(jìn)程。進(jìn)程和線程是兩個(gè)截然不同的概念。本文將介紹Android IPC機(jī)制之Binder的工作機(jī)制。
    2021-06-06
  • Kotlin協(xié)程操作之創(chuàng)建啟動(dòng)掛起恢復(fù)詳解

    Kotlin協(xié)程操作之創(chuàng)建啟動(dòng)掛起恢復(fù)詳解

    本文的定位是協(xié)程的創(chuàng)建、啟動(dòng)、掛起、恢復(fù),也會(huì)示例一些簡單的使用,這里不對(duì)suspend講解,,也不對(duì)協(xié)程的高級(jí)用法做闡述(熱數(shù)據(jù)通道Channel、冷數(shù)據(jù)流Flow...),本文主要講協(xié)程稍微深入的全面知識(shí)
    2022-08-08
  • Android實(shí)現(xiàn)幀動(dòng)畫的兩種方式

    Android實(shí)現(xiàn)幀動(dòng)畫的兩種方式

    幀動(dòng)畫(Frame?Animation)是一種在一定時(shí)間內(nèi)按順序播放一系列圖像幀(每一幀都是一個(gè)單獨(dú)的圖像),從而產(chǎn)生連續(xù)運(yùn)動(dòng)或變化的動(dòng)畫效果,本文給大家介紹了Android實(shí)現(xiàn)幀動(dòng)畫的兩種方式,需要的朋友可以參考下
    2024-02-02
  • Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件詳解

    Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件詳解

    自Android 3.0版本開始,系統(tǒng)給我們提供了一種全新的動(dòng)畫模式,屬性動(dòng)畫(property animation),它的功能非常強(qiáng)大,下面這篇文章主要給大家介紹了關(guān)于Android使用屬性動(dòng)畫如何自定義倒計(jì)時(shí)控件的相關(guān)資料,需要的朋友可以參考下
    2018-05-05
  • Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)教程

    Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)教程

    本文主要介紹了Android 6.0動(dòng)態(tài)權(quán)限申請(qǐng)的教程,具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • android短信攔截的實(shí)現(xiàn)代碼

    android短信攔截的實(shí)現(xiàn)代碼

    這篇文章介紹了android短信攔截的實(shí)現(xiàn)代碼,有需要的朋友可以參考一下
    2013-09-09

最新評(píng)論

黄龙县| 洛隆县| 同德县| 玉门市| 五原县| 霍州市| 灵宝市| 广丰县| 兴业县| 武城县| 启东市| 新宾| 罗平县| 裕民县| 加查县| 白朗县| 土默特右旗| 正安县| 潜江市| 上思县| 黄浦区| 台北县| 吉木乃县| 开阳县| 海宁市| 江川县| 方山县| 铜梁县| 抚顺县| 化隆| 巢湖市| 贵定县| 梅河口市| 安顺市| 镇安县| 兰西县| 蒙阴县| 上思县| 博客| 白城市| 阜新市|