Get Android view as Bitmap to save it on SD

前端 未结 1 356
孤街浪徒
孤街浪徒 2021-01-26 04:23

I\'m trying to save the path that is being drawn on a View but I just can\'t figure out how to do this.

Here is what I\'m doing in my Activity to create the View and set

1条回答
  •  礼貌的吻别
    2021-01-26 04:48

    You may try this:

    public static Bitmap getBitmapFromView(View view) {
        //Define a bitmap with the same size as the view
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        //Bind a canvas to it
        Canvas canvas = new Canvas(returnedBitmap);
        //Get the view's background
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null)
            //has background drawable, then draw it on the canvas
            bgDrawable.draw(canvas);
        else
            //does not have background drawable, then draw white background on the canvas
            canvas.drawColor(Color.WHITE);
        // draw the view on the canvas
        view.draw(canvas);
        //return the bitmap
        return returnedBitmap;
    }
    

    EDIT:

    How do you the above method?

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        View accPathView = new AccPathView(this, steps);
        setContentView(accPathView);
    
        accPathView.post(new Runnable() {
             public void run() {
                 Bitmap viewBitmap = getBitmapFromView(accPathView);
             }
        });
    
        // your remaining oncreate
    
    }
    

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