Android幻灯片动画闪烁

时间:2011-10-12 10:33:14

标签: android animation reset translate-animation

我正在尝试为ImageButton设置动画。单击按钮时,我希望它向右滑动。当它再次被点击时,它应该滑回原来的位置。

当动画结束时,它会返回到原始位置,因此我重新定位了ImageButton。我有一个小的“闪光灯”,其中ImageButton和动画在ImageButton重新定位之前都处于原始位置。谁能告诉我如何摆脱这种“闪光”?

我编写了以下代码来启动动画,当动画结束时,移动ImageButton本身。 它是什么,是一个onClickListener用于滑动ImageButton和一个AnimationListener。还有一个onClickListener和一个用于滑动ImageButton的AnimationListener。

private class SceneIndicatorListenerIn implements ImageButton.OnClickListener {
    ImageButton imageButton;
    public SceneIndicatorListenerIn (ImageButton imageButton) {
        this.imageButton = imageButton;
    }

    @Override
    public void onClick(View view) {
        // Create animation
        Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_in);
        anim.setAnimationListener(new SceneIndicatorListenerInDidEnd(imageButton));
        view.startAnimation(anim);
    }
}

private class SceneIndicatorListenerInDidEnd implements AnimationListener {
    ImageButton imageButton;
    public SceneIndicatorListenerInDidEnd (ImageButton imageButton) {
        this.imageButton = imageButton;
    }

    @Override
    public void onAnimationEnd(Animation anim) {
        Log.d(LOG_TAG, "In animation did end");

        // This is for density pixels
        float dp = context.getResources().getDisplayMetrics().density;

        // Keep position after animation
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight());
        params.setMargins((int) (0 * dp), imageButton.getTop(), 0, 0);
        imageButton.setLayoutParams(params);

        // Change on click listener
        imageButton.setOnClickListener(new SceneIndicatorListenerOut(imageButton));
    }

    @Override
    public void onAnimationRepeat(Animation arg0) {}

    @Override
    public void onAnimationStart(Animation arg0) {}
}

private class SceneIndicatorListenerOut implements ImageButton.OnClickListener {
    ImageButton imageButton;
    public SceneIndicatorListenerOut (ImageButton imageButton) {
        Log.d(LOG_TAG, "My imageButton was set");
        this.imageButton = imageButton;
    }

    @Override
    public void onClick(View view) {
        Log.d(LOG_TAG, "You clicked me");

        // Create animation
        Animation anim = AnimationUtils.loadAnimation(context, R.anim.sceneindicator_out);
        anim.setAnimationListener(new SceneIndicatorListenerOutDidEnd(imageButton));
        view.startAnimation(anim);
    }
}

private class SceneIndicatorListenerOutDidEnd implements AnimationListener {
    ImageButton imageButton;
    public SceneIndicatorListenerOutDidEnd (ImageButton imageButton) {
        this.imageButton = imageButton;
    }

    @Override
    public void onAnimationEnd(Animation anim) {
        Log.d(LOG_TAG, "Out animation did end");

        // This is for density pixels
        float dp = context.getResources().getDisplayMetrics().density;

        // Keep position after animation
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageButton.getWidth(), imageButton.getHeight());
        params.setMargins((int) (-199 * dp), imageButton.getTop(), 0, 0);
        imageButton.setLayoutParams(params);

        // Change on click listener
        imageButton.setOnClickListener(new SceneIndicatorListenerIn(imageButton));
    }

    @Override
    public void onAnimationRepeat(Animation arg0) {}

    @Override
    public void onAnimationStart(Animation arg0) {}
}

这是我滑动的动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0%"
               android:toXDelta="83%"
               android:duration="750" />
</set>

这是我的滑动动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0%"
               android:toXDelta="-80%"
               android:duration="750" />
</set>

1 个答案:

答案 0 :(得分:0)

我有同样的问题,想要为遇到此问题的人发布答案。无论你在xml中设置什么,都有一个导致这种情况的错误。

我解决它的方法是在动画之前手动设置屏幕外的X位置,以便闪光灯在屏幕外发生,然后它将被设置回原来的位置,因为动画正在处理。

image.setX(-5000);//Really any number off of the screen will do

Animation animation0 = AnimationUtils.loadAnimation(this,
            R.anim.slide_across_right);
animation0.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            animationHack();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }
});



private void animationHack() {
    (new Handler()).postDelayed(new Runnable() {
        public void run() {
            image.setX(0);
        }
    }, 20);

}