Android 將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針問(wèn)題解決辦法
Android 將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針問(wèn)題解決辦法
在做Android 項(xiàng)目的時(shí)候,有時(shí)候可能有這樣的需求,將一個(gè)View 或者一個(gè)布局文件轉(zhuǎn)換成一個(gè)Bitmap 對(duì)象。
方法其實(shí)大都差不多。但這其中有一些小細(xì)節(jié)需要注意一下。最近在項(xiàng)目中用到了這個(gè)功能,現(xiàn)在分享一下,希望能幫助到遇到果這個(gè)
問(wè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) * 來(lái)測(cè)量view 的時(shí)候,(如果你的布局中包含有 RelativeLayout )API 為17 或者 低于17 會(huì)包空指針異常 * 解決方法: * 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)
來(lái)測(cè)量view 的時(shí)候,(如果你的布局中包含有 RelativeLayout )API 為17 或者 低于17 會(huì)包空指針異常。在項(xiàng)目中遇到這個(gè)問(wèn)題
死活不知道是怎么回事,后來(lái)在看源碼的時(shí)候才發(fā)現(xiàn)。以下是這個(gè)方法的官方解釋:
/** * 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)中才修正了這個(gè)bug,這里有兩個(gè)解決方法:
1 ,布局文件中不要包含Relativelayout 布局
2,用 View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY) 好像也可以
以上就是Android 將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針問(wèn)題解決辦法,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,謝謝大家對(duì)本站的支持!
相關(guān)文章
Android獲得設(shè)備狀態(tài)信息、Mac地址、IP地址的方法
今天小編就為大家分享一篇關(guān)于Android獲得設(shè)備狀態(tài)信息、Mac地址、IP地址的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12詳解Android Studio正式簽名進(jìn)行調(diào)試的實(shí)現(xiàn)步驟
這篇文章主要介紹了詳解Android Studio正式簽名進(jìn)行調(diào)試的實(shí)現(xiàn)步驟的相關(guān)資料,需要的朋友可以參考下2017-07-07Android studio中生成引用.aar和.jar的方法詳解
這篇文章主要是講解.aar的生成與引用,文中的內(nèi)容屬于完全基礎(chǔ)性概念,對(duì)剛學(xué)習(xí)使用Android studio的朋友們很有幫助,有需要的可以參考學(xué)習(xí),下面來(lái)一起看看吧。2016-09-09在Android線程池里運(yùn)行代碼任務(wù)實(shí)例
這篇文章主要介紹了在Android線程池里運(yùn)行代碼任務(wù)實(shí)例,同時(shí)介紹了線程池中停止任務(wù)的方法,需要的朋友可以參考下2014-06-06解決Android studio 3.6.1 出現(xiàn)Cause: unable to find valid certifi
這篇文章主要介紹了Android studio 3.6.1 出現(xiàn)Cause: unable to find valid certification path to requested target 報(bào)錯(cuò)的問(wèn)題及解決方法,需要的朋友可以參考下2020-03-03Android開(kāi)屏頁(yè)倒計(jì)時(shí)功能實(shí)現(xiàn)的詳細(xì)教程
本篇文章主要介紹了Android實(shí)現(xiàn)開(kāi)屏頁(yè)倒計(jì)時(shí)功能實(shí)現(xiàn)的詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Android 驗(yàn)證碼功能實(shí)現(xiàn)代碼
這篇文章主要介紹了Android 驗(yàn)證碼功能實(shí)現(xiàn)代碼的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Android Okhttp斷點(diǎn)續(xù)傳面試深入解析
這篇文章主要給大家介紹了關(guān)于Android Okhttp斷點(diǎn)續(xù)傳面試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06