国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 動態加載二維碼視圖生成快照的示例

2019-10-22 18:24:57
字體:
來源:轉載
供稿:網友

1.需求背景

需要實現一個動態加載但不顯示出來的視圖,且該視圖上有個動態生成的二維碼,最后用其去生成一張快照(也就是圖片)。
(常見這種情況是來源于“圖片分享”的功能需求,與普通圖片分享不同在于,該快照圖片是動態加載不顯示的。)

Android,二維碼生成,動態加載二維碼

2.需求功能拆解

  1. 動態二維碼的實現
  2. 動態視圖生成快照的實現

3.踩坑點提要

  1. 獲取不到動態視圖的bitmap
  2. 無法獲取最新動態視圖的bitmap

4.開發實現

動態加載的視圖的布局文件代碼:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:id="@+id/qrcodeContentLl"   android:background="#F0E68C"   android:orientation="vertical">    <TextView     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_gravity="center"     android:layout_marginTop="100dp"     android:text="二維碼快照"     android:textSize="18sp"     android:textStyle="italic" />    <ImageView     android:id="@+id/qrcodeIv"     android:layout_width="100dp"     android:layout_height="100dp"     android:layout_gravity="center"     android:layout_marginTop="@dimen/activity_vertical_margin"     android:scaleType="fitCenter" />    <!--<TextView-->     <!--android:layout_width="wrap_content"-->     <!--android:layout_height="wrap_content"-->     <!--android:layout_marginTop="800dp"-->     <!--android:text="ahahds"-->     <!--android:layout_gravity="center"/>--> </LinearLayout> 

大概樣式如下:

Android,二維碼生成,動態加載二維碼

(上面的線框是用來顯示動態生成的二維碼圖片的)

a.動態二維碼的實現

關于這塊內容,網上有太多例子了,其實也不用詳解。主要是利用Zxing提供的jar包來進行處理。需要看這塊的詳細代碼可以去文章最后提供的GitHub地址查看,在此只提供下該jar包的資源下載(項目中若只涉及生成二維碼模塊,那么只要core核心jar包即可):點擊下載>> core-3.3.0.jar

b.動態視圖生成快照的實現

private void inflateAndShowCaptureView() {    if (hideView == null) {      hideView = LayoutInflater.from(this).inflate(R.layout.layout_quick_capture, null);      qrcodeIv = (ImageView) hideView.findViewById(R.id.qrcodeIv);      hideView.setDrawingCacheEnabled(true);//設置控件允許繪制緩存      hideView.measure(View.MeasureSpec.makeMeasureSpec(mainLayoutLl.getWidth(), View.MeasureSpec.EXACTLY),          View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));      hideView.layout(0, 0, hideView.getMeasuredWidth(), hideView.getMeasuredHeight());    } else {      hideView.destroyDrawingCache();//要得到新的視圖,就得銷毀之前的緩存    }     showCaptureView();  }   private void showCaptureView() {    String content = contentEt.getText().toString().trim();    if (content == null || content.length() == 0) {      return;    }    if (qrcodeIv.getWidth() == 0) {      return;    }    Bitmap qrcodeBitmap = ZXingUtils.createQRImage(content, qrcodeIv.getWidth(), qrcodeIv.getHeight());    qrcodeIv.setImageBitmap(qrcodeBitmap);//先將生成的二維碼顯示在加載的視圖上     Bitmap bitmap = hideView.getDrawingCache(); // 獲取視圖的繪制緩存(快照)    if (bitmap != null) {      showIv.setImageBitmap(bitmap);    }   } 

1.首先獲取到視圖的bitmap是通過getDrawingCache()得到的。

  1. 若視圖是在界面上直接顯示出來的——>那么使用該方法直接獲取bitmap是沒有問題的;
  2. 若視圖是動態加載且不顯示出來,那么此時獲取bitmap是null。

此處的解決辦法就是手動給該視圖布局:

hideView.measure(View.MeasureSpec.makeMeasureSpec(mainLayoutLl.getWidth(), View.MeasureSpec.EXACTLY),     View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); hideView.layout(0, 0, hideView.getMeasuredWidth(), hideView.getMeasuredHeight()); 

以下做點簡單解釋:

View.MeasureSpec.makeMeasureSpec(int size , int mode)中有兩個參數,size和mode,第一組MeasureSpec中我將size設置為了當前顯示頁面的布局的寬度(也就是屏幕寬度),然后mode設置為EXACTLY——>所表示的意義是:給hideView中的子View指定了精確的寬度大小為當前屏幕的寬度。

mode有三種,EXACTLY,AT_MOST,UNSPECIFIED。在上面代碼中,將高度的size指定為0,mode指定為 UNSPECIFIED 則表示——>整個動態加載的視圖高度指定為:依據于最后子View確認的高度。

若將第一組MeasureSpec的相關參數也改為size = 0, mode = UNSPECIFIED,則兩組圖對比顯示如下:

Android,二維碼生成,動態加載二維碼

可以看到,動態生成的快照的寬度也變成了顯示二維碼的ImageView的寬度了。

擴展:如何在寬高均為size = 0 && mode= UNSPECIFIED 的情況下獲取整個屏幕大小的視圖呢?
——>用幾個隱藏的組件埋在視圖的四個邊界,啊哈哈哈哈哈!

2.通過destroyDrawingCache()來刪除之前的緩存。

最后,GitHub地址>>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 纳雍县| 宁波市| 宁海县| 广东省| 抚宁县| 信宜市| 溆浦县| 昌图县| 东乡县| 梧州市| 青河县| 华安县| 巩义市| 凤台县| 容城县| 奈曼旗| 慈溪市| 砀山县| 富蕴县| 朝阳区| 竹山县| 泽普县| 长汀县| 定南县| 陇南市| 武川县| 嘉定区| 兰溪市| 姜堰市| 杭州市| 乌拉特前旗| 绍兴县| 车致| 天全县| 沅陵县| 金山区| 禄丰县| 长治县| 黎川县| 高邮市| 高邮市|