成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Android截屏截圖的幾種方法總結(jié)

 Android截屏

霸州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!

Android截屏的原理:獲取具體需要截屏的區(qū)域的Bitmap,然后繪制在畫布上,保存為圖片后進(jìn)行分享或者其它用途

一、Activity截屏

1、截Activity界面(包含空白的狀態(tài)欄)

/** 
   * 根據(jù)指定的Activity截圖(帶空白的狀態(tài)欄) 
   * 
   * @param context 要截圖的Activity 
   * @return Bitmap 
   */ 
  public static Bitmap shotActivity(Activity context) { 
    View view = context.getWindow().getDecorView(); 
    view.setDrawingCacheEnabled(true); 
    view.buildDrawingCache(); 
 
    Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
    view.setDrawingCacheEnabled(false); 
    view.destroyDrawingCache(); 
    return bitmap; 
  } 

2、截Activity界面(去除狀態(tài)欄)

/** 
   * 根據(jù)指定的Activity截圖(去除狀態(tài)欄) 
   * 
   * @param activity 要截圖的Activity 
   * @return Bitmap 
   */ 
  public Bitmap shotActivityNoStatusBar(Activity activity) { 
    // 獲取windows中最頂層的view 
    View view = activity.getWindow().getDecorView(); 
    view.buildDrawingCache(); 
 
    // 獲取狀態(tài)欄高度 
    Rect rect = new Rect(); 
    view.getWindowVisibleDisplayFrame(rect); 
    int statusBarHeights = rect.top; 
    Display display = activity.getWindowManager().getDefaultDisplay(); 
 
    // 獲取屏幕寬和高 
    int widths = display.getWidth(); 
    int heights = display.getHeight(); 
 
    // 允許當(dāng)前窗口保存緩存信息 
    view.setDrawingCacheEnabled(true); 
 
    // 去掉狀態(tài)欄 
    Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0, 
        statusBarHeights, widths, heights - statusBarHeights); 
 
    // 銷毀緩存信息 
    view.destroyDrawingCache(); 
 
    return bmp; 
  } 

二、View截屏

/** 
 * 根據(jù)指定的view截圖 
 * 
 * @param v 要截圖的view 
 * @return Bitmap 
 */ 
public static Bitmap getViewBitmap(View v) { 
  if (null == v) { 
    return null; 
  } 
  v.setDrawingCacheEnabled(true); 
  v.buildDrawingCache(); 
  if (Build.VERSION.SDK_INT >= 11) { 
    v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(), View.MeasureSpec.EXACTLY), 
        View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.EXACTLY)); 
    v.layout((int) v.getX(), (int) v.getY(), (int) v.getX() + v.getMeasuredWidth(), (int) v.getY() + v.getMeasuredHeight()); 
  } else { 
    v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
    v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
  } 
 
  Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 
  v.setDrawingCacheEnabled(false); 
  v.destroyDrawingCache(); 
  return bitmap; 
} 

三、ScrollView截屏:ScrollView只有一個(gè)childView,雖然沒有全部顯示在界面上,但是已經(jīng)全部渲染繪制,因此可以直接 調(diào)用scrollView.draw(canvas)來完成截圖

/** 
 * Scrollview截屏 
 * 
 * @param scrollView 要截圖的ScrollView 
 * @return Bitmap 
 */ 
public static Bitmap shotScrollView(ScrollView scrollView) { 
  int h = 0; 
  Bitmap bitmap = null; 
  for (int i = 0; i < scrollView.getChildCount(); i++) { 
    h += scrollView.getChildAt(i).getHeight(); 
    scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff")); 
  } 
  bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565); 
  final Canvas canvas = new Canvas(bitmap); 
  scrollView.draw(canvas); 
  return bitmap; 
} 

四、ListView截屏:ListView是會(huì)回收與重用Item,并且只會(huì)繪制在屏幕上顯示的ItemView,下面的方法采用一個(gè)List來存儲(chǔ)Item的視圖,這種方案依然不夠好,當(dāng)Item足夠多的時(shí)候,可能會(huì)發(fā)生oom。

/** 
 * ListView截圖 
 * 
 * @param listView 要截圖的ListView 
 * @return Bitmap 
 */ 
public static Bitmap shotListView(ListView listView) { 
  ListAdapter adapter = listView.getAdapter(); 
  int itemsCount = adapter.getCount(); 
  int allItemsHeight = 0; 
 
  ArrayList<Bitmap> bmps = new ArrayList<>(); 
  for (int i = 0; i < itemsCount; i++) { 
    View childView = adapter.getView(i, null, listView); 
    childView.measure(View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.EXACTLY), 
        View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED)); 
 
    childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight()); 
    childView.setDrawingCacheEnabled(true); 
    childView.buildDrawingCache(); 
    bmps.add(childView.getDrawingCache()); 
    allItemsHeight += childView.getMeasuredHeight(); 
  } 
 
  Bitmap bigBitmap = Bitmap.createBitmap(listView.getMeasuredWidth(), allItemsHeight, Bitmap.Config.ARGB_8888); 
  Canvas bigCanvas = new Canvas(bigBitmap); 
 
  Paint paint = new Paint(); 
  int iHeight = 0; 
 
  for (int i = 0; i < bmps.size(); i++) { 
    Bitmap bmp = bmps.get(i); 
    bigCanvas.drawBitmap(bmp, 0, iHeight, paint); 
    iHeight += bmp.getHeight(); 
 
    bmp.recycle(); 
    bmp = null; 
  } 
  return bigBitmap; 
} 

五、RecycleView截屏

/** 
   * RecyclerView截屏 
   * 
   * @param view 要截圖的RecyclerView 
   * @return Bitmap 
   */ 
  public static Bitmap shotRecyclerView(RecyclerView view) { 
    RecyclerView.Adapter adapter = view.getAdapter(); 
    Bitmap bigBitmap = null; 
    if (adapter != null) { 
      int size = adapter.getItemCount(); 
      int height = 0; 
      Paint paint = new Paint(); 
      int iHeight = 0; 
      final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); 
 
      // Use 1/8th of the available memory for this memory cache. 
      final int cacheSize = maxMemory / 8; 
      LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize); 
      for (int i = 0; i < size; i++) { 
        RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i)); 
        adapter.onBindViewHolder(holder, i); 
        holder.itemView.measure( 
            View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), 
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
        holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), 
            holder.itemView.getMeasuredHeight()); 
        holder.itemView.setDrawingCacheEnabled(true); 
        holder.itemView.buildDrawingCache(); 
        Bitmap drawingCache = holder.itemView.getDrawingCache(); 
        if (drawingCache != null) { 
 
          bitmaCache.put(String.valueOf(i), drawingCache); 
        } 
        height += holder.itemView.getMeasuredHeight(); 
      } 
 
      bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888); 
      Canvas bigCanvas = new Canvas(bigBitmap); 
      Drawable lBackground = view.getBackground(); 
      if (lBackground instanceof ColorDrawable) { 
        ColorDrawable lColorDrawable = (ColorDrawable) lBackground; 
        int lColor = lColorDrawable.getColor(); 
        bigCanvas.drawColor(lColor); 
      } 
 
      for (int i = 0; i < size; i++) { 
        Bitmap bitmap = bitmaCache.get(String.valueOf(i)); 
        bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint); 
        iHeight += bitmap.getHeight(); 
        bitmap.recycle(); 
      } 
    } 
    return bigBitmap; 
  } 

六、WebView截屏

1、截取webView可視區(qū)域的截圖

/**  
  * 截取webView可視區(qū)域的截圖  
  * @param webView 前提:WebView要設(shè)置webView.setDrawingCacheEnabled(true);  
  * @return  
  */  
private Bitmap captureWebViewVisibleSize(WebView webView){  
  Bitmap bmp = webView.getDrawingCache();  
  return bmp;  
}  

2、截取webView的整個(gè)頁面

/**  
 * 截取webView快照(webView加載的整個(gè)內(nèi)容的大小)  
 * @param webView  
 * @return  
 */  
private Bitmap captureWebView(WebView webView){  
  Picture snapShot = webView.capturePicture();  
    
  Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),snapShot.getHeight(), Bitmap.Config.ARGB_8888);  
  Canvas canvas = new Canvas(bmp);  
  snapShot.draw(canvas);  
  return bmp;  
}  

3、截取手機(jī)屏幕,獲取界面展示的webview

/** 
 * 截屏 
 * 
 * @param context 
 * @return 
 */ 
private Bitmap captureScreen(Activity context) { 
  View cv = context.getWindow().getDecorView(); 
  Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_8888); 
  Canvas canvas = new Canvas(bmp); 
  cv.draw(canvas); 
  return bmp; 
} 

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

網(wǎng)站題目:Android截屏截圖的幾種方法總結(jié)
新聞來源:http://jinyejixie.com/article10/jjpcdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、網(wǎng)站導(dǎo)航品牌網(wǎng)站建設(shè)、面包屑導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
台东市| 溧阳市| 宁强县| 隆化县| 泰安市| 衡南县| 淮安市| 阿拉善左旗| 石景山区| 教育| 共和县| 凯里市| 江山市| 巩留县| 垦利县| 青神县| 辽中县| 麻城市| 天祝| 普陀区| 武冈市| 冕宁县| 出国| 博兴县| 安化县| 梁河县| 台中县| 浮梁县| 嘉兴市| 高青县| 阿城市| 全南县| 梅河口市| 松阳县| 广灵县| 法库县| 张家川| 贡觉县| 民权县| 海淀区| 余庆县|