1

这是我的代码。我正在获取空值。我从早上开始就试图找出解决方案,我使用 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();
4

1 回答 1

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);
于 2013-06-18T12:00:08.483 回答