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 9256831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:03:44+00:00 2026-06-18T12:03:44+00:00

Following is my code. I am able to draw lines but the redo and

  • 0

Following is my code. I am able to draw lines but the redo and undo buttons not working. Please can anyone help out.

This is my activity below. Please check the undo and redo methods…

public class Paint_main extends Activity implements OnTouchListener {

    static Paint p1;
    static Paint p2;
    ImageView img;
    Bitmap bm;
    Float startx;
    Float starty;
    Float endx;
    Float endy;
    Button b1;
    EditText et;
    File file; 
    File myDir;
    private static ToggleButton toggleButton;
    private Canvas mCanvas;
    private Path mPath;

    private ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<Path> undonePaths = new ArrayList<Path>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn_undo = (Button) findViewById(R.id.undoButton);

        btn_undo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { onClickUndo(); }
        });

        Button btn_redo = (Button) findViewById(R.id.redoButton);

        btn_redo.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) { onClickRedo(); }
        });

        bm = Bitmap.createBitmap(getWindowManager().getDefaultDisplay().getWidth(),
                                 getWindowManager().getDefaultDisplay().getHeight(),
                                 Bitmap.Config.ARGB_8888);

        img = (ImageView) findViewById(R.id.imageView1);    
        img.setImageBitmap(bm);

        p1 = new Paint();
        p1.setAntiAlias(true);
        p1.setDither(true);
        p1.setStyle(Paint.Style.STROKE);
        p1.setStrokeJoin(Paint.Join.ROUND);
        p1.setStrokeCap(Paint.Cap.ROUND);
        p1.setColor(Color.GREEN);
        p1.setStrokeWidth(6);
        mCanvas = new Canvas(bm);
        mPath = new Path();
        // paths.add(mPath); 
        img.setOnTouchListener(this);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);

        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, p1);
        // kill this so we don't double draw            
        // mPath = new Path();
        // paths.add(mPath);
        paths.add(mPath);
        mPath = new Path();
    }

    public void onClickUndo () { 
        if (paths.size() > 0) { 
            undonePaths.add(paths.remove(paths.size()-1));
            img.invalidate();
            onDraw(mCanvas);
        }
    }

    public void onClickRedo (){
        if (undonePaths.size() > 0) { 
            paths.add(undonePaths.remove(undonePaths.size()-1)); 
            img.invalidate();
            onDraw(mCanvas);
        } 
    }


    public boolean onTouch(View arg0, MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                img.invalidate();
                onDraw(mCanvas);
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                img.invalidate();
                onDraw(mCanvas);
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                img.invalidate();
                onDraw(mCanvas);
                break;
        }
        return true;
    }

    protected void onDraw(Canvas canvas) {
        //mPath = new Path();
        //canvas.drawPath(mPath, mPaint);

        for (Path p : paths) { canvas.drawPath(p, p1); }
        canvas.drawPath(mPath, p1);
    }
}

May be i am calling the above onDraw method incorrectly.

  • 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-06-18T12:03:46+00:00Added an answer on June 18, 2026 at 12:03 pm

    I get it. Problem is: img.setOnTouchListener(this); This get event, process it and return true, which means this event was handlet.

    You have 3 options:

    1) Set onTouchListener to parent view and manually send touch event to childs
    2) You could determine if user touch images return true, otherwise return false
    3) write your own class and override onTouchEvent(..) method

    I recommend 3 option. I rewrite for you:

    package com.example.paint_main;
    
    import java.io.File;
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
    import android.os.Bundle;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    import android.widget.ToggleButton;
    
    public class Paint_main extends Activity  {
    
        MyImageView img;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_paint_main);
    
            img = (MyImageView) findViewById(R.id.imageView1);
            img.init(getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight());
    
            Button btn_undo = (Button) findViewById(R.id.undoButton);
    
            btn_undo.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) { img.onClickUndo(); }
            });
    
            Button btn_redo = (Button) findViewById(R.id.redoButton);
    
            btn_redo.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) { img.onClickRedo(); }
            });
        }
    
        static class MyImageView extends ImageView {
    
            static Paint p1;
            static Paint p2;
            Bitmap bm = null;
            Float startx;
            Float starty;
            Float endx;
            Float endy;
            Button b1;
            EditText et;
            File file; 
            File myDir;
            private static ToggleButton toggleButton;
            private Canvas mCanvas;
            private Path mPath;
    
            private ArrayList<Path> paths = new ArrayList<Path>();
            private ArrayList<Path> undonePaths = new ArrayList<Path>();
    
            static {
                p1 = new Paint();
                p1.setAntiAlias(true);
                p1.setDither(true);
                p1.setStyle(Paint.Style.STROKE);
                p1.setStrokeJoin(Paint.Join.ROUND);
                p1.setStrokeCap(Paint.Cap.ROUND);
                p1.setColor(Color.GREEN);
                p1.setStrokeWidth(6);
            }
    
            public MyImageView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
            }
    
            public MyImageView(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
    
            public MyImageView(Context context) {
                super(context);
            }
    
            public void init(int width, int height) {
                bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                mCanvas = new Canvas(bm);
                mPath = new Path();
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                float x = event.getX();
                float y = event.getY();
    
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touch_start(x, y);
                        this.invalidate();
                        onDraw(mCanvas);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        touch_move(x, y);
                        this.invalidate();
                        onDraw(mCanvas);
                        break;
                    case MotionEvent.ACTION_UP:
                        touch_up();
                        this.invalidate();
                        onDraw(mCanvas);
                        break;
                }
                return true;
            }
    
            private float mX, mY;
            private static final float TOUCH_TOLERANCE = 4;
    
            private void touch_start(float x, float y) {
                mPath.reset();
                mPath.moveTo(x, y);
                mX = x;
                mY = y;
            }
    
            private void touch_move(float x, float y) {
                float dx = Math.abs(x - mX);
                float dy = Math.abs(y - mY);
    
                if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                    mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                    mX = x;
                    mY = y;
                }
            }
    
            private void touch_up() {
                mPath.lineTo(mX, mY);
                // commit the path to our offscreen
                mCanvas.drawPath(mPath, p1);
                // kill this so we don't double draw            
                // mPath = new Path();
                // paths.add(mPath);
                paths.add(mPath);
                mPath = new Path();
            }
    
            public void onClickUndo () { 
                if (paths.size() > 0) { 
                    undonePaths.add(paths.remove(paths.size()-1));
                    invalidate();
                    onDraw(mCanvas);
                }
            }
    
            public void onClickRedo (){
                if (undonePaths.size() > 0) { 
                    paths.add(undonePaths.remove(undonePaths.size()-1)); 
                    invalidate();
                    onDraw(mCanvas);
                } 
            }
    
            protected void onDraw(Canvas canvas) {
                //mPath = new Path();
                //canvas.drawPath(mPath, mPaint);
    
                if (bm == null)
                    return;
    
                for (Path p : paths) { 
                    canvas.drawPath(p, p1); }
                    canvas.drawPath(mPath, p1);
            }
        }
    } 
    

    Don’t forget add your class into layout:

    <view class="com.example.paint_main.Paint_main$MyImageView"
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/redoButton"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />
    

    In addition, you should use View instead of ImageView because you don’t use any specific ImageView functions. I rewrite with ImageView for more easier understanding what I mean in third option.

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

Sidebar

Related Questions

I was able to draw a dashed box, using the following code: CAShapeLayer *shapeLayer
I can draw a hyperlinked shape in tikz using the following code: \documentclass{article} \usepackage{tikz}
With the following code I'm able to draw the plot of a single 2D-Gaussian
I'm able to draw a rectangle with rounded corner with following code. What I'm
the following code is not able to hash the user's password, and it stores
I have the following code through which i am able to retrieve phone numbers.
Using the following code, I'm able to successfully open a raw disk on my
Using the following code, I am able to display up to 10 images. What
Having the following code to draw circle (taken from Google Play Services maps sample):
I am trying to achieve the following http://www.qksnap.com/i/3hunq/4ld0v/screenshot.png I am currently able to draw

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.