Discussion:
onTouchEvent not called on every touch motion
l***@gmail.com
2011-09-18 10:20:59 UTC
Permalink
I want to let a user smoothly draw elements with his finger and avoid
any lags as follows:

public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mCanvas.drawBitmap(primitive, x, y, mPaint);

invalidate();
break;
case MotionEvent.ACTION_MOVE:
mCanvas.drawBitmap(primitive, x, y, mPaint);

invalidate();
break;
case MotionEvent.ACTION_UP:
mCanvas.drawBitmap(primitive, x, y, mPaint);

invalidate();
break;
}
return true;
}
}

Using android SDK I find onTouchEvent is not called for every motion
event. This results in drawing of a bitmap only at the points when the
motion event is detected.

I can do this very easily using iPhone SDK . Can anyone confirm if
this is a limitation in Android or if there is a way we can increase
the rate of motion events?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
blake
2011-09-18 14:20:17 UTC
Permalink
If you look at the sample code in the Google documents you'll see that
motion events have history. You'll need to use it to get a smooth
trace
-Blake
Programming Android, FTW
Post by l***@gmail.com
I want to let a user smoothly draw elements with his finger and avoid
public boolean onTouchEvent(MotionEvent event) {
                        float x = event.getX();
                        float y = event.getY();
                        switch (event.getAction()) {
                                mCanvas.drawBitmap(primitive, x, y, mPaint);
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y, mPaint);
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y, mPaint);
                                invalidate();
                                break;
                        }
                        return true;
                }
        }
Using android SDK I find onTouchEvent is not called for every motion
event. This results in drawing of a bitmap only at the points when the
motion event is detected.
I can do this very easily using iPhone SDK . Can anyone confirm if
this is a limitation in Android or if there is a way we can increase
the rate of motion events?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Dianne Hackborn
2011-09-18 22:03:48 UTC
Permalink
You don't need the history to do smooth tracking. The history gives you all
of the points between this in the last motion event... but when tracking
you always want to show your object at the most recent reported position, so
the history is irrelevant.

The problem with the code is that it is trying to draw in to mCanvas
directly. The view hierarchy is update-based; you need to update the state
and then invalidate the view drawing it to show the new state. Even if you
could draw to the Canvas outside of an update, the code here would be broken
because it would just leave a trail of bitmaps drawn at various positions
without erasing the previous one. (Though I guess it is doing an
invalidate() after drawing the bitmap, so you'd end up with the bitmap being
briefly drawn and then erased as the invalidate is executed.)

Anyway, just store the new position of the bitmap, call invalidate(), and
then in your onDraw() draw the bitmap at the current position.
Post by blake
If you look at the sample code in the Google documents you'll see that
motion events have history. You'll need to use it to get a smooth
trace
-Blake
Programming Android, FTW
Post by l***@gmail.com
I want to let a user smoothly draw elements with his finger and avoid
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
invalidate();
break;
mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
invalidate();
break;
mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
invalidate();
break;
}
return true;
}
}
Using android SDK I find onTouchEvent is not called for every motion
event. This results in drawing of a bitmap only at the points when the
motion event is detected.
I can do this very easily using iPhone SDK . Can anyone confirm if
this is a limitation in Android or if there is a way we can increase
the rate of motion events?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
***@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails. All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
l***@gmail.com
2011-09-19 04:05:43 UTC
Permalink
Will it not just move the element as I move my finger on the screen. I
want to draw the element at all positions where my finger has touched
so that I can see a continuous drawing.

Let me know if I have misunderstood.
You don't need the history to do smooth tracking.  The history gives you all
of the points between this in the last motion event...  but when tracking
you always want to show your object at the most recent reported position, so
the history is irrelevant.
The problem with the code is that it is trying to draw in to mCanvas
directly.  The view hierarchy is update-based; you need to update the state
and then invalidate the view drawing it to show the new state.  Even if you
could draw to the Canvas outside of an update, the code here would be broken
because it would just leave a trail of bitmaps drawn at various positions
without erasing the previous one.  (Though I guess it is doing an
invalidate() after drawing the bitmap, so you'd end up with the bitmap being
briefly drawn and then erased as the invalidate is executed.)
Anyway, just store the new position of the bitmap, call invalidate(), and
then in your onDraw() draw the bitmap at the current position.
Post by blake
If you look at the sample code in the Google documents you'll see that
motion events have history.  You'll need to use it to get a smooth
trace
-Blake
Programming Android, FTW
Post by l***@gmail.com
I want to let a user smoothly draw elements with his finger and avoid
public boolean onTouchEvent(MotionEvent event) {
                        float x = event.getX();
                        float y = event.getY();
                        switch (event.getAction()) {
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                        }
                        return true;
                }
        }
Using android SDK I find onTouchEvent is not called for every motion
event. This results in drawing of a bitmap only at the points when the
motion event is detected.
I can do this very easily using iPhone SDK . Can anyone confirm if
this is a limitation in Android or if there is a way we can increase
the rate of motion events?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
l***@gmail.com
2011-09-19 06:14:41 UTC
Permalink
Historical data for motion events helped! Thank you Blake!
Post by l***@gmail.com
Will it not just move the element as I move my finger on the screen. I
want to draw the element at all positions where my finger has touched
so that I can see a continuous drawing.
Let me know if I have misunderstood.
You don't need the history to do smooth tracking.  The history gives you all
of the points between this in the last motion event...  but when tracking
you always want to show your object at the most recent reported position, so
the history is irrelevant.
The problem with the code is that it is trying to draw in to mCanvas
directly.  The view hierarchy is update-based; you need to update the state
and then invalidate the view drawing it to show the new state.  Even if you
could draw to the Canvas outside of an update, the code here would be broken
because it would just leave a trail of bitmaps drawn at various positions
without erasing the previous one.  (Though I guess it is doing an
invalidate() after drawing the bitmap, so you'd end up with the bitmap being
briefly drawn and then erased as the invalidate is executed.)
Anyway, just store the new position of the bitmap, call invalidate(), and
then in your onDraw() draw the bitmap at the current position.
Post by blake
If you look at the sample code in the Google documents you'll see that
motion events have history.  You'll need to use it to get a smooth
trace
-Blake
Programming Android, FTW
Post by l***@gmail.com
I want to let a user smoothly draw elements with his finger and avoid
public boolean onTouchEvent(MotionEvent event) {
                        float x = event.getX();
                        float y = event.getY();
                        switch (event.getAction()) {
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                        }
                        return true;
                }
        }
Using android SDK I find onTouchEvent is not called for every motion
event. This results in drawing of a bitmap only at the points when the
motion event is detected.
I can do this very easily using iPhone SDK . Can anyone confirm if
this is a limitation in Android or if there is a way we can increase
the rate of motion events?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
l***@gmail.com
2011-09-27 12:26:29 UTC
Permalink
Problem is not yet resolved :(

I'm able to draw my bitmaps in a continuous path, but the rendering is
very slow.

I am taking history points of a motion event into account but these
also do not give me all the points. I'm introducing more points if the
distance between 2 history points is greater than say 10. Number of
points depend on the distance between two history points and the
radius of the bitmap to be drawn.

Radius of bitmap can vary between 2x2 and 30x30.

There are considerable lags if these many bitmaps are drawn and if I
reduce the number of points, the curve is not smooth.

There is bug reported in the android bug base:
http://code.google.com/p/android/issues/detail?id=1740

Could it be because of the same reason?

Is there a better way of doing it? Is it possible to improve
performance using a canvas?
Post by l***@gmail.com
Historical data for motion events helped! Thank you Blake!
Post by l***@gmail.com
Will it not just move the element as I move my finger on the screen. I
want to draw the element at all positions where my finger has touched
so that I can see a continuous drawing.
Let me know if I have misunderstood.
You don't need the history to do smooth tracking.  The history gives you all
of the points between this in the last motion event...  but when tracking
you always want to show your object at the most recent reported position, so
the history is irrelevant.
The problem with the code is that it is trying to draw in to mCanvas
directly.  The view hierarchy is update-based; you need to update the state
and then invalidate the view drawing it to show the new state.  Even if you
could draw to the Canvas outside of an update, the code here would be broken
because it would just leave a trail of bitmaps drawn at various positions
without erasing the previous one.  (Though I guess it is doing an
invalidate() after drawing the bitmap, so you'd end up with the bitmap being
briefly drawn and then erased as the invalidate is executed.)
Anyway, just store the new position of the bitmap, call invalidate(), and
then in your onDraw() draw the bitmap at the current position.
Post by blake
If you look at the sample code in the Google documents you'll see that
motion events have history.  You'll need to use it to get a smooth
trace
-Blake
Programming Android, FTW
Post by l***@gmail.com
I want to let a user smoothly draw elements with his finger and avoid
public boolean onTouchEvent(MotionEvent event) {
                        float x = event.getX();
                        float y = event.getY();
                        switch (event.getAction()) {
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                                mCanvas.drawBitmap(primitive, x, y,
mPaint);
Post by l***@gmail.com
                                invalidate();
                                break;
                        }
                        return true;
                }
        }
Using android SDK I find onTouchEvent is not called for every motion
event. This results in drawing of a bitmap only at the points when the
motion event is detected.
I can do this very easily using iPhone SDK . Can anyone confirm if
this is a limitation in Android or if there is a way we can increase
the rate of motion events?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Dianne Hackborn
Android framework engineer
Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-***@googlegroups.com
To unsubscribe from this group, send email to
android-developers+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
Loading...