2013-10-22 16 views
8

di riferimento a questo tema: Android get screenshot of all ListView itemsConverti elementi ListView in un'unica immagine bitmap

voglio fare una cosa del genere, ma ho adattatore personalizzato ed è buttarmi NullPointerException errore in questo codice:

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

mio adattatore personalizzato:

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; 


} 

} 

My Custom Fila:

<?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> 

Grazie per il vostro aiuto.

+1

provare getDrawableCache() funzione per ottenere bitmap dalla vista. –

+0

@Adnan hai trovato una soluzione –

+0

@Adhan mostra il tuo codice sorgente completo .. il tuo listview o childView lancia un errore puntatore nullo –

risposta

4

è possibile ottenere il bitmap da qualsiasi vista in questo modo:

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; 
} 
2

prova questo e adattalo al tuo codice. Spero che questo aiuto ....

public static Bitmap getWholeListViewItemsToBitmap() { 

ListView listview = MyActivity.mFocusedListView; 
ListAdapter adapter = listview.getAdapter(); 
int itemscount  = adapter.getCount(); 
int allitemsheight = 0; 
List<Bitmap> bmps = new ArrayList<Bitmap>(); 

for (int i = 0; i < itemscount; i++) { 

    View childView  = adapter.getView(i, null, listview); 
    childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 

    childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight()); 
    childView.setDrawingCacheEnabled(true); 
    childView.buildDrawingCache(); 
    bmps.add(childView.getDrawingCache()); 
    allitemsheight+=childView.getMeasuredHeight(); 
} 

Bitmap bigbitmap = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888); 
Canvas bigcanvas = new Canvas(bigbitmap); 

Paint paint = new Paint(); 
int iHeight = 0; 

for (int i = 0; i < bmps.size(); i++) { 
    Bitmap bmp = bmps.get(i); 
    bigcanvas.drawBitmap(bmp, 0, iHeight, paint); 
    iHeight+=bmp.getHeight(); 

    bmp.recycle(); 
    bmp=null; 
} 


return bigbitmap; 
} 
0

Facile & modo semplice per farlo -

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; 

}