How to convert View to Bitmap in android?

前端 未结 4 1585
南笙
南笙 2020-12-21 00:41

I\'m using following line to convert view to bitmap in android.

view.setDrawingCacheEnabled(true);
Bitmap b= view.getDrawingCache(); 

I am

相关标签:
4条回答
  • 2020-12-21 00:47

    This could work

    public static Bitmap getBitmapFromView(View view) {
       Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
       Canvas canvas = new Canvas(returnedBitmap);
       view.layout(0, 0, view.getLayoutParams().width, view.getLayoutParams().height);
       view.draw(canvas);
       return returnedBitmap;
    }
    
    0 讨论(0)
  • 2020-12-21 00:52

    Try This :

    LinearLayout view1 = (LinearLayout) findViewById(R.id.linear1);
    ImageView my_image = (ImageView) findViewById(R.id.my_imageView);
    
    Bitmap b = Bitmap.createBitmap(view1.getWidth(), view1.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    view1.draw(c);
    
    my_image.setImageBitmap(b); 
    

    Hope it helps you.

    Thanks.

    0 讨论(0)
  • 2020-12-21 01:12

    Use below code : just replace yourview value

    YourView view = (YourView )findViewById(R.id.yourViewId);
    
    view.setDrawingCacheEnabled(true);
    
    view.buildDrawingCache();
    
    Bitmap bm = view.getDrawingCache();
    

    Reference

    0 讨论(0)
  • 2020-12-21 01:13

    Activity Class In this Class I Convert the Layout(View) into Image.

    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    
    public class ImageActivity extends Activity {
    
        LinearLayout layout=null;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_convert);
            layout=(LinearLayout)findViewById(R.id.layout);
            ((ImageView)findViewById(R.id.ImageView01)).setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Bitmap map=ConvertToBitmap(layout);
                    Log.v("BitmapObject", map.toString());
                }
            });       
        }
    
        protected Bitmap ConvertToBitmap(LinearLayout layout) {
            Bitmap map;
            layout.setDrawingCacheEnabled(true);
            layout.buildDrawingCache();
            return map=layout.getDrawingCache();
        }
    }
    
    0 讨论(0)
提交回复
热议问题