2014-11-14 54 views
1

如何使主轴位于图像的底部中心(下面给出的图像链接)来模拟模拟时钟的秒针。第二只手的动画制作

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

image

代码下面

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); 
} 

回答

0

当我使用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); 
0

秒针的长度给定为近似等于半径的圆的或圆的直径的一半。你的第二只手的旋转起源需要成为该圈子的中心。将圆心置于零xy原点。将第二只手的底部放在零原点上。旋转是相对于圆心的。