Andriod 通用按钮文本点击效果

2024-02-29  本文已影响0人  风吹过山

按钮点击效果,背景透明度变化。

继承Button,Button有点击效果。

继承LinearLayout,LinearLayout有点击效果。

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

@SuppressLint("AppCompatCustomView")
public class AnimButton extends Button {
    private static final float FROM_VALUE = 1.0f;
    private static final float TO_VALUE = 0.94f;
    private static final float ALPHA_VALUE = 0.8f;
    private AnimatorSet animatorSet = new AnimatorSet();

    public AnimButton(Context context) {
        super(context);
        init();
    }

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

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

    public AnimButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setClickable(true);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if (visibility == 0) {
            setAlpha(FROM_VALUE);
            setScaleX(FROM_VALUE);
            setScaleY(FROM_VALUE);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isClickable() && isEnabled()) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                downAnim(this);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                animNormal(this);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                animNormal(this);
            }
        }
        return super.onTouchEvent(event);
    }

    private void animNormal(View view) {
        animatorSet.setDuration(250);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", TO_VALUE, FROM_VALUE);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", TO_VALUE, FROM_VALUE);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", ALPHA_VALUE, FROM_VALUE);
        animatorSet.play(scaleX).with(scaleY).with(alpha);
        animatorSet.start();
    }

    private void downAnim(View view) {
        animatorSet.setDuration(200);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", FROM_VALUE, TO_VALUE);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", FROM_VALUE, TO_VALUE);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", FROM_VALUE, ALPHA_VALUE);
        animatorSet.play(scaleX).with(scaleY).with(alpha);
        animatorSet.start();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读