I've made a simple SurfaceView animation. I first draw a stroked white
rectangle, and then on the next draw, draw a black filled rectangle to hide
it, using the same rectangle. Then I move the rectangle object's
coordinates a bit, redraw the white rectangle and so on. Problem is, this
doesn't work as I'd expect it to.
1) A part of the old rectangle remains visible. At the top it's not
connected to the newly drawn rectangle.
2) On every other step, everything moves a few pixels forward, including
the remnants of the old rectangles.
3) On every other step, a new rectangle remnant appears, everything moves
back to its former location, and the white lines become two pixels wide.
4) Occasionally there are extra lines outside the rectangle, which are left
visible as the rectangle moves forward.
This effect disappears when I use a larger black rectangle to paint over
the moving rectangle. I really don't know what's happening here...
Here's the code:
private void doDraw( Canvas canvas ) {
Paint fillBlack = new Paint();
fillBlack.setARGB( 255, 0, 0, 0 );
fillBlack.setStyle( Style.FILL );
canvas.drawRect( currentRect, fillBlack );
if (dstX > currentRect.left)
currentRect.left = Math.min( dstX, currentRect.left + 5 );
else if (dstX < currentRect.left)
currentRect.left = Math.max( dstX, currentRect.left - 5 );
else if (dstY > currentRect.top)
currentRect.top = Math.min( dstY, currentRect.top + 5 );
else if (dstY < currentRect.top)
currentRect.top = Math.max( dstY, currentRect.top - 5 );
else if (currentRect.left > 0 && currentRect.top == 0)
dstY = viewHeight - imageHeight;
else if (currentRect.left > 0 && currentRect.top > 0)
dstX = 0;
else if (currentRect.left == 0 && currentRect.top > 0)
dstY = 0;
else if (currentRect.left == 0 && currentRect.top == 0)
dstX = viewWidth - imageWidth;
currentRect.right = currentRect.left + imageWidth;
currentRect.bottom = currentRect.top + imageHeight;
Paint strokeWhite = new Paint();
strokeWhite.setARGB( 255, 255, 255, 255 );
strokeWhite.setStyle( Style.STROKE );
canvas.drawRect( currentRect, strokeWhite );
}
A frame, the next frame and a screenshot with tearing that shows how the
image jumps forward on every other step. These are from an emulator, but
same thing happens on a device.
<https://lh6.googleusercontent.com/-uE6_xE997yY/T5gfNkcPhbI/AAAAAAAAAAM/jwzu5Dt4whM/s1600/step_1.png><https://lh5.googleusercontent.com/-ot9qWdU7mQY/T5gfZNYQ88I/AAAAAAAAAAY/QOaEqsw0Uc8/s1600/step_2.png><https://lh3.googleusercontent.com/-f7TFS_yKckY/T5gfsyNLicI/AAAAAAAAAAk/a-Px_ZG-oMc/s1600/tearing.png>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en