如何在表面上绘制平滑的运动

时间:2015-05-29 12:02:58

标签: android drawing android-canvas surfaceview

我正在尝试实现绘制对象的平滑移动 在这里我绘制了由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;
    }

}

1 个答案:

答案 0 :(得分:2)

应用程序无法确定显示器的刷新率。试图"设置"通过睡眠16.7ms的速率达到60fps将产生不稳定的结果。您应该使用Choreographer来获取通知和时间信息,并按实际时间增量而不是固定长度的帧推进动画。

有关Android游戏循环的讨论,请参阅architecture doc

可以在Grafika中找到使用编舞者进行流畅动画的插图;尝试"记录GL应用"活动,即使丢帧也能平滑动画。观看"预定交换"也很有趣。尽管帧速率较低,但可以用来证明30fps在60fps显示器上看起来比48fps更平滑。

使用GLSurfaceView"队列填充"平滑动画方法可以在Android Breakout中看到。它使用自上一次绘制回调以来的时间来确定推进动画的距离,这不如编舞者准确,但足够接近大多数用途。