【问题标题】:ObjectAnimator reset to initial statusObjectAnimator 重置为初始状态
【发布时间】:2018-03-11 15:25:07
【问题描述】:

我想要达到的目标

  1. 自定义视图最初是绿色的。
  2. 单击按钮时,视图开始在绿色和白色之间无限地设置动画。
  3. 再次单击按钮时,视图应将其颜色重置为其初始状态(绿色)并停止动画。

第 2 步和第 3 步可以无限应用。

问题

我的问题是,当在动画进行时单击按钮时,视图不会将其颜色重置为其初始状态(绿色),相反,无论动画在那一刻到达哪一点,视图变成最终状态(白色)。

我尝试使用来自另一个类似问题的solution SO,但没有运气。

代码

我如何初始化我的ObjectAnimator

animator = ObjectAnimator.ofArgb(this, "color", WHITE);
animator.setDuration(ANIMATION_DURATION);
animator.setRepeatCount(Animation.INFINITE);
animator.setRepeatMode(ValueAnimator.REVERSE);

自定义视图类有一个私有成员Animator.AnimatorListener,它实现了onAnimationEnd(),我将它附加到startAnimation()中的动画师,将它与onAnimationEnd()中的动画师分离,因为我上面引用的解决方案说所以。

private Animator.AnimatorListener animatorListener = new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        animation.removeListener(this);
        animation.setDuration(0);
        ((ObjectAnimator) animation).reverse();
    }
};

public void startAnimation(){
    animator.addListener(animatorListener);
    animator.setDuration(ANIMATION_DURATION);
    animator.start();
}

public void stopAnimation(){
    animator.end();
    // animatorListener.onAnimationEnd() will be called here
}

我不确定这是否是实现我目标的最佳方式,任何建议都将不胜感激。

【问题讨论】:

    标签: android android-animation objectanimator


    【解决方案1】:

    尝试以下方法:

    public class Demo8 extends AppCompatActivity {
    
    private Button b;
    private ViewC v;
    private ObjectAnimator animator;
    
    private enum animation_state {START, CLEAR}
    
    private animation_state animation_current_state = animation_state.START;
    
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo8);
    
        b = (Button) findViewById(R.id.b);
        v = (ViewC) findViewById(R.id.v);
    
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!animator.isRunning()) {
                    CAnimator(animation_state.START);
                } else {
                    CAnimator(animation_state.CLEAR);
                }
            }
        });
    
        animator = ObjectAnimator.ofArgb(v, "background", Color.WHITE);
        animator.setDuration(1000);
        animator.setRepeatCount(Animation.INFINITE);
        animator.setRepeatMode(ObjectAnimator.REVERSE);
        animator.addListener(animatorListener);
    }
    
    private Animator.AnimatorListener animatorListener = new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation, boolean isReverse) {
    
        }
    
        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
            Log.e("End" , "***********True");
            animation.removeListener(this);
    
        }
    
        @Override
        public void onAnimationRepeat(Animator animation) {
            super.onAnimationRepeat(animation);
            Log.e("Repeat" , "***********True");
          }
    
    };
    
    
    private void CAnimator(animation_state s) {
        animation_current_state = s;
        if (s == animation_state.START) {
            animator.start();
        } else if (s == animation_state.CLEAR) {
            animator.cancel();
            v.setBackground(Color.GREEN);
        }
    }
    
    }
    

    ViewC(自定义视图):

    public class ViewC extends View {
    
    
    private static final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int color;
    
    public ViewC(Context context) {
        super(context);
    }
    
    public ViewC(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        color = Color.GREEN;
    }
    
    public ViewC(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    public ViewC(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
    
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
    final RectF rect = new RectF(2.5f * 20,
                0.5f * (getHeight() - 5), getWidth() - 2.5f * 20,
                0.5f * (getHeight() + 5));
        paint.setColor(color);
        canvas.drawRect(rect, paint);
    }
    
    public void setBackground(int color){
        this.color = color;
        invalidate();
    }
    
    
        }
    

    布局 demo8.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    
    <com.example.admin.machinelearning.ViewC // Change this to your package
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/v">
    </com.example.admin.machinelearning.ViewC>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/b"
        android:text="Button"/>
    
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 2015-02-21
      • 2023-01-27
      • 2021-01-15
      • 1970-01-01
      • 2020-04-12
      • 2019-08-18
      • 2020-08-31
      • 2013-03-27
      • 2017-12-08
      相关资源
      最近更新 更多