android动画第一次不起作用

时间:2015-06-18 14:23:21

标签: android animation

我是初学Android动画,我有这个动画代码,当我第一次点击按钮时,我启动应用程序,动画不起作用,但我第二次点击它,动画工作。有人能告诉我我做错了吗?

button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
                ObjectAnimator anim1 = ObjectAnimator.ofFloat(button, "scaleX", 1f, 0.7f);
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(button, "scaleY", 1f, 0.7f);
                anim1.setDuration(100);
                anim2.setDuration(100);
                anim1.start();
                anim2.start();
                return false;
            }
            if (event.getActionMasked() == MotionEvent.ACTION_UP) {
                ObjectAnimator anim1 = ObjectAnimator.ofFloat(button, "scaleX", 0.7f, 1f);
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(button, "scaleY", 0.7f, 1f);
                anim1.setDuration(300);
                anim2.setDuration(300);
                anim1.start();
                anim2.start();
                return false;
            }
            return false;
        }
    });

1 个答案:

答案 0 :(得分:0)

使用此视图:

public class TouchAnimateView extends FrameLayout {

private Animator downAnimator, upAnimator;

private GestureDetector gestureDetector =
        new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
            @Override
            public void onShowPress(MotionEvent e) {
                downAnimator.start();
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                performClick();
                return true;
            }
        });

public TouchAnimateView(@NonNull Context context) {
    super(context);
    init();
}

public TouchAnimateView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    init();
}

public TouchAnimateView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    downAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.view_click_down);
    downAnimator.setTarget(this);
    upAnimator = AnimatorInflater.loadAnimator(getContext(), R.animator.view_click_up);
    upAnimator.setTarget(this);
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    switch (event.getAction()) {
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            upAnimator.start();
            break;
    }
    return true;
}}

view_click_down:

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator
    android:duration="100"
    android:propertyName="scaleX"
    android:valueFrom="1"
    android:valueTo="0.95" />

<objectAnimator
    android:duration="100"
    android:propertyName="scaleY"
    android:valueFrom="1"
    android:valueTo="0.95" />

<objectAnimator
    android:duration="100"
    android:propertyName="alpha"
    android:valueFrom="1"
    android:valueTo="0.5" />

view_click_up:

<?xml version="1.0" encoding="utf-8"?>

<objectAnimator
    android:propertyName="scaleX"
    android:duration="100"
    android:valueFrom="0.95"
    android:valueTo="1" />

<objectAnimator
    android:propertyName="scaleY"
    android:duration="100"
    android:valueFrom="0.95"
    android:valueTo="1" />

<objectAnimator
    android:propertyName="alpha"
    android:duration="100"
    android:valueFrom="0.5"
    android:valueTo="1" />