2014-02-28 48 views
0

我已经应用旋转动画到使用ObjectAnimator的按钮,而旋转似乎工作;枢轴点仍然是按钮的左上角。设置枢轴旋转动画(ObjectAnimator),不工作?

MainActivity

Button bt1; 

float pivotX=0f; 
float pivotY=0f; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    bt1 = (Button) findViewById(R.id.button1); 

    pivotX = bt1.getX() + (float)bt1.getWidth();// 500;// bt1.getHeight() ; 
    pivotY = bt1.getY() + (float)bt1.getHeight();// 500;// bt1.getWidth(); 

    bt1.setPivotX(pivotX); 
    bt1.setPivotY(pivotY); 

    bt1.setOnClickListener(this); 
} 


private void rotate() 
{ 
    ObjectAnimator rotate = ObjectAnimator.ofFloat(bt1, View.ROTATION_X,360); 
    rotate.setDuration(2000); 

    AnimatorSet aSet = new AnimatorSet(); 
    aSet.play(rotate); 
    aSet.start(); 
} 

@Override 
public void onClick(View v) 
{ 
    switch(v.getId()) 
    { 
    case R.id.button1: 
     rotate(); 
     break; 

    } 
} 

编辑: bt1.getX和bt1.getY返回0会更新一次,我知道为什么了。

编辑:这似乎也没有工作。

@Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
    //Here you can get the size! 

    width = bt1.getWidth(); 
    height = bt1.getHeight(); 

} 
+0

你知道了吗?和,你想在哪里枢轴点?如果它是中心的,我认为你需要做的就是将现有的枢纽值除以2,并且应该居中 – wkhatch

回答

-2

的getX()中OnCreate中总是给你一个零 - 在视图

,你必须等待它不是“落实”。我用这个循环等待绝对非零值:

new Thread(new Runnable() { 
     public void run() { 
      int count = 0; 
      do { 
       try { 
        Thread.sleep(50); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       Log.d(TAG, "checkin... count="+count); 
       count++; 
      } while (!(playStopButton.getWidth() > 0) && count < 100); 
      runOnUiThread(new Runnable() { 
       public void run() { 
        //here get X, get width etc 
       } 
      }); 
     } 
    }).start(); 
0

bt1.getWidth()不能得到结果,但在onCreate 0,因为bt1不测量yet.you可以试试下面如下:

button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout(){ 
    //here you can get size 
    width = button.getWidth(); 
    height = button.getHeight(); 
    } 
});