欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

在Android中將view轉(zhuǎn)換為Bitmap出現(xiàn)空指針如何解決

這篇文章主要介紹了在Android中將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針如何解決,此處通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:

目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、朔州網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

 首先是轉(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 的時(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)

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

死活不知道是怎么回事,后來在看源碼的時(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) 好像也可以

到此這篇關(guān)于在Android中將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針如何解決的文章就介紹到這了,更多相關(guān)在Android中將view 轉(zhuǎn)換為Bitmap出現(xiàn)空指針如何解決的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!

文章標(biāo)題:在Android中將view轉(zhuǎn)換為Bitmap出現(xiàn)空指針如何解決
本文路徑:http://chinadenli.net/article38/jiidsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、面包屑導(dǎo)航品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、品牌網(wǎng)站建設(shè)建站公司

廣告

聲明:本網(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ǎng)站建設(shè)