Android,动画图像向上和向下

时间:2011-05-02 11:53:53

标签: android image animation

我正在尝试动画图像开始波纹管屏幕,向上,然后向下(为Galaxy选项卡做应用程序)。 这两个动画都是分开工作的,但是当我尝试创建AnimationSet时,我无法让它们工作。 我甚至尝试创建2个AsyncTasks,并在第一个AsyncTask的onPostExecute中调用第二个动画,但仍然无效。

这是我的XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/rel_layout"
   >
    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ad200"
        android:id="@+id/image"
    />
</RelativeLayout>

这是我试图用来动画图片的代码:

AnimationSet set = new AnimationSet(true);

Animation anim1 = new TranslateAnimation(0, 0, 1024, 824);
anim1.setDuration(3000);
anim1.setFillAfter(true);
set.addAnimation(anim1);

Animation anim2 = new TranslateAnimation(0, 0, 824, 1024);
anim2.setDuration(3000);
anim2.setFillAfter(true);
set.addAnimation(anim2);

imageView.clearAnimation();
set.setFillAfter(true);
imageView.startAnimation(set);

3 个答案:

答案 0 :(得分:1)

WendiKidd,

您非常接近您的解决方案。您刚刚错过了代码中的Animation.AnimationListener()。

Animation anim1 = new TranslateAnimation(0, 0, 1024, 824);
anim1.setDuration(3000);
anim1.setFillAfter(true);

anim1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Animation anim2 = new TranslateAnimation(0, 0, 824, 1024);
                anim2.setDuration(3000);
                anim2.setFillAfter(true);
                imageView.clearAnimation();
                imageView.startAnimation(anim2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


imageView.startAnimation(anim1);

上面的示例将在anim1结束后启动anim2。

答案 1 :(得分:0)

看起来您正在同时启动这两个动画。将anim2.setStartOffset(3000)添加到第二个动画中。这将导致第二个动画在第一个动画之后3000毫秒开始。

BR, 克里斯托弗

答案 2 :(得分:0)

这是一个解决方案...尝试下面的代码..我使用这段代码很多次..它工作正常.. ======&gt;

    Animation zoomin =new TranslateAnimation(1, 1, 0, -50);
    zoomin.setDuration(1000);
    zoomin.setFillEnabled(true);
    zoomin.setFillAfter(true);

    Animation zoomout =  new TranslateAnimation(1, 1, -50, 0);
    zoomout.setDuration(1000);
    zoomout.setFillEnabled(true);
    zoomout.setFillAfter(true);

    imageView.startAnimation(zoomin);

    zoomin.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation arg0) {
            imageView.startAnimation(zoomout);
          }
    });

    zoomout.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation arg0) {

            imageView.startAnimation(zoomin);


        }
    });
相关问题