code

Android View.getDrawingCache는 null 만 반환합니다.

codestyles 2020. 8. 29. 11:06
반응형

Android View.getDrawingCache는 null 만 반환합니다.


누구든지 이유를 설명해 주시겠습니까

public void addView(View child) {
  child.setDrawingCacheEnabled(true);
  child.setWillNotCacheDrawing(false);
  child.setWillNotDraw(false);
  child.buildDrawingCache();
  if(child.getDrawingCache() == null) { //TODO Make this work!
    Log.w("View", "View child's drawing cache is null");
  }
  setImageBitmap(child.getDrawingCache()); //TODO MAKE THIS WORK!!!
}

항상 그리기 캐시가 null임을 기록하고 비트 맵을 null로 설정합니까?

캐시가 설정되기 전에 실제로 뷰를 그려야합니까?

감사!


나는 또한이 문제가 있었고 다음 답변을 찾았습니다.

v.setDrawingCacheEnabled(true);

// this is the important code :)  
// Without it the view will have a dimension of 0,0 and the bitmap will be null          
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight()); 

v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache

getDrawingCache항상 returning null사람 이라면 다음을 사용하십시오.

public static Bitmap loadBitmapFromView(View v) {
     Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
     Canvas c = new Canvas(b);
     v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
     v.draw(c);
     return b;
}

참조 : https://stackoverflow.com/a/6272951/371749


null을 얻는 기본적인 이유는 뷰가 언급되지 않았기 때문입니다. 그런 다음 view.getDrawingCache () 및 view.buildDrawingCache ()를 포함하여 view.getWidth (), view.getLayoutParams (). width 등을 사용하는 모든 시도는 쓸모가 없습니다. 따라서 먼저 뷰에 치수를 설정해야합니다. 예 :

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

(원하는대로 '너비'와 '높이'를 이미 설정했거나 WindowManager 등으로 획득했습니다.)


대신 이것을 사용하고 있습니다.

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap bitmap = Bitmap.createBitmap(myView.getDrawingCache());
        }
    });

최고의 4me 작업

    Bitmap bitmap = Bitmap.createBitmap( screen.getMeasuredWidth(), screen.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    screen.layout(0, 0, screen.getMeasuredWidth(), screen.getMeasuredHeight());
    screen.draw(canvas);

If the view you want catch really shows on screen, but it return null. That means you catch the view before Window manager generate it. Some layouts are very complicated. If layout includes nested layouts, layout_weight..etc, it causes several times relayout to get exactly size. The best solution is waiting until window manager finish job and then get screen shot. Try to put getDrawingCache() in handler.


@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.


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);

            }else{

            }


        } catch (WriterException e) {
            e.printStackTrace();
        }

I think this code will help someone..


The error may be bacause your View too large to fit into drawing cache.

I got the explanation of my "View.getDrawingCache returns null" problem from my logs:

W/View: View too large to fit into drawing cache, needs 19324704 bytes, only 16384000 available

Android docs also says: Android devices can have as little as 16MB of memory available to a single application. That is why you can not load big bitmap.


This the simple and efficient way to get bitmap

public void addView(View v) {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();

        Bitmap bitmap = v.getDrawingCache();

        if(bitmap == null) {
            // bitmap is null
            // do whatever you want
        } else {
            setImageBitmap(bitmap);
        }
        v.setDrawingCacheEnabled(false);
        v.destroyDrawingCache();
    }

참고URL : https://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null

반응형