Android View.getDrawingCache returns null, only null

后端 未结 10 865
遇见更好的自我
遇见更好的自我 2020-11-22 13:06

Would anyone please try to explain to me why

public void addView(View child) {
  child.setDrawingCacheEnabled(true);
  child.setWillNotCacheDrawing(false);
          


        
相关标签:
10条回答
  • 2020-11-22 13:41

    Im using this instead.

    myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Bitmap bitmap = Bitmap.createBitmap(myView.getDrawingCache());
            }
        });
    
    0 讨论(0)
  • 2020-11-22 13:41

    @cV2 and @nininho's answers both work for me.

    One of the other reasons that i found out the hard way was that the view that i had was generating a view that has width and height of 0 (i.e. imagine having a TextView with a string text of an empty string). In this case, getDrawingCache will return null, so just be sure to check for that. Hope that helps some people out there.

    0 讨论(0)
  • 2020-11-22 13:41

    I am trying to creating the n number of images dynamically based on view.

    LayoutInflater infalter=(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addview=infalter.inflate(R.layout.barcode_image_list_row_item, null);
    final ImageView imv=(ImageView) addview.findViewById(R.id.imageView1);
    final TextView tv=(TextView) addview.findViewById(R.id.textView1);
    
    try {         
        final Bitmap bitmap = encodeAsBitmap(""+value, BarcodeFormat.CODE_128, 600, 300);
    
        if (bitmap != null) {
            // TODO Auto-generated method stub
            imv.setImageBitmap(bitmap);
            tv.setText(value);
    
            addview.setDrawingCacheEnabled(true);
    
            // this is the important code :)  
            // Without it the view will have a dimension of 0,0 and the bitmap will be null          
            addview.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            addview.layout(0, 0, addview.getMeasuredWidth(), addview.getMeasuredHeight()); 
    
            addview.buildDrawingCache(true);
            Bitmap b = Bitmap.createBitmap(addview.getDrawingCache());
            addview.setDrawingCacheEnabled(false); // clear drawing cache
            // saving the bitmap   
            savebarcode(b,value);
        }
    } catch (WriterException e) {
        e.printStackTrace();
    }
    

    I think this code will help someone..

    0 讨论(0)
  • 2020-11-22 13:45

    The basic reason one gets nulls is that the view is not dimentioned. All attempts then, using view.getWidth(), view.getLayoutParams().width, etc., including view.getDrawingCache() and view.buildDrawingCache(), are useless. So, you need first to set dimensions to the view, e.g.:

    view.layout(0, 0, width, height);
    

    (You have already set 'width' and 'height' as you like or obtained them with WindowManager, etc.)

    0 讨论(0)
提交回复
热议问题