美文网首页
Andriod 通用按钮文本点击效果

Andriod 通用按钮文本点击效果

作者: 风吹过山 | 来源:发表于2024-02-29 14:24 被阅读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();
        }
    }
    

    相关文章

      网友评论

          本文标题:Andriod 通用按钮文本点击效果

          本文链接:https://www.haomeiwen.com/subject/virqzdtx.html