本文介绍了在Android的入门零位来自getDrawingCache()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code。我得到空值。我试图找出从早上的解决方案,我已经使用XML文件创建布局,然后我得到的看法位图,但我想它会动态和我创建布局编程参见下文code。
我的看法是出现罚款,但仍获得getDrawingCache()空位图

 的LinearLayout mainlayout =新的LinearLayout(getApplicationContext());
        mainlayout.setOrientation(LinearLayout.VERTICAL);
        mainlayout.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
         ImageView的顶部=新ImageView的(getApplicationContext());
         ImageView的BOT =新ImageView的(getApplicationContext());
           位图底图= BitmapFactory.de codeResource(getResources(),R.drawable.collagebot);
            位图topImage = BitmapFactory.de codeResource(getResources(),R.drawable.collagetop);
            top.setImageBitmap(topImage);
            bot.setImageBitmap(底图);            的LinearLayout midLayout =新的LinearLayout(getApplicationContext());
                        midLayout.setOrientation(LinearLayout.HORIZONTAL);
                        midLayout.setLayoutParams(新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
                                位图midlaybg = BitmapFactory.de codeResource(getResources(),R.drawable.bg);
                                可绘制middrow =新BitmapDrawable(getResources(),midlaybg);
                        midLayout.setBackground(middrow);                    ImageView的正确=新ImageView的(getApplicationContext());
                    ImageView的左=新ImageView的(getApplicationContext());
                    位图右图= BitmapFactory.de codeResource(getResources(),R.drawable.fb);
                    位图leftImage = BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
                    可绘制rightdrow =新BitmapDrawable(getResources(),右图);
                    可绘制leftdrow =新BitmapDrawable(getResources(),leftImage);
                    right.setBackground(rightdrow);
                    left.setBackground(leftdrow);
                    right.setLayoutParams(新的LayoutParams(150,150));                    left.setLayoutParams(新的LayoutParams(150,150));
                    left.setLayoutParams(新LinearLayout.LayoutParams(150,150,10));
                    midLayout.setPadding(50,0,0,0);
                    midLayout.addView(右);
                    midLayout.addView(左);                    mainlayout.addView(顶部);
                    mainlayout.addView(midLayout);
                    mainlayout.addView(BOT);                    位图mainlaybg = BitmapFactory.de codeResource(getResources(),R.drawable.bg);
                    可绘制maindrow =新BitmapDrawable(getResources(),mainlaybg);
                    mainlayout.setBackground(maindrow);                     mainlayout.setDrawingCacheEnabled(真);
                        mainlayout.buildDrawingCache();                 //的setContentView(mainlayout); //我不想的setContentView
                    Log.e(ceche,+ mainlayout.getDrawingCache()); //这里我得到mainlayout鉴于空位图
                     位图IMGB = mainlayout.getDrawingCache();


解决方案

  mainlayout.measure(MeasureSpec.makeMeasureSpec(
            mainlayout.getLayoutParams()。width方法,MeasureSpec.EXACTLY)
            MeasureSpec.makeMeasureSpec(
                    mainlayout.getLayoutParams()的高度,
                    MeasureSpec.EXACTLY));
    mainlayout.layout(0,0,mainlayout.getMeasuredWidth(),
            mainlayout.getMeasuredHeight());    mainlayout.setDrawingCacheEnabled(真);
    最后的位图bitmap_ =位图
            .createBitmap(mainlayout.getDrawingCache());
    mainlayout.setDrawingCacheEnabled(真);

This is my code. I am Getting null value. i was trying to find out the solution since morning, i have created layout by using xml file then i got the bitmap of view, but i want it dynamically and I have created layout programatically see bellow code.My view is appearing fine but still 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();
解决方案
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);

这篇关于在Android的入门零位来自getDrawingCache()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 16:53