0

我正在尝试从 ListView 项目中创建位图。我正在使用 Greendroid 库(如果这有什么不同的话)。我最初让这个项目适用于项目数量 < 7 的列表(因为你只能在屏幕上看到 6 个项目)。我现在尝试在获取绘图缓存之前单独构建每个视图项。在下面的代码中,我在 childView.buildDrawingCache() 处得到一个空指针异常。我正确调用 getView() 吗?这个问题还有其他简单的解决方案吗?

try {

                Bitmap main = BitmapFactory.decodeResource(getResources(),R.drawable.dummy);
                Bitmap dummy = BitmapFactory.decodeResource(getResources(),R.drawable.dummy);
                Bitmap view;
                Bitmap temp;
                Canvas canvas = new Canvas();
                Paint paint = new Paint();
                Matrix matrix = new Matrix();
                paint.setFilterBitmap(true);
                File pic = new File(getFilesDir(),"txtabot.jpg");
                //(ViewGroup) (getListView().getParent());
                //((LinearLayout) getListView().getParent()).setDrawingCacheEnabled(true);

                ListView lView = (ListView) getListView();

                for (int i = 0; i<lView.getCount();i++) {
                    lView.setSelectionFromTop(i, 0);
                    View childView = adapter.getView(i, null ,lView );
                    childView.setDrawingCacheEnabled(true);
                    childView.buildDrawingCache(true);
                    view = childView.getDrawingCache().copy(Bitmap.Config.ARGB_8888, true);

                    //ItemView iView = ((ItemView)lView.getItemAtPosition(i));
                    //lView.getChildAt(i).setDrawingCacheEnabled(true);
                    //lView.getChildAt(i).buildDrawingCache(true);
                    //view = lView.getChildAt(i).getDrawingCache().copy(Bitmap.Config.ARGB_8888, true);
                    temp = Bitmap.createScaledBitmap(dummy, view.getWidth(), main.getHeight()+view.getHeight(), true);
                    temp = temp.copy(Bitmap.Config.ARGB_8888, true);
                    canvas.setBitmap(temp);
                    canvas.drawBitmap(main, matrix, paint);
                    //matrix.preTranslate(0, main.getHeight()-view.getHeight());
                    matrix.postTranslate(0, main.getHeight());
                    canvas.drawBitmap(view, matrix, paint);
                    matrix.reset();
                    main = temp.copy(Bitmap.Config.ARGB_8888, true);    
                }
                //getListView().getChildAt(getListView().getCount());
                //Bitmap main = getListView().getDrawingCache().copy(Bitmap.Config.ARGB_8888, true);
                main.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(pic));

                Log.i("TAG", "ofter FOS.CLOSE()");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

这是堆栈跟踪:

10-05 10:46:51.101: D/AndroidRuntime(16836): Shutting down VM
10-05 10:46:51.101: W/dalvikvm(16836): threadid=1: thread exiting with uncaught exception (group=0x40a481f8)
10-05 10:46:51.140: E/AndroidRuntime(16836): FATAL EXCEPTION: main
10-05 10:46:51.140: E/AndroidRuntime(16836): java.lang.NullPointerException
10-05 10:46:51.140: E/AndroidRuntime(16836):    at com.gigabites.smsbot.LogActivity.onHandleActionBarItemClick(LogActivity.java:153)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at greendroid.app.GDActivity$1.onActionBarItemClicked(GDActivity.java:387)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at greendroid.widget.ActionBar$1.onClick(ActionBar.java:396)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at android.view.View.performClick(View.java:3511)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at android.view.View$PerformClick.run(View.java:14105)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at android.os.Handler.handleCallback(Handler.java:605)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at android.os.Looper.loop(Looper.java:137)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at android.app.ActivityThread.main(ActivityThread.java:4575)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at java.lang.reflect.Method.invokeNative(Native Method)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at java.lang.reflect.Method.invoke(Method.java:511)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-05 10:46:51.140: E/AndroidRuntime(16836):    at dalvik.system.NativeStart.main(Native Method)
10-05 10:46:51.710: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:51.718: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:52.226: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:52.230: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:52.726: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:52.730: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:53.230: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:53.230: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:53.730: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:53.753: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:54.238: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:54.253: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:54.730: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:54.738: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:55.230: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:55.234: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:55.742: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:55.742: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:56.234: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:56.238: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:57.074: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:57.082: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:57.238: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:57.253: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:57.738: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:57.742: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:58.250: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:58.250: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:58.750: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:58.750: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:59.238: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:59.242: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:46:59.746: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:46:59.746: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:47:00.246: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:47:00.257: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:47:00.742: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:47:00.753: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:47:01.582: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:47:01.585: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'
10-05 10:47:02.105: I/dalvikvm(16836): threadid=3: reacting to signal 3
10-05 10:47:02.113: I/dalvikvm(16836): Wrote stack traces to '/data/anr/traces.txt'

更新:现在我正在膨胀一个视图并将其绘制到画布(位图)并做同样的事情,但一切都很奇怪。我不太了解 itemView.measure() 和 itemView.layout 方法。这是一种有效的代码:

try {
                // Prepare Utilities
                Bitmap main = BitmapFactory.decodeResource(getResources(),R.drawable.dummy);
                Bitmap dummy = BitmapFactory.decodeResource(getResources(),R.drawable.dummy);
                Bitmap view;
                Bitmap temp;
                //Canvas tempCanvas = new Canvas();
                Canvas canvas = new Canvas();
                Paint paint = new Paint();
                Matrix matrix = new Matrix();
                paint.setFilterBitmap(true);
                File pic = new File(getFilesDir(),"txtabot.jpg");


                final ListView lView = (ListView) getListView();
                //for (int i = 0; i<lView.getChildCount();i++) {
                for (int i = 0; i<3;i++) {

                    //View itemView = lView.getAdapter().getView(i, null, lView);
                    //itemView = lView.getChildAt(i);
                    //itemView.setDrawingCacheEnabled(true);
                    //itemView.draw(tempCanvas);
                    //final int viewheight = lView.getChildAt(lView.getFirstVisiblePosition()).getHeight();
                    View itemView = lView.getAdapter().getView(i, null, lView);
                    itemView.setLayoutParams(new LayoutParams(
                            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                    itemView.measure(
                            MeasureSpec.getSize(itemView.getMeasuredWidth()),
                            MeasureSpec.getSize(itemView.getMeasuredHeight())
                            );
                    itemView.layout(0, 0,itemView.getMeasuredWidth() , itemView.getMeasuredHeight());
                    view = Bitmap.createScaledBitmap(dummy,itemView.getMeasuredWidth(),itemView.getMeasuredHeight(),true);
                    canvas.setBitmap(view);
                    itemView.draw(canvas);
                    //iew.setSelectionFromTop(i, 0);
                    //View itemView = lView.getChildAt(i);
                    //itemView.setDrawingCacheEnabled(true);
                    //view = itemView.getDrawingCache().copy(Bitmap.Config.ARGB_8888, true);

                    temp = Bitmap.createScaledBitmap(dummy, view.getWidth(), main.getHeight()+view.getHeight(), true);
                    //temp = temp.copy(Bitmap.Config.ARGB_4444, true);
                    canvas.setBitmap(temp);
                    canvas.drawBitmap(main, matrix, paint);
                    matrix.postTranslate(0, main.getHeight());
                    canvas.drawBitmap(view, matrix, paint);
                    matrix.reset();
                    main = temp.copy(Bitmap.Config.ARGB_8888, true);    
                }
                main.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(pic));

                Log.i("TAG", "ofter FOS.CLOSE()");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

它设法遍历列表项,但输出全黑,带有一些可见的视图图标。

4

0 回答 0