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

首頁 > 系統(tǒng) > Android > 正文

Android 將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針問題解決辦法

2019-12-12 02:22:03
字體:
供稿:網(wǎng)友

Android 將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針問題解決辦法

在做Android 項目的時候,有時候可能有這樣的需求,將一個View 或者一個布局文件轉(zhuǎn)換成一個Bitmap  對象。

方法其實大都差不多。但這其中有一些小細(xì)節(jié)需要注意一下。最近在項目中用到了這個功能,現(xiàn)在分享一下,希望能幫助到遇到果這個

問題的人。

 首先是轉(zhuǎn)換 的代碼:

/**   * 將View(布局) 轉(zhuǎn)換為bitmap   * @param view   * @return   */  public static Bitmap createBitmap(View view){    view.setDrawingCacheEnabled(true);    /**     * 這里要注意,在用View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)     * 來測量view 的時候,(如果你的布局中包含有 RelativeLayout )API 為17 或者 低于17 會包空指針異常     * 解決方法:     * 1 布局中不要包含RelativeLayout     * 2 用 View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY) 好像也可以     *     */    view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());    view.buildDrawingCache();    Bitmap bitmap = view.getDrawingCache();    return bitmap;  }

 上面就是轉(zhuǎn)換成Bitmap 的方法,但是要注意,在用View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)

          來測量view 的時候,(如果你的布局中包含有 RelativeLayout )API 為17 或者 低于17 會包空指針異常。在項目中遇到這個問題

死活不知道是怎么回事,后來在看源碼的時候才發(fā)現(xiàn)。以下是這個方法的官方解釋:

/**     * Creates a measure specification based on the supplied size and mode.     *     * The mode must always be one of the following:     * <ul>     * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>     * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>     * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>     * </ul>     *     * <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's     * implementation was such that the order of arguments did not matter     * and overflow in either value could impact the resulting MeasureSpec.     * {@link android.widget.RelativeLayout} was affected by this bug.     * Apps targeting API levels greater than 17 will get the fixed, more strict     * behavior.</p>     *     * @param size the size of the measure specification     * @param mode the mode of the measure specification     * @return the measure specification based on size and mode     */    public static int makeMeasureSpec(int size, int mode) {      if (sUseBrokenMakeMeasureSpec) {        return size + mode;      } else {        return (size & ~MODE_MASK) | (mode & MODE_MASK);      }    }

  在API 17 以上的系統(tǒng)中才修正了這個bug,這里有兩個解決方法:

 1 ,布局文件中不要包含Relativelayout 布局

 2,用 View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY) 好像也可以

以上就是Android 將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針問題解決辦法,如有疑問請留言或者到本站社區(qū)交流討論,謝謝大家對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 江永县| 马龙县| 抚顺县| 桦川县| 米泉市| 辽源市| 淮滨县| 南平市| 长岛县| 高尔夫| 女性| 丰台区| 江都市| 祁阳县| 商都县| 灵丘县| 烟台市| 密云县| 新竹县| 洪雅县| 西乌珠穆沁旗| 佛山市| 铁力市| 滁州市| 故城县| 永兴县| 额敏县| 西峡县| 濉溪县| 休宁县| 伽师县| 台南市| 万盛区| 广东省| 福清市| 巴青县| 珠海市| 抚州市| 海晏县| 和林格尔县| 日喀则市|