资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

在Android中将view转换为Bitmap出现空指针如何解决

这篇文章主要介绍了在Android中将view 转换为Bitmap出现空指针如何解决,此处通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下:

目前创新互联已为上1000家的企业提供了网站建设、域名、虚拟空间、网站托管、服务器租用、企业网站设计、朔州网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

 首先是转换 的代码:

/**
   * 将View(布局) 转换为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;
  }

 上面就是转换成Bitmap 的方法,但是要注意,在用View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)

          来测量view 的时候,(如果你的布局中包含有 RelativeLayout )API 为17 或者 低于17 会包空指针异常。在项目中遇到这个问题

死活不知道是怎么回事,后来在看源码的时候才发现。以下是这个方法的官方解释:

/**
     * Creates a measure specification based on the supplied size and mode.
     *
     * The mode must always be one of the following:
     * 
         * 
  • {@link android.view.View.MeasureSpec#UNSPECIFIED}
  •      * 
  • {@link android.view.View.MeasureSpec#EXACTLY}
  •      * 
  • {@link android.view.View.MeasureSpec#AT_MOST}
  •      * 
     *      * 

Note: 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.

     *      * @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 以上的系统中才修正了这个bug,这里有两个解决方法:

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

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

到此这篇关于在Android中将view 转换为Bitmap出现空指针如何解决的文章就介绍到这了,更多相关在Android中将view 转换为Bitmap出现空指针如何解决的内容请搜索创新互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持创新互联!


当前文章:在Android中将view转换为Bitmap出现空指针如何解决
本文地址:http://cdkjz.cn/article/jiidsp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220