Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:46:39+00:00 2026-05-30T16:46:39+00:00

So I got a canvas and a layout. On the canvas I draw paint

  • 0

So I got a canvas and a layout.
On the canvas I draw paint while in the layout is the background image being drawn onto.
After painting on the canvas, I would like to save the image drawn together with the layout background. Can anyone help me on how could I possibly do that?
Moreover, when I save the canvas, it only saves a black image.

public class paint  extends Activity implements OnTouchListener{

DrawPanel dp;
private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
private Paint mPaint;
Path path;
protected Button cancel;
protected Button save;
protected FrameLayout fl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    dp = new DrawPanel(this);
    dp.setOnTouchListener(this);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    mPaint = new Paint();
    mPaint.setDither(true);
    mPaint.setColor(Color.WHITE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.MITER);
    mPaint.setStrokeCap(Paint.Cap.BUTT);
    mPaint.setStrokeWidth(5);

    fl = new FrameLayout(this); 
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
            WindowManager.LayoutParams.TYPE_APPLICATION,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSPARENT);
    fl.addView(dp,370,370); 
    this.getWindowManager().addView(fl , lp);
    setContentView(R.layout.boy_crown);


    Button save = new Button(this);
    FrameLayout.LayoutParams params2 = new FrameLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
    params2.topMargin = 375;
    params2.gravity =0;
    params2.rightMargin = 160;
    params2.gravity =0;
    save.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
    save.setPadding(0,0, 130, 60);
    fl.addView(save, params2);
    save.setOnClickListener(new ButtonClickHandler2());

}

public class ButtonClickHandler2 implements View.OnClickListener {
    public void onClick(View view) {

        dp.setDrawingCacheEnabled(true);
        Bitmap bm = dp.getDrawingCache();
        try {
            String path = Environment.getExternalStorageDirectory().toString();
            File imgDirectory = new File("/sdcard/BillysCrown");
            imgDirectory.mkdirs();
            OutputStream fOut = null;
            File file = null;
            String fileid = System.currentTimeMillis() + "";
            file = new File(path, "/BillysCrown/" + fileid + ".png");
             Toast.makeText(paint.this, "saved at: " + file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
            fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    dp.pause();
}



@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    dp.resume();
}



public class DrawPanel extends SurfaceView implements Runnable{

    Thread t = null;
    SurfaceHolder holder;
    boolean isItOk = false ;


    public DrawPanel(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        holder = getHolder();

    }

    @Override
    public void run() {

        // TODO Auto-generated method stub
        while( isItOk == true){

            if(!holder.getSurface().isValid()){
                continue;
            }
            holder.setFormat(PixelFormat.TRANSPARENT);
            Canvas c = holder.lockCanvas();
            onDraw(c);
            holder.unlockCanvasAndPost(c);

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
                    synchronized(pointsToDraw)
      {
        for (Path path : pointsToDraw)
        {
            canvas.drawPath(path, mPaint);
        }
       }
    }

    public void pause(){
        isItOk = false;
        while(true){
            try{
                t.join();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            break;
        }
        t = null;
    }

    public void resume(){
        isItOk = true;  
        t = new Thread(this);
        t.start();

    }


}


@Override
public boolean onTouch(View v, MotionEvent me) {
    // TODO Auto-generated method stub
            synchronized(pointsToDraw)
            {
    if(me.getAction() == MotionEvent.ACTION_DOWN){
        path = new Path();
        path.moveTo(me.getX(), me.getY());
        //path.lineTo(me.getX(), me.getY());
        pointsToDraw.add(path);
    }else if(me.getAction() == MotionEvent.ACTION_MOVE){
        path.lineTo(me.getX(), me.getY());
    }else if(me.getAction() == MotionEvent.ACTION_UP){
        //path.lineTo(me.getX(), me.getY());
    }
    }       
    return true;

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:46:40+00:00Added an answer on May 30, 2026 at 4:46 pm

    I have written a tutorial about how you can save a screenshot from a SurfaceView.

    It is basically the same for each View in Android. Just make sure you have read until the last paragraph:

    That should work with other views, too. The only problem with Views in
    general is, that you need to be sure that they are fully inflated, so
    you can’t create a screenshot of a view while being in the onCreate()
    method. You should work with the ViewTreeObserver to get a callback
    when the view is fully inflated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a circle that I've drawn on a canvas. I'd like to draw
Got a bluescreen in windows while cloning a mercurial repository. After reboot, I now
I've got a canvas drawn (I have to keep the canvas, it's doing other
I've got a Canvas with a few UIElements on. After I've moved them on
I've got an array like this: var hitColors = [#ff0000,#00ff00,#0000ff,#ffff00,#00ffff,#ff00ff]; I've got a canvas
I've got error like jQuery and Canvas.toDataURL Some user told that might run it
I have Encrypted->Decrypted images and draw it to canvas. i got no error when
I've got an Image with an overlay Canvas with some rectangles whose position is
I've got a flowchart-style application drawn on a WPF Canvas. (Basically boxes connected with
I've got some time, and I really would like to learn to get my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.