一个关于安卓点击水波纹效果的工具类

2023-10-23  本文已影响0人  Tyson_Wu

public class AppAnimateUtil {


    public static void handleRippleBg(View rippleView, ColorStateList colorStateList, Context context, Drawable originalDrawable) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            RippleDrawable rippleDrawable = new RippleDrawable(colorStateList
                    , originalDrawable, new ColorDrawable(Color.parseColor("#DBE3EB")));
            rippleView.setBackground(rippleDrawable);
        } else {
            TypedValue typedValue = new TypedValue();
            context.getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
            int[] attribute = new int[]{android.R.attr.selectableItemBackground};
            TypedArray typedArray = context.getTheme().obtainStyledAttributes(typedValue.resourceId, attribute);
            rippleView.setBackground(typedArray.getDrawable(0));
        }
    }

}

使用

    private void initRipple() {
        int color = Color.parseColor("#DBE3EB");
        //设置ripple,5.0波纹效果
        AppAnimateUtil.handleRippleBg(ivBack, ColorStateList.valueOf(color),
                this, ivBack.getBackground());
        AppAnimateUtil.handleRippleBg(mIvSave, ColorStateList.valueOf(color),
                this, mIvSave.getBackground());
        AppAnimateUtil.handleRippleBg(mIvCopy, ColorStateList.valueOf(color),
                this, mIvCopy.getBackground());
    }
上一篇 下一篇

猜你喜欢

热点阅读