Question

I have created panel with collection of different colors, pencil, eraser and different shapes similar to MS-Paint. I could be able to draw or write on a screen using Touch Event method. But when I draw something on a screen (when I touch the screen), MotionEvent.ACTION_Down method is calling. So it works fine. When I release my finger from the screen, MotionEvent.ACTION_up method is calling and works fine.

So, my problem is, like MS-PAINT I am not able to see what I drew or wrote before I release my finger from the screen. For an example, see this video. User can see when he dragged the shapes or trying to draw a pencil. Also, in this link user draws using pencil and it is visible without releasing a finger on the screen.

But, when I draw something on the screen, once I released the finger only it appears.

What I need is, when user touch the screen itself if he/she moves the finger on the screen, user must be able to see what they are trying to draw or write on the screen.

For an example: if I try to write some word like "Apple" on a screen, I am trying to put "A" . But when I write letter "A", it is invisible unless I take my finger from the screen. Once if I released my finger from the screen after I drew letter"A" then only the text or picture has been appeared on a screen what I drew.

So, I have done MotionEvent.ACTION_DOWN and MotionEVent.ACTION_UP. It works fine.

But, MotionEvent.ACTION_MOVE is not working properly at all.

This is my Code,

 @Override
 public boolean onTouchEvent(MotionEvent event) 
 {
 if(event.getAction() == MotionEvent.ACTION_DOWN)
 {
 if(Shape == ShapeLine)
 {
 GraphicObject = new Line();
 ((Line) GraphicObject).getBegin().setX(event.getX());
 ((Line) GraphicObject).getBegin().setY(event.getY());
 }
 if(Shape== ShapeRect)
  { 
  GraphicObject = new Rectangle();
  Point temp = new Point(event.getX(), event.getY());
  endPoint = new Point();
  ((Rectangle) GraphicObject).settemppointOfOneEndRectangle(temp);
  }
 else if(event.getAction() == MotionEvent.ACTION_MOVE){
if(Shape== ShapeLine)
{
final float x=event.getX();
final float y=event.getY();     
}
if(Shape == ShapeRect)
{
 endPoint.x=event.getX();
 endPoint.y=event.getY();
 invalidate();
 }

Anyone suggest me, about ACTION_MOVE. I have tried a lot in a code but no changes and I didn't find any solution while moving.

Was it helpful?

Solution

Basic idea is when you tap record that point in a variable,then inside ACTION_MOVE record the current point and draw a line in between these 2 points.Once done save this point in the previous point. Sudo code:

Point last;
Point current;
...
case ACTION_DOWN:
      last=mouse.position;
      break;
case ACTION_MOVE:
      current=mouse.position;
      drawLine(current,last);
      last=current;
      break;

Do this way,your drawing should be fine. N.B. Remember,this is a sudo code. :P

EDIT. Example from one of my app. Basically I pointed out what you should do:

public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();
        switch(action & MotionEvent.ACTION_MASK)
        {

            case MotionEvent.ACTION_DOWN:
                initial.x=(int)event.getX();
                initial.y=(int)event.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                current.x=(int)event.getX();
                current.y=(int)event.getY();
                //draw line using initial as start and current as end point
                //sudo code: drawLine(initial,current)
                //now set initial to current
                initial=current// for the continuity of drawing.
                break;
        }
        return true;
    }

initial and current both are Point objects.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top