以编程方式在android中创建自定义列表视图的图像与总列表项

时间:2014-05-27 15:34:15

标签: android listview bitmap

用于创建自定义列表视图的图像创建每个列表项的位图并通过回收所有小位图创建一个大位图但是创建大位图应用程序时冻结,如果用户在bigbitmap创建期间滚动列表视图图像创建不正确,我按照所有stackoverflow链接都与列表视图的截图相关但无法解决此问题。 这是我创建bigbitmap的代码。请帮忙

    // View childView=null;

    ListAdapter adapter = null;

    try {

        // listview = Result_Activity.mFocusedListView;
        adapter = mFocusedListView.getAdapter();
        int itemscount = adapter.getCount();
        System.out.println("itemscount" + itemscount);
        int allitemsheight = 0;
        List<Bitmap> bmps = new ArrayList<Bitmap>();

        // This loop is for Getting height of the list view
        for (int i = 0; i < itemscount; i++) {

            System.out.println("i " + i);
            System.out.println("ilistview " + mFocusedListView);
            System.out.println("height og listview  "
                    + mFocusedListView.getHeight());
            childView = adapter.getView(i, mFocusedListView, null);

            System.out.println("child view " + childView);
            // setting height and width for child view
            childView.measure(MeasureSpec.makeMeasureSpec(
                    mFocusedListView.getMeasuredWidth(),
                    MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                    mFocusedListView.getMeasuredHeight(),
                    MeasureSpec.UNSPECIFIED));
            // Setting Layout for child view
            childView.layout(0, 0, childView.getMeasuredWidth(),
                    childView.getMeasuredHeight());
            childView.setDrawingCacheEnabled(true);
            childView.setBackgroundColor(0xFFFFFFFF);
            // System.out.println("array size  ="+bmps.size());

            allitemsheight += childView.getMeasuredHeight();

            System.out.println("all item, height" + allitemsheight);

        }
        // Creating a big bitmap height of as listview
        bigbitmap = Bitmap.createBitmap(
                mFocusedListView.getMeasuredWidth(), allitemsheight,
                Bitmap.Config.ARGB_8888);
        // Setting color to bitmap
        bigbitmap.eraseColor(0xFFFFFFFF);

        // System.out.println("bigbitmap "+bigbitmap);

        int height = 0;
        // loop for getting view for every item in list view
        for (int j = 0; j < itemscount; j++) {

            System.out.println("i " + j);
            System.out.println("height is " + height);

            childView = adapter.getView(j, mFocusedListView, null);

            // Settting height and width for childView

            childView.measure(MeasureSpec.makeMeasureSpec(
                    mFocusedListView.getMeasuredWidth(),
                    MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(
                    mFocusedListView.getMeasuredHeight(),
                    MeasureSpec.UNSPECIFIED));

            childView.layout(0, 0, childView.getMeasuredWidth(),
                    childView.getMeasuredHeight());
            childView.setDrawingCacheEnabled(true);
            // setting backgroung color to white
            childView.setBackgroundColor(0xFFFFFFFF);

            Bitmap bb = childView.getDrawingCache();
            bigbitmap2 = overlay(bigbitmap, bb, height);
            // storeImage(bb, "textdcjsajkl"+j+".png");
            height = height + bb.getHeight();
        }
        // Storing Image in SD card
        storeImage(bigbitmap2,
                "BigBitmap" + System.currentTimeMillis() + ".png");

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bigbitmap;

1 个答案:

答案 0 :(得分:0)

我不知道为什么你需要完全这样做,但是你要创建一个大位图(通过首先检索每个listview项高度),然后为每个listview项创建另一个位图以将其添加到大位图......你认为这个过程有很大的可能性来冻结屏幕吗?处理位图需要大量内存。我没有测试过代码,但也许你应该尝试优化整个过程或使用AsyncTask来避免阻塞主线程。

首先,尝试使用此方法检索每个视图的位图:

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

使用AsyncTask进行此任务,而不是在主线程中。