Android Programjaim

Android, Flutter/Dart, Kotlin, Java, Unity, HarmonyOS

Android how to screen-shot a ListView with some code?

2012. október 07. 17:25 - lacas8282

basically this is very easy, we iterate on the listview's items, then we just call

setDrawingCacheEnabled(true)

and

getDrawingCache()

on each childs, then add all Bitmaps to an ArrayList, then we draw all the Bitmaps to a big Bitmap, and here it is, the final code:

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

Tehát, magyarul is: hogyan fotózzunk le egy ListView-et Androidban, borzasztó egyszerűen a ListView elemeit az adapterből vesszük, majd egyenként végigmegyünk a listview child-jain, amiket "lefotózunk" egy Bitmap-be, amit egy ArrayList-hez adunk. Ezután A nagy képre minden egyes List-ben lévő Bitmapet átmásolunk a nagyképbe egymás alá...És kész is!

Ez egy saját workaround, mivel a sima screen-shot androidban csak a képernyőről készít shot-ot, így a listview elemei kimaradnak. Ez az én áthidaló megoldásom.

komment
süti beállítások módosítása