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