99

誰か私に理由を説明してくれませんか

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 に設定しますか?

キャッシュを設定する前に実際にビューを描画する必要がありますか?

ありがとう!

4

10 に答える 10

245

私もこの問題を抱えていて、この答えを見つけました:

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
于 2011-01-06T17:58:12.997 に答える
61

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

于 2012-08-13T15:25:15.357 に答える
6

null を取得する基本的な理由は、ビューが言及されていないことです。その後、view.getWidth()、view.getLayoutParams().width など (view.getDrawingCache() および view.buildDrawingCache() を含む) を使用するすべての試みは役に立ちません。したがって、最初にビューにディメンションを設定する必要があります。たとえば、次のようになります。

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

(「width」と「height」は既に好きなように設定されているか、WindowManager などで取得されています)

于 2016-03-13T10:21:40.403 に答える
4

代わりにこれを使用しています。

myView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap bitmap = Bitmap.createBitmap(myView.getDrawingCache());
        }
    });
于 2015-06-02T10:47:15.313 に答える
3

ベスト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);
于 2016-08-21T12:11:40.437 に答える
2

キャッチしたいビューが実際に画面に表示されるが、null を返す場合。つまり、ウィンドウ マネージャーがビューを生成する前にビューをキャッチします。一部のレイアウトは非常に複雑です。レイアウトにネストされたレイアウト、layout_weight などが含まれている場合、relayout が正確なサイズを取得する原因となります。最良の解決策は、ウィンドウ マネージャーがジョブを終了してからスクリーン ショットを取得するまで待機することです。ハンドラーに getDrawingCache() を入れてみてください。

于 2013-05-15T21:42:21.987 に答える
1

@ cV2 と @nininho の答えはどちらも私にとってはうまくいきます。

私が難しい方法を見つけた他の理由の 1 つは、私が持っていたビューが幅と高さが 0 のビューを生成していたことです (つまり、空の文字列の文字列テキストを持つ TextView があると想像してください)。この場合、getDrawingCache は null を返すので、必ず確認してください。それが何人かの人々を助けることを願っています。

于 2015-06-27T07:46:46.617 に答える
1

エラーは、ビューが大きすぎて描画キャッシュに収まらないことが原因である可能性があります。

ログから、「View.getDrawingCache returns null」問題の説明を取得しました。

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

Android のドキュメントにも次のように書かれいますそのため、大きなビットマップをロードできません。

于 2016-04-13T11:02:00.420 に答える
1

ビューに基づいて n 個の画像を動的に作成しようとしています。

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

このコードは誰かに役立つと思います..

于 2016-04-01T07:39:47.873 に答える