listview中的item转成bitmap并绘制

本文介绍了如何将ListView中的Item转换为Bitmap并进行绘制,包括两种方法:一种不使用内存缓存,另一种则利用setDrawingCacheEnabled(true)将Bitmap放入内存缓存,详细解析了每一步的操作过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 
   
   
  1. 将layout转为bitmap绘制的步骤:

  2. 方法一:不将bitmap放入内存缓存当中

  3. 1、调用measure方法、设置视图的大小,应传入父级的layout的宽高(int类型)

  4. 2、传入measure的属性,可以通过以下方法进行设置并传入:View.MeasureSpec.makeMeasuereSpec(int width, int model),其中width为view的大小,

  5.    model有三种,详细见MeasureSpec的model详解

  6. 3、设置view的布局尺寸和位置,调用view.layout(int, int, int, int)方法,例如view.layout(0,0,view.getMeasureWidth(),view.getMeasureHeight())表示在左上角开始,右下角结束点为view的长度和宽度

  7. 4、构建bitmap,调用Bitmap.createBitmap(view.getWidth(), view.getHeight())生成bitmap

  8. 5、利用bitmap生成画布

  9. 6、将view的内容绘制在画布上,即可完成

  10. 方法二:将bitmap放入内存缓存当中

  11. 1、调用view.setDrawingCacheEnabled(true);将draw设置为可缓存

  12. 2、调用measure方法、设置视图的大小,应传入父级的layout的宽高(int类型)

  13. 3、传入measure的属性,可以通过以下方法进行设置并传入:View.MeasureSpec.makeMeasuereSpec(int width, int model),其中width为view的大小,根据需要进行设置;

  14.    model有三种,详细见MeasureSpec的model​详解

  15. 4、设置view的布局尺寸和位置,调用view.layout(int, int, int, int)方法,例如view.layout(0,0,view.getMeasureWidth(),view.getMeasureHeight())表示在左上角开始,右下角结束点为view的长度和宽度

  16. 5、构建bitmap,调用Bitmap.createBitmap(view.getWidth(), view.getHeight())生成bitmap

  17. 6、调用view.buildDrawingCache();将view写入内存缓存当中

  18. 7、利用Bitmap bitmap = view.getDrawingCache()获取内存当中view缓存

  19. 8、将view的内容绘制在画布上,即可完成

  1.       
          
    1. @Override
    2. public View getView(int position, View convertView, ViewGroup parent) {

    3.     /** 此处省略代码 */

    4.     // 方法一

    5.     // // 如果没有调用这个方法,得到的bitmap为null

    6.     // convertView.measure(View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY),

    7.     // View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.EXACTLY));

    8.     // // 设置布局的尺寸和位置

    9.     // convertView.layout(0, 0, convertView.getMeasuredWidth(), convertView.getMeasuredHeight());

    10.     // // 生成bitmap

    11.     // Bitmap bitmap = Bitmap.createBitmap(convertView.getWidth(), convertView.getHeight(),

    12.     // Bitmap.Config.RGB_565);

    13.     // // 利用bitmap生成画布

    14.     // Canvas canvas = new Canvas(bitmap);

    15.     // // 把view中的内容绘制在画布上

    16.     // convertView.draw(canvas);

    17.     // 方法二

    18.     // 调用该方法你应该避免启用硬件加速时调用该方法,如果你不需要绘制位图缓存,调用该方法会增加内存使用和导致软件视图呈现一次,因此负面影响性能

    19.     // 获取屏幕宽度大小、设置bitmap的宽度

    20.     WindowManager windowManager = ((Activity) mContext).getWindowManager();

    21.     Display display = windowManager.getDefaultDisplay();

    22.     // 以下方法将bitmap加入缓存当中

    23.     convertView.setDrawingCacheEnabled(true);

    24.     

    25.     // 如果没有调用这个方法,得到的bitmap为null

    26.     // 高度最好选用UNSPECTIFIED模式、这样才会使得每个item的高度能自适应

    27.     convertView.measure(View.MeasureSpec.makeMeasureSpec(display.getWidth(), View.MeasureSpec.EXACTLY),

    28.         View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.UNSPECIFIED)); // 关于MeasureSpec.EXACTLY的说明和理解,见以下

    29.     // 设置布局的尺寸和位置

    30.     convertView.layout(0, 0, convertView.getMeasuredWidth(), convertView.getMeasuredHeight());

    31.     // 获得绘图缓存中的Bitmap

    32.     convertView.buildDrawingCache();

    33.     Bitmap bitmap = convertView.getDrawingCache();

    34.     converView.draw(bitmap);

    35.     /** 此处省略其他代码 */

    36.     return converView;

    37. }


以下是Android官方文档的解释
/**
 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
 * Each MeasureSpec represents a requirement for either the width or the height.
 * A MeasureSpec is comprised of a size and a mode. There are three possible
 * modes:
 * <dl>
 <dt>UNSPECIFIED</dt>
 <dd>
 * The parent has not imposed any constraint on the child. It can be whatever size
 * it wants.
 * </dd>
 *
 * <dt>EXACTLY</dt>
 <dd>
 * The parent has determined an exact size for the child. The child is going to be
 * given those bounds regardless of how big it wants to be.
 * </dd>
 *
 * <dt>AT_MOST</dt>
 <dd>
 * The child can be as large as it wants up to the specified size.
 * </dd>
 </dl>
 *
 * MeasureSpecs are implemented as ints to reduce object allocation. This class
 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
 */
UNSPECIFIED:如果你的layout的xml文件中含有隐藏的控件,则改model可以动态根据你的view来获取width和height,不会将隐藏的控件高度计算进来
EXACTLY:完全按照你的layout的xml文件的宽高设置,如果含有隐藏的控件,也会将隐藏的控件宽高度计算进来
AT_MOST:最大按照你设置的宽高度,例如View.MeasureSpec.makeMeasureSpec(256, View.MeasureSpec.AT_MOST),则不管你的view有多大,最大只能为256
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值