2012-02-22 68 views
3

我想以編程方式爲自定義視圖設置動畫效果。我希望它只在xml文件中聲明的位置有動畫效果,並且我不希望它弄亂佈局。另外這個動畫受其他因素影響,所以android動畫類是不可能的。如何以編程方式爲自定義視圖(ImageView)設置動畫效果?

我測試了這個樣本tutorial並觀看這個videos。我的問題是,大多數教程是爲了將一個精靈動畫到一個畫布中,在這個畫布中你將跟蹤精靈的位置。

如何在不影響佈局且不使用Android動畫類的情況下爲自定義視圖設置動畫效果?

編輯: 自定義視圖將作爲動畫gif,並在事件激活時觸發另一組幀。

+0

你想做什麼?你想如何動畫? – Blundell 2012-02-22 08:48:14

+0

動畫就像一個加載欄,它運行時間,但是當事件發生時,另一組動畫將在該視圖上播放。動畫只能影響自定義視圖。 – Marl 2012-02-22 08:58:30

+1

使用AnimationDrawable,您可以輕鬆地切換到另一組框架,如果您調用:rocketImage.setBackgroundResource(R.drawable。<< Some other animation-list >>);當你需要的時候。 – auval 2012-02-22 10:04:04

回答

3

如果您想要像動畫GIF一樣「運行」的「逐幀」動畫,您可以按照以下步驟操作: drawable-animation official tutorial

從教程引用:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="true"> 
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 
</animation-list> 

,並在你的代碼

AnimationDrawable rocketAnimation; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); 
    rocketImage.setBackgroundResource(R.drawable.rocket_thrust); 
    rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 
} 

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    rocketAnimation.start(); 
    return true; 
    } 
    return super.onTouchEvent(event); 
} 
+0

AnimationDrawable類是我需要的。謝謝。 – Marl 2012-02-22 09:31:19

5

您可以創建動畫的XML和程序,動畫應用到您的ImageView支票下面的例子

可以爲您的自定義視圖啓動動畫只需致電viewobject.startAnimation(animationobject);

animation.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false"> 
<scale 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fromXScale="1.0" 
    android:toXScale="1.4" 
    android:fromYScale="1.0" 
    android:toYScale="0.6" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:fillAfter="false" 
    android:duration="700" /> 
<set 
    android:interpolator="@android:anim/accelerate_interpolator" 
    android:startOffset="700"> 
    <scale 
     android:fromXScale="1.4" 
     android:toXScale="0.0" 
     android:fromYScale="0.6" 
     android:toYScale="0.0" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:duration="400" /> 
    <rotate 
     android:fromDegrees="0" 
     android:toDegrees="-45" 
     android:toYScale="0.0" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:duration="400" /> 
</set> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> 

</LinearLayout> 

那麼你的活動類myActivity.java

public class myActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    ImageView image = (ImageView) findViewById(R.id.ImageView01); 

    //create animation class object 
    Animation hyperspaceJump = 
     AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump); 
    //start animation for image 
    image.startAnimation(hyperspaceJump); 
} 
} 
相關問題