从android中的getDrawingCache()获取Null位图

时间:2013-06-18 11:51:18

标签: android android-layout

这是我的代码。我正在获得空值。我试图从早上找到解决方案,我已经使用xml文件创建了布局,然后我得到了视图的位图,但我想要它动态地I have created layout programatically看到下面的代码。 我的观点看似很好,但仍然是getting getDrawingCache() null bitmap

 LinearLayout mainlayout = new LinearLayout(getApplicationContext());
        mainlayout.setOrientation(LinearLayout.VERTICAL);
        mainlayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
         ImageView top= new ImageView(getApplicationContext());
         ImageView bot= new ImageView(getApplicationContext());
           Bitmap bottomImage = BitmapFactory.decodeResource(getResources(), R.drawable.collagebot);
            Bitmap topImage =BitmapFactory.decodeResource(getResources(), R.drawable.collagetop);
            top.setImageBitmap(topImage);
            bot.setImageBitmap(bottomImage);

            LinearLayout midLayout = new LinearLayout(getApplicationContext());
                        midLayout.setOrientation(LinearLayout.HORIZONTAL);
                        midLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                                Bitmap midlaybg =BitmapFactory.decodeResource(getResources(), R.drawable.bg);
                                Drawable middrow = new BitmapDrawable(getResources(),midlaybg);
                        midLayout.setBackground(middrow);

                    ImageView right= new ImageView(getApplicationContext());
                    ImageView left= new ImageView(getApplicationContext());
                    Bitmap rightImage = BitmapFactory.decodeResource(getResources(), R.drawable.fb);
                    Bitmap leftImage =BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                    Drawable rightdrow = new BitmapDrawable(getResources(),rightImage);
                    Drawable leftdrow = new BitmapDrawable(getResources(),leftImage);
                    right.setBackground(rightdrow);
                    left.setBackground(leftdrow);
                    right.setLayoutParams(new LayoutParams(150,150));

                    left.setLayoutParams(new LayoutParams(150,150));
                    left.setLayoutParams(new LinearLayout.LayoutParams(150, 150, 10));
                    midLayout.setPadding(50, 0, 0, 0);
                    midLayout.addView(right);
                    midLayout.addView(left);

                    mainlayout.addView(top);
                    mainlayout.addView(midLayout);
                    mainlayout.addView(bot);

                    Bitmap mainlaybg =BitmapFactory.decodeResource(getResources(), R.drawable.bg);
                    Drawable maindrow = new BitmapDrawable(getResources(),mainlaybg);
                    mainlayout.setBackground(maindrow);

                     mainlayout.setDrawingCacheEnabled(true);
                        mainlayout.buildDrawingCache();

                 // setContentView(mainlayout); // i dont want to setContentView


                    Log.e("ceche",""+mainlayout.getDrawingCache());// here i am getting null bitmap of mainlayout view
                     Bitmap imgb=mainlayout.getDrawingCache();

1 个答案:

答案 0 :(得分:4)

mainlayout.measure(MeasureSpec.makeMeasureSpec(
            mainlayout.getLayoutParams().width, MeasureSpec.EXACTLY),
            MeasureSpec.makeMeasureSpec(
                    mainlayout.getLayoutParams().height,
                    MeasureSpec.EXACTLY));
    mainlayout.layout(0, 0, mainlayout.getMeasuredWidth(),
            mainlayout.getMeasuredHeight());

    mainlayout.setDrawingCacheEnabled(true);
    final Bitmap bitmap_ = Bitmap
            .createBitmap(mainlayout.getDrawingCache());
    mainlayout.setDrawingCacheEnabled(true);