简体   繁体   English

围绕中心点旋转一条线

[英]Rotate a line around a center point

I'm currently working on a game with a timer.我目前正在开发一个带有计时器的游戏。

As time passes the redline is supposed to rotate about its center point and when times is over end up at the opposite side.随着时间的流逝,红线应该围绕其中心点旋转,当时间结束时,红线会在另一侧结束。 I currently have the line rotating and it consistently stays the same length but it is moving sporadically and the animation isn't smooth.我目前让生产线旋转,它始终保持相同的长度,但它偶尔移动,animation 不平滑。 I'm not totally sure what I'm doing wrong here.我不完全确定我在这里做错了什么。

startX and Y is the starting end point of the line and endX and Y is the ending point for the line when its at 250 psi startX 和 Y 是线的起点,endX 和 Y 是线在 250 psi 时的终点

Two points about smoothness关于平滑的两点

1) Use an animator of float type not int as I don't think the time on the Update calls is guaranteed to be millisecond accurate, therefore you are forcing it to truncate the value as the value is actually calculated using a fraction (float) between 0 and 1 of the duration 1)使用浮点类型而不是 int 的动画器,因为我认为更新调用的时间不能保证精确到毫秒,因此您强制它截断值,因为该值实际上是使用分数计算的(浮点数)持续时间的 0 到 1 之间

eg In a perfect world the timing sequence of 0 to 20 would begin like:-例如,在一个完美的世界中,从 0 到 20 的时序开始如下:-

  • 0ms = 0 0ms = 0
  • 500ms = 1 500 毫秒 = 1
  • 1000ms = 2 1000 毫秒 = 2

But in reality the timing sequence might begin like:-但实际上,时序可能开始如下:-

  • 0ms = 0 0ms = 0
  • 499ms = 0.999 (when converted to an int would also be 0) 499ms = 0.999(转换为 int 时也为 0)
  • 1000ms = 2 1000 毫秒 = 2

2) Recalculating the start and end point of the line to redraw it might not be the fastest and therefore the smoothest method. 2)重新计算线的起点和终点以重绘它可能不是最快的,因此也是最平滑的方法。

It is probably faster and smoother to set the Rotation of a Canvas that contains the line onAnimationUpdate (with the Canvas pivot point X and Y set to the centre of the dial)将包含 onAnimationUpdate 行的onAnimationUpdate的 Rotation 设置为可能更快更顺畅(将 Canvas pivot 点 X 和 Y 设置为表盘中心)

This is because this is most likely to be hardware accelerated transform这是因为这很可能是硬件加速转换

eg例如

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class Drawline extends View {
    Paint paint = new Paint();

    private void init() {
        paint.setColor(Color.BLACK);
    }

    public Drawline(Context context) {
        super(context);
        init();
    }

    public Drawline(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Drawline(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawLine(100, 100, 150, 150, paint);
    }

}

public class MainActivity extends AppCompatActivity {

    Drawline drawLine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        drawLine = new Drawline(this);
        drawLine.setPivotX(100f);
        drawLine.setPivotY(100f);
        setContentView(drawLine);

        ValueAnimator animator = ValueAnimator.ofFloat(0f, 20f);
        animator.setDuration(10000);
        animator.setInterpolator(new LinearInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float x = (float) animation.getAnimatedValue();

                drawLine.setRotation(x);
                drawLine.invalidate();
            }
        });
        animator.start();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM