2017-09-03 52 views
0

我有一個圖像。我需要它在左下角旋轉一個小角度。當我點擊屏幕時,此動畫可以工作。圖像出現在那裏並旋轉。但對於不同的職位,這個關鍵是不變的。我怎樣才能以編程方式動態設置每個案例的樞紐?如何爲旋轉動畫設置動態軸

?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <rotate 
     android:duration="500" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotY="100%" 
     android:pivotX="0%" 
     android:repeatCount="0" 
     android:toDegrees="10"/> 
</set> 

有我們給與價值,我們getX()getY()樞軸值什麼辦法?

+0

你是如何做旋轉動畫?根據你用來做什麼,你會得到不同的答案。如果你是通過矩陣來做的話,你可以在應用旋轉矩陣之前將原點翻譯爲你想要的點作爲關鍵點。 –

+0

我沒有嘗試過使用矩陣。現在我會這樣做。我用xml來做旋轉 –

回答

0

得到了答案。通過這樣做

var screenH=Resources.getSystem().getDisplayMetrics().heightPixels 
var screenH=Resources.getSystem().getDisplayMetrics().widthPixels 

var x= ("x value of your pivot)/screenW 

var y= ("y value of your pivot)/screenH 

var rotateAnim=RotateAnimation(0.0f , 20.0f , Animation.RELATIVE_TO_PARENT , x , 
Animation.RELATIVE_TO_PARENT , y) 

rotateAnim.setDuration(1000) 
rotateAnim.setFillAfter(true) 

view.startAnimation(rotateAnim)