0

Im just starting to use touch events. So i search an example and found this http://www.vogella.com/articles/AndroidTouch/article.html.. The tutorial is great but one thing is that when i run the code. I move my fingers in every direction and it is not showing in cavas.. I mean it does not draw something.

here is my code:

public class SingleTouchEventView extends View {
    private Paint paint = new Paint();
    private Path path = new Path();
    public SingleTouchEventView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint.setAntiAlias(true);
        paint.setStrokeWidth(6f);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);

    }


    @Override
    protected void onDraw(Canvas canvas) {
       canvas.drawPath(path,paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                path.moveTo(eventX, eventY);
                return true;
            case MotionEvent.ACTION_MOVE:
                path.lineTo(eventX, eventY);
                break;
            case MotionEvent.ACTION_UP:
                // nothing to do
                break;
            default:
                return false;
        }

        // Schedules a repaint.
        invalidate();
        return true;
    }
}

and my main activty

public class SingleTouchActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SingleTouchEventView(this,null));
    }
}

What am i missing here? Thanks

thenewbie
  • 755
  • 19
  • 40
  • This is a total guess but try not calling `return` on `ACTION_DOWN` – seaplain Jan 24 '13 at 05:50
  • does the code hit your constructor? add in a log. The only reason I say this is because your constructor is set up to be called from `xml`. the call you make looks valid but a simple check can't hurt – seaplain Jan 24 '13 at 06:00
  • I put the log and nothing is showing so i guest i am not calling something. Then there is definitely missing in the code.. Do you know what would be it? – thenewbie Jan 24 '13 at 06:03
  • change the constructor to not have the Attribute set argument and remove the null when you set the content view. That should work... should... Otherwise actually set up a `main.xml` with your custom view in it (this is where the tutorial heads anyway) – seaplain Jan 24 '13 at 06:06
  • Still not working. I dont know how to set it up on the xml cause this is the first time ive used touch event. Ill just search for an example.. thanks – thenewbie Jan 24 '13 at 06:11
  • the api demos you should have gotten when you installed the android SDK have a good exampe. `Android\android-sdk\samples\android-16\ApiDemos\src\com\example\android\apis\graphics\FingerPaint.java` – seaplain Jan 24 '13 at 06:15

2 Answers2

1

try like this

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Let the ScaleGestureDetector inspect all events.
    mScaleDetector.onTouchEvent(ev);

    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN: {
            final float x = ev.getX();
            final float y = ev.getY();
            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = ev.getPointerId(0);
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;

                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;

            break;
        }

        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }

        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                    >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }
            break;
        }
    }    
    return true;
}

For more information check this link

Community
  • 1
  • 1
Janmejoy
  • 2,721
  • 1
  • 20
  • 38
1

Try out as below :

private void touch_up()
{
    path.lineTo(m_X, m_Y);
    // commit the path to our offscreen
    canvas.drawPath(m_path, m_paint);
    // kill this so we don't double draw
    path.reset();
}
private void touch_start(float p_x, float p_y)
{
    path.reset();
    path.moveTo(p_x, p_y);
    m_X = p_x;
    m_Y = p_y;
}   
private void touch_move(float p_x, float p_y)
{
    float m_dx = Math.abs(p_x - m_X);
    float m_dy = Math.abs(p_y - m_Y);
    if (m_dx >= TOUCH_TOLERANCE || m_dy >= TOUCH_TOLERANCE)
    {
        m_path.quadTo(m_X, m_Y, (p_x + m_X) / 2, (p_y + m_Y) / 2);
        m_X = p_x;
        m_Y = p_y;          
        m_pathDrawn = true;
    }       
}

For the onTouchEvent:

@Override
public boolean onTouchEvent(MotionEvent p_event)
{
    float m_x = p_event.getX();
    float m_y = p_event.getY();     
    switch (p_event.getAction())
    {
        case MotionEvent.ACTION_DOWN:
            touch_start(m_x, m_y);
            invalidate();                   
            break;
        case MotionEvent.ACTION_MOVE:
            touch_move(m_x, m_y);
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            touch_up();
            invalidate();                   
            break;
    }
    return true;
}

I hope it will help you.

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
GrIsHu
  • 29,068
  • 10
  • 64
  • 102