【问题标题】:Animating a second hand动画秒针
【发布时间】:2014-11-14 17:10:24
【问题描述】:

如何以枢轴为图像底部中心的模拟时钟的秒针制作动画(下面给出的图像链接)。

  1. 目前图像以中心为轴心旋转,但我需要轴心位于图像中红点指示的底部中心。我尝试设置 0.5,1.0 和 1.0,0.5,但图像移动到不同的区域并且没有在正确的点旋转。
  2. 在当前动画下秒针平滑扫动,如何实现类似于传统模拟时钟的停止和动画,停止和动画循环。

我正在使用的代码如下所示

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageView = (ImageView) findViewById(R.id.image);
    final Handler handler = new Handler();
    calendar = Calendar.getInstance();
    seconds = calendar.get(Calendar.SECOND);
    seconds++;
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            Log.i("Canvas", "second is " + seconds);
            // imageView.animate()
            // .setDuration(1000)
            // .rotation(6f * seconds);
            RotateAnimation rotateAnimation = new RotateAnimation(
                    (seconds - 1) * 6, seconds * 6,
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);

            rotateAnimation.setInterpolator(new LinearInterpolator());
            rotateAnimation.setDuration(1000);
            rotateAnimation.setFillAfter(true);
            imageView.startAnimation(rotateAnimation);
            handler.postDelayed(this, 1000);
            seconds++;
        }
    }, 1000);
}

【问题讨论】:

    标签: android animation


    【解决方案1】:

    秒针的长度大约等于圆的半径或圆直径的一半。秒针的旋转原点必须是该圆的中心。将圆心置于零 x y 原点。将秒针的底座放在零原点上。旋转是相对于圆心的。

    【讨论】:

      【解决方案2】:

      当我使用 0.5f 和 1.0f 作为旋转轴时,它起作用了。所需的更改是 ImageView 的高度和宽度应该是固定的。早些时候,两者都设置为 WRAP_CONTENT。这导致图像在旋转之前发生偏移。

      RotateAnimation rotateAnimation = new RotateAnimation(
                      (seconds - 1) * 6, seconds * 6,
                      Animation.RELATIVE_TO_SELF, 0.5f,
                      Animation.RELATIVE_TO_SELF, 1f);
      

      【讨论】:

        猜你喜欢
        • 2011-11-23
        • 1970-01-01
        • 2016-11-21
        • 1970-01-01
        • 2013-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多