闪烁Alpha动画没有淡入淡出效果

时间:2014-04-25 12:21:56

标签: android

我正在尝试实施Blink动画。

此代码显示淡入淡出和淡出的视图:

AlphaAnimation blink = new AlphaAnimation(0.0f, 1.0f);

blink.setDuration(500); 
blink.setStartOffset(0); 
//changing it makes more than one animation appear in different time stamps.
blink.setRepeatMode(Animation.REVERSE);
blink.setRepeatCount(Animation.INFINITE);

我有两个问题:

  1. setStartOffset(N); - >更改n会使不同的时间戳中出现多个动画。没有同步。我希望它能够同步,所有动画都应该同时出现并消失。
  2. 我不想淡入或淡出,只是可见&延迟几毫秒。 有没有我必须使用的其他动画类,或者我必须制作自定义动画。 PLS。帮助。

2 个答案:

答案 0 :(得分:4)

对于没有褪色的动画,您可以创建自己的自定义插值器:

import android.view.animation.Interpolator;

public class StepInterpolator implements Interpolator {

    float mDutyCycle;
    public StepInterpolator(float dutyCycle) {
        mDutyCycle = dutyCycle;
    }

    @Override
    public float getInterpolation(float input) {
        return input < mDutyCycle ? 0 : 1;
    }

}

然后在Animation对象中设置插值器:

blink.setInterpolator(new StepInterpolator(0.5f));

答案 1 :(得分:1)

所以,我的答案......这是一个在一定时间间隔内切换视图可见性的类。当然它可以用不同的方式解决,也许你会得到一些灵感......

public static class ViewBlinker {

    private Handler handler = new Handler();
    private Runnable blinker;

    public ViewBlinker(final View v, final int interval) {
        this.blinker = new Runnable() {
            @Override
            public void run() {
                v.setVisibility(v.getVisibility()==View.VISIBLE?View.INVISIBLE:View.VISIBLE);
                handler.postDelayed(blinker, interval);
            }
        };
    }

    public void startBlinking() {
        handler.post(blinker);
    }

    public void stopBlinking() {
        handler.removeCallbacks(blinker);
    }

}

你可以这样使用它:

    ViewBlinker blinker = new ViewBlinker(YOUR_BLINK_VIEW, YOUR_BLINK_INTERVAL);

    blinker.startBlinking();

当视图完成闪烁时,请致电

    blinker.stopBlinking();
相关问题