2012-12-26 30 views
15

Alphaing可繪製工作做好這樣的:如何通過ObjectAnimator旋轉drawable?

if(mAlphaAnimation == null){ 
     mAlphaAnimation = ObjectAnimator.ofFloat(this, "alpha", 0.0f,1.0f).setDuration(TARGET_ANIM_ALPHA_DURATION); 
     mAlphaAnimation.setInterpolator(new AccelerateDecelerateInterpolator()); 
     mAlphaAnimation.setStartDelay(TARGET_ANIM_ALPHA_DELAY_BASE*power); 
     mAlphaAnimation.setRepeatCount(ValueAnimator.INFINITE); 
     mAlphaAnimation.setRepeatMode(ValueAnimator.REVERSE); 
     mAlphaAnimation.addUpdateListener(this); 
} 

但是,如果我要旋轉繪製如下圖所示,它唐的工作。

private void createRotateAnim(float fromDegress,float toDegress,int duration){ 
    if(mRotateAnimation == null){ 
     mRotateAnimation = ObjectAnimator.ofFloat(this, "rotation",fromDegress,toDegress).setDuration(duration); 
     mRotateAnimation.setStartDelay(100); 
     mRotateAnimation.setInterpolator(new AccelerateInterpolator()); 
     mRotateAnimation.addUpdateListener(this); 
    } 
} 

任何人都可以幫助我解決這個問題,或者這些是任何其他方式來創建一個旋轉可繪製動畫。

對不起,我可憐的英語。

+0

我想通過觸摸事件來改變drawable狀態,drawable是一個drawable,而不是一個drawable list。例如,根據屏幕上的移動距離更改一個可繪製的alpha值。此功能已完成。然後,我想根據移動角度改變可繪製的方向。換句話說,根據角度旋轉drawable。這些可繪製的東西將以相同的觀點來繪製。 我想知道ObjectAnimator對象的旋轉屬性可以旋轉drawable或不旋轉。也許,它可以旋轉,因爲imageview可以旋轉一個drawable用於繪製背景。 – marine8888

+0

[Android:使用ObjectAnimator將視圖的視圖維度的小數值轉換爲視圖的可能重複](http://stackoverflow.com/questions/10854940/android-using-objectanimator-to-translate-a-view-with- )的分數值) –

回答

13

試試這個簡單的旋轉動畫應用於圖像。

ImageView imageview = (ImageView)findViewById(R.id.myimage); 
RotateAnimation rotate = new RotateAnimation(180, 360, Animation.RELATIVE_TO_SELF,  
0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    rotate.setDuration(500); 
imageview.startAnimation(rotate); 

這個答案僅僅是問題的緣故,這是正確的,可點擊區域會比View的當前位置不同。請檢查此問題是否使可點擊區域正確。 Button is not clickable after TranslateAnimation

+0

謝謝。但我不想把drawable放到其他視圖中,比如ImageView等等,但是想把它繪製在視圖的子類上(而不是ViewGroup)。 – marine8888

+1

@ marine8888我認爲你錯了,'ImageView'只是'View'的子類,而不是'ViewGroup'(它本身就是'View'的子類)。你可以創建你自己的View類的子類,並將drawable渲染到一個畫布上,但是這可以和ImageView做同樣的工作。 –

47

ObjectAnimator來代替。

ImageView imageview = (ImageView)findViewById(R.id.image); 

ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview , 
        "rotation", 0f, 360f); 
      imageViewObjectAnimator.setDuration(1000); // miliseconds 
      imageViewObjectAnimator.start(); 

編輯 由於這個問題,得出了一些關注,讓我來解釋一下爲什麼要使用ObjectAnimator,而不是其他的過渡動畫

事情有關使用ObjectAnimator的是,它的移動既可見,如果您使用其他動畫方法(例如Transition Animation或其他動畫設備),並假設您想將屏幕左下方的Button移動到左上方,則它只會移動可見區域但是而不是Button本身,可點擊區域仍然位於之前的位置,在這種情況下,可點擊區域仍然位於左下角,而不是您移動按鈕的左上角。

如果您對ObjectAnimator的操作相同,則可見區域和可點擊區域都會移動所需的位置。