得到绘图缓存总是null android

时间:2013-12-05 02:57:58

标签: android image

我有这样的代码,

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.RGBA_8888);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.splash2);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


        image_book = (RelativeLayout) findViewById(R.id.layout_image_book);
        harga_book = (TextView) findViewById(R.id.tv_harga_book);
        nama_book = (TextView) findViewById(R.id.tv_nama_book);
        des_book = (TextView) findViewById(R.id.tv_des_book);
        frmCaptureThis = (RelativeLayout) findViewById(R.id.layout_book);

        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        final Cursor cur = db.getFood();

        if (cur.getCount() != 0) {

            if (cur.moveToFirst()) {
                do {

                    food_name = cur.getString(1);
                    food_price = cur.getString(2);
                    food_des = cur.getString(3);
                    food_kalori = cur.getString(4);
                    nama_book.setText(cur.getString(1));
                    String value = cur.getString(2);
                    char chars[] = value.toCharArray();
                    if (value.length() > 2) {
                        value = Character.toString(chars[0])
                                + Character.toString(chars[1]) + ","
                                + Character.toString(chars[2]);
                        Log.e("value", value);
                    }
                    Log.e("food name", cur.getString(1));
                    harga_book.setText(value);

                    des_book.setText(cur.getString(3));

                    Drawable d = Drawable.createFromPath(cur.getString(5));
                    Log.e("path image", cur.getString(5));

                    if (Build.VERSION.SDK_INT >= 16) {
                        image_book.setBackground(d);
                    } else {
                        image_book.setBackgroundDrawable(d);
                    }

                    frmCaptureThis.post(new Runnable(){
                        @Override
                        public void run() {
                            Bitmap bmp = merge();
                            saveToLocal(bmp);
                            i++;
                        }
                    });



                } while (cur.moveToNext());

            }
            cur.close();

        }

        db.close();    

        }

此代码在内容相对布局中设置视图,如textView,并设置相对布局的背景,在此视图设置后,我想使用merge()使此relativelayout父级成为图像。这是方法merge()

public Bitmap merge() {


        View v1 = frmCaptureThis;

        v1.setDrawingCacheEnabled(true);
        Bitmap merge = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);

        return merge;
    }

frmCapture这是relativelayout parent,但为什么,总是在v1.getDrawingCache中返回null,请问任何解决方案?感谢

1 个答案:

答案 0 :(得分:0)

问题可能是因为layout尚未测量和绘制。请尝试以下方式:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFormat(PixelFormat.RGBA_8888);
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.splash2);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


    image_book = (RelativeLayout) findViewById(R.id.layout_image_book);
    harga_book = (TextView) findViewById(R.id.tv_harga_book);
    nama_book = (TextView) findViewById(R.id.tv_nama_book);
    des_book = (TextView) findViewById(R.id.tv_des_book);
    frmCaptureThis = (RelativeLayout) findViewById(R.id.layout_book);

    frmCaptureThis.post(new Runnable(){
        @Override
        public void run() {
            processFromDatabase();
        }
    });
}

private void processFromDatabase() {
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());
    final Cursor cur = db.getFood();

    if (cur.getCount() != 0) {
        if (cur.moveToFirst()) {
            do {
                food_name = cur.getString(1);
                food_price = cur.getString(2);
                food_des = cur.getString(3);
                food_kalori = cur.getString(4);
                nama_book.setText(cur.getString(1));
                String value = cur.getString(2);
                char chars[] = value.toCharArray();
                if (value.length() > 2) {
                    value = Character.toString(chars[0])
                            + Character.toString(chars[1]) + ","
                            + Character.toString(chars[2]);
                    Log.e("value", value);
                }
                Log.e("food name", cur.getString(1));
                harga_book.setText(value);

                des_book.setText(cur.getString(3));

                Drawable d = Drawable.createFromPath(cur.getString(5));
                Log.e("path image", cur.getString(5));

                if (Build.VERSION.SDK_INT >= 16) {
                    image_book.setBackground(d);
                } else {
                    image_book.setBackgroundDrawable(d);
                }

                Bitmap bmp = merge();
                saveToLocal(bmp);
                i++;        

            } while (cur.moveToNext());
        }
        cur.close();
    }
    db.close();    

}