[英]how to make onDraw drawing smoother
I am moving an image from top of the screen till it reaches the bottom of the screen to give it a dropping pin like effect. 我正在将图像从屏幕顶部移到屏幕底部,以使其具有类似效果的下垂图钉。 It is working fine but the problem is with the animating effects.
它工作正常,但问题在于动画效果。 I want this to be as smooth as possible but I can easily find jerks in this dropping effect.
我希望它尽可能平滑,但在这种下降效果中我可以轻松找到混蛋。
I have used a Surfaceview and a thread to achieve this. 我使用了Surfaceview和一个线程来实现此目的。 Thread keeps on updating the image's position by one pixel and call Surfaceview's onDraw(canvas) method where the image with updated coordinates gets drawn.
线程继续将图像的位置更新一个像素,并调用Surfaceview的onDraw(canvas)方法,在该方法中绘制具有更新坐标的图像。
Here is some code snippet. 这是一些代码片段。 First the drawing thread that keeps on updating my image coordinates by some pixels.
首先,绘图线程不断将我的图像坐标更新一些像素。
while(mRun){
canvas = null;
try{
canvas = mSurfaceHolder.lockCanvas();
//increment the coordinate by 5 pixels
image.getCoordinate.setX(image.getCoordinate.getX + 5);
image.getCoordinate.setY(image.getCoordinate.getY + 5);
synchronized (mSurfaceHolder) {
//call onDraw method to update position on canvas
mPanel.onDraw(canvas);
}
}finally{
if(canvas != null){
mPanel.getSurfaceHolder().unlockCanvasAndPost(canvas);
}
}
}
Then the onDraw method from my SurfaceView class that draws the updated image on the canvas : 然后从我的SurfaceView类中的onDraw方法在画布上绘制更新的图像:
canvas.drawBitmap(image, image.getCoordinate().getX(),image.getCoordinate().getY(), null);
This seems to be simple but there are jerks and I am looking for some ways to avoid these jerks. 这似乎很简单,但是有些混蛋,我正在寻找避免这些混蛋的方法。
Thanks in advance. 提前致谢。
@Saurabh Pareek : Thanks buddy for your nice suggestion. @Saurabh Pareek:谢谢哥们的好建议。
I was testing the same on the emulator. 我在模拟器上进行了测试。 But when I tried to run the same on a Device, it worked fine.
但是,当我尝试在设备上运行相同代码时,它运行良好。 No jerk and pretty nice animation.
没有混蛋,很好看的动画。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.