【问题标题】:Animation of different images in list列表中不同图像的动画
【发布时间】:2011-03-09 06:36:43
【问题描述】:

我有不同的图像,比如 100 张左右。现在,我想对它们应用动画。我希望我的 ImageView 在指定的时间间隔后获取每个图像,但是当图像发生变化时,每个图像都应该 FadeIn 或 FadeOut。我将我的图像放在@drawable/[list_of_images].xml 文件中:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a1" android:duration="1000"/>
<item android:drawable="@drawable/a2" android:duration="2000"/>
<item android:drawable="@drawable/a3" android:duration="3000"/>
<item android:drawable="@drawable/a4" android:duration="500"/>

然后我可以使用以下方法根据它们在 ImageView 中的时间间隔成功更改这些图像:

public class AnimTest extends Activity
{
   AnimationDrawable myAnim;

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

       ImageView myIV = (ImageView) findViewById(R.id.image_view);
       myIV.setBackgroundResource(R.drawable.list_of_images.xml);

       myAnim = (AnimationDrawable) myIV.getBackground();

   }

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

}

问题是我无法弄清楚如何在每张图像上应用淡入淡出效果,而它们正在被上述动画改变。我可以在整个图像列表上应用淡入淡出动画,但不能在每张图像上都这样做。我是否朝着正确的方向实现此功能?如果没有,请引导我走向正确的道路。

问候, 哈瓦尔

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以尝试将动画的重复计数设置为 imagecount-1,然后将 AnimationListener 添加到动画中,以在每次重复和开始时更改 ImageView 的背景图像。

    这是一个使用 RoboGuice 的简单示例(它使代码更清晰,但对您的问题没有任何影响):https://github.com/bostonandroid/batgirl/blob/master/src/org/roboguice/batgirl/Batgirl.java

    public class Batgirl extends RoboActivity {
        // Views
        @InjectView(R.id.content) LinearLayout linearLayout;
    
        // Resources
        @InjectResource(R.anim.spin) Animation spin;
        @InjectResource(R.integer.max_punches) int MAX_PUNCHES;
    
        // Other Injections
        @Inject ChangeTextAnimationListener changeTextAnimationListener;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // Set up the animation
            linearLayout.setAnimation(spin);
            spin.setAnimationListener(changeTextAnimationListener);
            spin.setRepeatCount(MAX_PUNCHES - 1);
    
            spin.start();
        }
    }
    
    /**
     * A simple animation listener that swaps out the text between repeats.
     */
    class ChangeTextAnimationListener implements AnimationListener {
        @InjectView(R.id.hello) TextView helloView;
        @Inject Fist fist;
        @Inject PackageInfo info;
    
        public void onAnimationRepeat(Animation animation) {
            onAnimationStart(animation);
        }
    
        public void onAnimationStart(Animation animation) {
            helloView.setText( getNextTextString() );  // getNextTextString() not shown in this example
        }
    
        public void onAnimationEnd(Animation animation) {            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-19
      • 1970-01-01
      • 2018-03-12
      • 2016-04-14
      • 1970-01-01
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多