Issue
I am trying to modify my Canvas and touch event so that the circles drawn to it eventually fade out while the user is still moving along the canvas.
Where the smaller circles fade out overtime.
I've gone through the majority of similar questions for this that deal with Bitmaps. I've been struggling to implement the tips suggested in those questions as my circle coordinates are stored to an ArrayList which is looped through and drawn, rather than an individual shape/image drawn to the canvas.
Below is what I've written so far:
case MotionEvent.ACTION_MOVE: {
if (inCircle(event, sceneView.circleRadius - 30)) {
int pointer = event.getPointerCount();
Point xyPoint = new Point();
for (int i = 0; i < pointer; i++) {
xInput = (int) event.getX() - (50 / 2);
yInput = (int) event.getY() - (50 / 2);
colorList.add(getCircleColor(sceneView.myBitmap));
xyPoint.x = xInput;
xyPoint.y = yInput;
pointList.add(xyPoint);
}
invalidate();
return true;
}
}
and the onDraw:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < pointList.size(); i++) {
Point p = pointList.get(i);
touchCircle.setColor(colorList.get(i));
canvas.drawCircle(p.x, p.y, touchRadius, touchCircle);
}
}
I've tried to modify the alpha value of the circles in the ArrayList bar the last element to reflect the fade out, but that causes a stuttering effect rather than a smooth fade out of all the previous points.
Apologies if this question comes across as repetitive, I'd just really appreciate some opinions on how to solve this issue.
Please let me know if there's anything I should clarify either.
Solution
If you want a smooth fade out, you need to animate using Handlers
. The idea is, if you want your fade out to be very smooth, you need to call onDraw()
at a very short interval. That's how you will make the fade out smooth. You are facing the stuttering effect because you are not calling onDraw()
enough times.
What you need is a Handler
and you need to post a Runnable
at a very short interval. This way you will invalidate()
multiple times and make the fade out as smooth as you want.
If you want to know how to use a Handler
, you can google it. I have worked on a Pagination control in which I used this, you could refer this as well: https://github.com/Madrapps/pagination/blob/master/src/main/java/com/instrap/apps/pagination/views/MovingDots.java. This has nothing to do with your use case, but will give you an idea on how to use Handlers
and Runnable
to perform animations in a custom view.
Answered By - Henry
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.