当前位置: 首页 > article >正文

Android水波纹搜索效果

Tips:Android,蓝牙搜索设备,水波纹效果

效果图:

在这里插入图片描述

源码:

public class RippleAnimationView extends RelativeLayout {

    private Paint mPaint;
    public static final int STROKE_WIDTH = 5;
    List<View> views = new ArrayList<>();
    private boolean animationRunning = false;

    public RippleAnimationView(Context context) {
        super(context);
    }

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

    private void init(Context context, AttributeSet attrs) {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);//抗锯齿
        int radius = (int) context.getResources().getDimension(com.kl.common.R.dimen.dp_150);//初始大小
        int rippleColor = ContextCompat.getColor(context, R.color.rippleColor);
        mPaint.setStrokeWidth(STROKE_WIDTH);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(rippleColor);

        //        延迟时间
        int rippleDuration = 3500;
        int singleDelay = rippleDuration / 4;//间隔时间 (上一个波纹  和下一个波纹的)
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(radius + STROKE_WIDTH, radius + STROKE_WIDTH);
        params.addRule(CENTER_IN_PARENT);
        for (int i = 0; i < 4; i++) {
            RippleCircleView rippleCircleView = new RippleCircleView(this);
            addView(rippleCircleView, params);
            views.add(rippleCircleView);

            PropertyValuesHolder aplhaHolder = PropertyValuesHolder.ofFloat("Alpha", 1, 0);
            PropertyValuesHolder scaleXHolder = PropertyValuesHolder.ofFloat("scaleX", 2);
            PropertyValuesHolder scaleYHolder = PropertyValuesHolder.ofFloat("scaleY", 2);
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(rippleCircleView, aplhaHolder, scaleXHolder, scaleYHolder);
            animator.setDuration(rippleDuration);
            animator.setStartDelay(i * singleDelay);
            animator.setRepeatMode(ObjectAnimator.RESTART);
            animator.setRepeatCount(ObjectAnimator.INFINITE);
            rippleCircleView.setTag(animator);
        }
    }

    /**
     * 启动动画
     */
    public void startRippleAnimation() {
        if (!animationRunning) {
            for (View rippleView : views) {
                rippleView.setVisibility(VISIBLE);
                ((ObjectAnimator) rippleView.getTag()).start();
            }
            animationRunning = true;
        }

    }

    /**
     * 停止动画
     */
    public void stopRippleAnimation() {
        if (animationRunning) {
            Collections.reverse(views);
            for (View rippleView : views) {
                rippleView.setVisibility(INVISIBLE);
                ((ObjectAnimator) rippleView.getTag()).end();
                ((ObjectAnimator) rippleView.getTag()).cancel();
            }
            animationRunning = false;
        }

    }


    public boolean isAnimationRunning() {
        return animationRunning;
    }

    public Paint getPaint() {
        return mPaint;
    }
}

public class RippleCircleView extends View {
    private RippleAnimationView mRippleAnimationView;

    public RippleCircleView(RippleAnimationView rippleAnimationView) {
        super(rippleAnimationView.getContext());
        mRippleAnimationView = rippleAnimationView;
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float radius = (float) Math.min(getWidth(), getHeight()) / 2;
        canvas.drawCircle(radius, radius, radius - RippleAnimationView.STROKE_WIDTH, mRippleAnimationView.getPaint());
    }
}

使用:

    <com.kl.analyze.view.RippleAnimationView
        android:id="@+id/ripple_view"
        android:layout_width="@dimen/dp_335"
        android:layout_height="@dimen/dp_335"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/dp_100">

        <ImageView
            android:id="@+id/iv_bluetooth"
            android:layout_width="@dimen/dp_150"
            android:layout_height="@dimen/dp_150"
            android:layout_centerInParent="true"
            android:src="@drawable/ic_search_bluetooth"/>
    </com.kl.analyze.view.RippleAnimationView>
    /**
     * 开始旋转动画
     */
    private fun startRotateAnim() {
        rippleView.startRippleAnimation()
    }

    /**
     * 停止动画
     */
    private fun stopRotateAnim() {
        rippleView.stopRippleAnimation()
    }

http://www.kler.cn/a/448434.html

相关文章:

  • 【085】基于51单片机PID直流电机控制系统【Proteus仿真+Keil程序+报告+原理图】
  • Ubuntu vi(vim)编辑器配置一键补全main函数
  • How to run Flutter on an Embedded Device
  • 基于AT89C52单片机的6位电子密码锁设计
  • 了解RPC
  • postman读取文件执行
  • Java并发编程框架之综合案例—— 分布式爬虫(四)
  • springboot基于Java的校园导航微信小程序的设计与实现
  • React+Vite项目框架
  • 如何构建一个简单的SpringBoot程序
  • 《软件工程文档攻略:解锁软件开发的“秘籍”》
  • 基于Spring Boot的营销项目系统
  • 题解:单调栈求解良好的感觉
  • leetcode 面试经典 150 题:无重复字符的最长子串
  • [react]searchParams转普通对象
  • 【CVE-2024-56145】PHP 漏洞导致 Craft CMS 出现 RCE
  • vue3 setup模式使用事件总线Event bus用mitt,app.config.globalProperties.$bus
  • MySQL 主从复制与高可用
  • MongoDB(下)
  • 深度学习之目标检测——RCNN
  • 《Java核心技术I》Swing的组合框
  • MongoDB 介绍及 Java 实现基本操作
  • kafka详解
  • Gin-vue-admin(1):环境配置和安装
  • 管理系统、微信小程序类源码文档-哔哩哔哩教程同步
  • CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究