1
我正试图实现绘制对象的平滑运动。
在这里,绘制由itselfe在Hypotrochoid路径移动的圆。 我设置延迟16毫秒,以获得每秒60帧和每帧的位置。但是我的圈子仍然不流畅。 主要活动如何在表面上绘制平滑的运动
Handler handler = new Handler();
Runnable runnable = new Runnable(){
@Override
public void run(){
masterMotion.tryDrawing();
handler.postDelayed(runnable, 16);
}
};
@Override
public void surfaceCreated(SurfaceHolder holder){
masterMotion = new MasterMotion(MainActivity.this, holder);
handler.postDelayed(runnable, 16);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h){
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
handler.removeCallbacks(runnable);
}
运动
public Motion(int type, int color, int xPos, int yPos, int radius, int totalHeight){
this.type = type;
this.color = color;
this.radius = radius;
xBottom = xPos;
yBottom = yPos;
xTop = xBottom;
yTop = (int) (radius * 0.2 - radius);
xMid = xBottom;
yMid = (int) (radius * 0.2 - radius + totalHeight/2);
xAlt = xBottom;
yAlt=yBottom;
switch(type){
case 0:
innerR = 20;
hR = 10;
hD = 2;
break;
}
}
public void drawMyStuff(final Canvas canvas, final Paint mPaint){
updatePositions();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(color);
canvas.drawCircle(xR, yR, radius, mPaint);
}
public void updatePositions(){
xR =
(float) (xAlt + (innerR - hR) * Math.cos(angle) + hD * Math.cos(
((innerR - hR)/
hR) *
angle
));
yR =
(float) (yAlt + (innerR - hR) * Math.sin(angle) + hD * Math.sin(
((innerR - hR)/
hR) *
angle
));
angle = (angle + 0.03) % (2 * Math.PI);
if(stepCount>=0){
xAlt+=stepX;
yAlt+=stepY;
stepCount--;
}
}
public void goBottom(){
mustMoove=true;
direction =0;
stepX = (xBottom-xAlt)/20;
stepY = (yBottom - yAlt) /20;
stepCount=20;
}
public void goMid(){
mustMoove = true;
direction = 1;
stepX = (xMid - xAlt)/100;
stepY = (yMid - yAlt)/100;
stepCount=100;
}
public void goTop(){
mustMoove = true;
direction = 2;
stepX = (xTop - xAlt)/100;
stepY = (yTop - yAlt)/100;
stepCount=100;
}
}
平滑度将取决于您设置为60 fps的刷新率,而且还取决于对象在每个帧之间传播的距离。尝试减慢圆圈以查看它是否变得平滑。 – Niddro
我用'数量的框架x2'来划分路径,因此步骤变得非常小 – Yarh
而不是使用分数,使用固定数字并尝试降低并查看是否会影响平滑度。 – Niddro