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

Andriod 通用按钮文本点击效果

作者: 风吹过山 | 来源:发表于2023-05-04 20:09 被阅读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 AnimatorSetanimatorSet =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) {

animOne(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 animOne(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();

    }

}

```

相关文章

  • UIActionSheet和UIAlertView的使用

    先来看看效果 1.点击登录按钮 弹出UIActionSheet 2.点击确定 弹出UIAlertView带文本框 ...

  • 12-撰写按钮点击动画

    撰写按钮点击动画 效果 点击加号背景磨砂效果 各个按钮依次弹出 点击空白处按钮依次消失,恢复弹出前的效果 某个按钮...

  • 我与一位叫交互的朋友聊天

    随着material design的发布,谷歌为andriod中注入了很多的动态效果。比如FBA按钮的动态效果,水...

  • 点击按钮复制文本

  • 点击事件穿透效果

    具体效果图: 未点击时的效果图:未点击.png 点击橘黄色view上非按钮点击部分的效果图:点击非按钮部分.png...

  • UIPickView的使用

    一 :预期效果 :点击按钮出现 UIPickerView 背景变为半透明效果 .1 在按钮的点击事件中 //创建全...

  • 小程序  点击按钮后修改颜色(样式)

    需实现的功能:点击按钮后,按钮的颜色会加深 效果展示:原按钮 点击后的按钮 思路:给按钮设置一个点击事件,点击了按...

  • FCC-JS-JQ-Change Text with Click

    通过点击事件来更改文本。当我们点击按钮时,我们可以更新HTML页面任务:点击"Get Message"按钮,将cl...

  • 第一次作业第二题

    点击求积按钮文本3显示乘积,点击清除按钮清除所有文本内容。 代码 ...namespace 第一次第二个应用程序{...

  • 按钮点击效果总结

    一、在Android 开发中控制按钮或者listview 点击效果1:主要元素 定义要在某些状态期间使用的可绘制对...

网友评论

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

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