10

重複の可能性:
ビューを Android に表示せずにビットマップに変換しますか?

次の参照リンクからビューをビットマップに変換しようとしています

リンクテキスト

今問題は、ビューのみから変換されているビットマップを取得する方法です。この例では、著者はrelativelayout.dispatchDraw(c)を使用していますが、この行はコンパイル時エラーを引き起こしています。

タイプ ViewGroup のメソッド dispatchDraw(Canvas) が表示されない

ここに私のコードがあり、 onCreate 関数内に次のコードを書きました

    Canvas c=null;

    //Create Layout
    RelativeLayout relativeView ;           
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT
    );

    relativeView = new RelativeLayout(this);            
    relativeView.setLayoutParams(lp);

    //Background of Layout
    Bitmap viewBgrnd  = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack);
    relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd));

    //associated with canvas 
    Bitmap returnedBitmap =               Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);     
    c = new Canvas(returnedBitmap);
    Paint paint = new Paint();


    //Create Imageview that holds image             
    ImageView newImage = new ImageView(this);
    Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink);
    newImage.setImageBitmap(srcBitmap);

    TextView newText = new  TextView(this);

    newText.setText("This is the text that its going to appear");       

    c.drawBitmap(viewBgrnd, 0, 0, paint);
            relativeView.layout(100, 0, 256, 256);  
    relativeView.addView(newImage);
    relativeView.addView(newText);


        // here i am getting compile time error so for timing i have replaced this line
        // with relativeView.draw(c);

    relativeView.dispatchDraw(c);

ここで、returnedBitmap には (ImageView および TextView) のイメージが含まれている必要がありますが、このビットマップには、relativeView の bacground ビットマップ、つまり bgblack のみが含まれています。

4

2 に答える 2

28

ここに私の解決策があります:

public static Bitmap getBitmapFromView(View view) {
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        bgDrawable.draw(canvas);
    else 
        canvas.drawColor(Color.WHITE);
    view.draw(canvas);
    return returnedBitmap;
}

楽しみ :)

于 2012-03-07T04:50:58.247 に答える
6

これは私のために働く:

Bitmap viewCapture = null;

theViewYouWantToCapture.setDrawingCacheEnabled(true);

viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());

theViewYouWantToCapture.setDrawingCacheEnabled(false);

ビューが表示されている必要があると思います (つまり、getVisiblity() == View.VISIBLE)。キャプチャしようとしていると同時にユーザーに非表示にしようとしている場合は、画面の外に移動するか、その上に何かを置くことができます。

于 2011-02-01T02:01:46.610 に答える