Android 将Listview项转换为单个位图图像

Android 将Listview项转换为单个位图图像,android,listview,bitmap,Android,Listview,Bitmap,参考本主题: 我想这样做,但我有一个自定义适配器,下面的代码中有一个抛出NullPointerException错误: childView.measure(MeasureSpec.makeMeasureSpec(_listView.getWidth(), MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 我的自定义适配器: public class InvoiceListAdap

参考本主题:

我想这样做,但我有一个自定义适配器,下面的代码中有一个抛出NullPointerException错误:

childView.measure(MeasureSpec.makeMeasureSpec(_listView.getWidth(),    MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

我的自定义适配器:

public class InvoiceListAdapter extends BaseAdapter {

  ArrayList<Object> _itemList;
  public Activity _context;
  public LayoutInflater _inflater;
  BarCodeGenerator Generator = new BarCodeGenerator();


  public InvoiceListAdapter(Activity context,ArrayList<Object> itemList)
  {
      super();
      this._context=context;
      this._itemList=itemList;
      this._inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  }
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return _itemList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return _itemList.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public static class ViewHolder
{
 TextView ProductName;
 TextView Qnt;
 ImageView Barcode;
 TextView BarcodeFormat;
 Button Del;

}



@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
     if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = _inflater.inflate(R.layout.custom_row_view, null);


            holder.ProductName = (TextView) convertView.findViewById(R.id.txt_CRow_ProdName);
            holder.Qnt = (TextView) convertView.findViewById(R.id.txt_CRow_Qnt);
            holder.Del = (Button) convertView.findViewById(R.id.btn_CRow_Delete);
            holder.Barcode = (ImageView) convertView.findViewById(R.id.Img_CRow_Barcode);
            /*-----------------------------Deleting Item with Button--------------------*/
            holder.Del.setTag(holder);


            convertView.setTag(holder);
        }
        else
        {
            holder=(ViewHolder)convertView.getTag();
        }

        AnItem Item = (AnItem) _itemList.get(position);

        holder.ProductName.setText(Item.getProductName());
        holder.Qnt.setText(Item.getQnt());
        holder.Barcode.setImageBitmap(Generator.Generate(Item.getProductName(), BarcodeFormat.valueOf(Item.getBarcode() ),500,200 ) ); 

        holder.Del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(_context,"Item Deleted!: "+position, Toast.LENGTH_SHORT).show();

                _itemList.remove(position);  
                notifyDataSetChanged();

                // TODO Auto-generated method stub

            }
        });

        return convertView;


}

}
公共类InvoiceListAdapter扩展了BaseAdapter{
ArrayList\u itemList;
公共活动环境;
公共场所用充气机;
条码生成器=新条码生成器();
公共InvoiceListAdapter(活动上下文,ArrayList itemList)
{
超级();
这._context=context;
这个._itemList=itemList;
这个.u充气器=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气器\u SERVICE);
}
@凌驾
public int getCount(){
//TODO自动生成的方法存根
返回_itemList.size();
}
@凌驾
公共对象getItem(int位置){
//TODO自动生成的方法存根
return\u itemList.get(位置);
}
@凌驾
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
公共静态类视图持有者
{
TextView产品名称;
文本视图Qnt;
图像视图条码;
文本视图条码格式;
按钮Del;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null)
{
holder=新的ViewHolder();
convertView=\充气器.充气(R.layout.custom\行\视图,空);
holder.ProductName=(TextView)convertView.findViewById(R.id.txt\u CRow\u ProdName);
holder.Qnt=(TextView)convertView.findViewById(R.id.txt\u CRow\u Qnt);
holder.Del=(按钮)convertView.findViewById(R.id.btn\u CRow\u Delete);
holder.Barcode=(ImageView)convertView.findViewById(R.id.Img\U CRow\U条形码);
/*-----------------------------使用按钮删除项目--------------------*/
holder.Del.setTag(holder);
convertView.setTag(支架);
}
其他的
{
holder=(ViewHolder)convertView.getTag();
}
AnItem Item=(AnItem)\u itemList.get(position);
holder.ProductName.setText(Item.getProductName());
holder.Qnt.setText(Item.getQnt());
holder.Barcode.setImageBitmap(生成器.Generate(Item.getProductName(),BarcodeFormat.valueOf(Item.getBarcode()),500200));
holder.Del.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(_context,“Item Deleted!”:“+position,Toast.LENGTH_SHORT).show();
_项目列表。删除(位置);
notifyDataSetChanged();
//TODO自动生成的方法存根
}
});
返回视图;
}
}

我的自定义行:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical">

<TextView
android:id="@+id/txt_CRow_ProdName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/ProductName"
android:textSize="16sp"
android:textStyle="bold" />

<Button
android:id="@+id/btn_CRow_Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:text="@string/Delete"
android:textSize="12sp"/>

<TextView
android:id="@+id/txt_CRow_Qnt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Img_CRow_Barcode"
android:hint="@string/Qnt"
android:textSize="12sp"
android:textStyle="bold" />

<ImageView
android:id="@+id/Img_CRow_Barcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_CRow_ProdName"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btn_CRow_Delete"
android:layout_alignParentLeft="true"
 />

</RelativeLayout>


谢谢您的帮助。

尝试此方法并适应您的代码。希望这有助于

公共静态位图getWholeListViewItemsToBitmap(){
ListView ListView=MyActivity.mfocustedListView;
ListAdapter=listview.getAdapter();
int itemscont=adapter.getCount();
int-allitemsheight=0;
List bmps=new ArrayList();
对于(int i=0;i
您可以从以下任何视图获取位图:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        bgDrawable.draw(canvas);
    else
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

简单易行的方法-

public  Bitmap getBitmapFromView(View pView, int width, int height){

pView.setDrawingCacheEnabled(true);
pView.measure(MeasureSpace.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

pView.layout(0, 0, width, height);
pView.buildDrawingCache(true);

Bitmap bitmap = Bitmape.createScaledBitmap(pView.getDrawingCache(), width, height, false);
pView.setDrawingCacheEnabled(false);

return bitmap;

}

尝试使用getDrawableCache()函数从视图中获取位图。@Adnan您找到了吗solution@Adhan显示完整的源代码。。您的listview或childView正在抛出空指针错误。您实际在哪里拍摄屏幕快照?是此行吗:holder.Barcode.setImageBitmap(Generator.Generate(Item.getProductName(),BarcodeFormat.valueOf(Item.getBarcode()),500200));堆栈跟踪应该很有用。