亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android實(shí)現(xiàn)全屏截圖或長(zhǎng)截屏功能

 更新時(shí)間:2018年05月15日 11:53:07   作者:拔絲地瓜  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)全屏截圖或長(zhǎng)截屏功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android實(shí)現(xiàn)全屏截圖或長(zhǎng)截屏功能的具體代碼,供大家參考,具體內(nèi)容如下

全屏截圖:

/**
* 傳入的activity是要截屏的activity
*/
public static Bitmap getViewBitmap(Activity activity) {
  // View是你需要截圖的View
  View view = activity.getWindow().getDecorView();
  //這兩句必須寫(xiě),否則getDrawingCache報(bào)空指針
  view.setDrawingCacheEnabled(true);
  view.buildDrawingCache();
  Bitmap b1 = view.getDrawingCache();

  // 獲取狀態(tài)欄高度
  Rect frame = new Rect();
  activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
  int statusBarHeight = frame.top;

  // 獲取屏幕長(zhǎng)和高
  int width = activity.getResources().getDisplayMetrics().widthPixels;
  int height = activity.getResources().getDisplayMetrics().heightPixels;
  // 去掉標(biāo)題欄
  Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
  view.destroyDrawingCache();
  return b;
}

ScrollView或者ListView或者LinearLayout等ViewGroup的長(zhǎng)截圖:

public static Bitmap getViewGroupBitmap(ViewGroup viewGroup) {
  //viewGroup的總高度
  int h = 0;
  Bitmap bitmap; 

  // 適用于ListView或RecyclerView等求高度
  for (int i = 0; i < viewGroup.getChildCount(); i++) {
   h += viewGroup.getChildAt(i).getHeight();
  }

  // 若viewGroup是ScrollView,那么他的直接子元素有id的話,如下所示:
  // h = mLinearLayout.getHeight();

  }
  // 創(chuàng)建對(duì)應(yīng)大小的bitmap(重點(diǎn))
  bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  scrollView.draw(canvas);
  return bitmap;
 }


總結(jié):

1. 布局為ScrollView,ListView,RecyclerView等能滑動(dòng)的,用for循環(huán)遍歷子元素求實(shí)際高度。
ps:ScrollView由于只能有一個(gè)直接子元素,那么我們可以直接用他的子元素來(lái)求高度。
2. 布局為L(zhǎng)inearLayout等ViewGroup,直接.getHeight()獲取

注意:

1. getHeight(),getWidth()不能直接在avtivity生命周期中調(diào)用,因?yàn)閍ctivity尚未生成完畢之前,控件的長(zhǎng)寬尚未測(cè)量,故所得皆為0
2. 用該方式實(shí)現(xiàn)長(zhǎng)截屏需要注意背景色的問(wèn)題,如果你的截圖背景色出了問(wèn)題,仔細(xì)檢查XML文件,看看該背景色是否設(shè)置在你截屏的控件中

補(bǔ)充:

對(duì)于混合布局比如說(shuō):根RelativeLayout布局中有ViewGroup+RelativeLayout等子布局,可以分別測(cè)量他們的高度并生成bitmap對(duì)象,然后拼接在一起即可。

/**
* 上下拼接兩個(gè)Bitmap,
* drawBitmap的參數(shù):1.需要畫(huà)的bitmap
*     2.裁剪矩形,bitmap會(huì)被該矩形裁剪
*     3.放置在canvas的位置矩形,bitmap會(huì)被放置在該矩形的位置上
*     4.畫(huà)筆
*/
public static Bitmap mergeBitmap_TB_My(Bitmap topBitmap, Bitmap bottomBitmap) {
  int width = topBitmap.getWidth();
  int height = topBitmap.getHeight() + bottomBitmap.getHeight();
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  Rect rectTop = new Rect(0, 0, width, topBitmap.getHeight());
  Rect rectBottomRes = new Rect(0, 0, width, bottomBitmap.getHeight());
  RectF rectBottomDes = new RectF(0, topBitmap.getHeight(), width, height);
  canvas.drawBitmap(topBitmap, rectTop, rectTop, null);
  canvas.drawBitmap(bottomBitmap, rectBottomRes, rectBottomDes, null);
  return bitmap;
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論