代码片|水波纹

Posted aikongmeng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了代码片|水波纹相关的知识,希望对你有一定的参考价值。

N年前的代码片分享

package com.example.myapplication


import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.Gravity
import android.view.animation.OvershootInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import kotlin.math.min

class WaterView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), Runnable 

    /**
     * 圆中心点的x和y
     */
    var centerXPoint: Float = 0f;
    var centerYPoint: Float = 0f;

    /**
     * 圆圈默认颜色
     */
    val CIRCLE_DEFAULT_COLOR: Int = Color.parseColor("#FFB6C1")


    /**
     * 等圆圈扩散到这个大小后,增加下一个圆圈
     */
    val CENTER_CIRCLE_SIZE = 75;

    /**
     * 圆圈集合和透明度集合  里面存放的是圆圈宽度和透明度
     */
    var circleList = mutableListOf<Int>()
    var circleAlphaList = mutableListOf<Int>()

    /**
     * 圆圈透明度递减步数
     */
    var circleAlphaReduceStep: Int = 0;

    /**
     * 圆圈扩散步数
     */
    val DIFFUSE_DISTANCE: Int = 10;

    /**
     * 圆圈画笔
     */
    lateinit var circlePaint: Paint

    /**
     * 中心圆ImageView
     */
    var circleImageView: ImageView = ImageView(context);

    var mHandler = Handler(Looper.myLooper()!!)

    /**
     * 中心圆ImageView动画
     */
    override fun run() 
        var x = ObjectAnimator.ofFloat(circleImageView, "scaleX", 1f, 1.3f, 1f)
        x.interpolator = OvershootInterpolator();

        var y = ObjectAnimator.ofFloat(circleImageView, "scaleY", 1f, 1.3f, 1f)
        y.interpolator = OvershootInterpolator();
        var animatorSet = AnimatorSet()

        animatorSet.setDuration(700)
        animatorSet.playTogether(x, y);
        animatorSet.start()

        mHandler.postDelayed(this, 800)
    

    init 
        circleImageView.setImageResource(R.mipmap.ic_launcher)
        var layoutParams =
            FrameLayout.LayoutParams(
                CENTER_CIRCLE_SIZE, CENTER_CIRCLE_SIZE
            )
        layoutParams.gravity = Gravity.CENTER
        addView(circleImageView, layoutParams)
        addNewCircle();
        setWillNotDraw(false)
        mHandler.postDelayed(this, 0)

        circlePaint = Paint().apply 
            color = CIRCLE_DEFAULT_COLOR
        
    

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) 
        super.onSizeChanged(w, h, oldw, oldh)
        centerXPoint = (w / 2).toFloat();
        centerYPoint = (h / 2).toFloat();

        /**
         * 计算出从中心点开始递减透明度的步数
         */
        var min = min(w, h)
        var temp = min / DIFFUSE_DISTANCE;
        circleAlphaReduceStep = ((255 / temp) * 2);
    

    fun addNewCircle() 
        circleList.add(0)
        circleAlphaList.add(255)
    

    override fun onDraw(canvas: Canvas) 
        super.onDraw(canvas)

        /**
         * 遍历所有圆圈,第一次的时候数量是 1
         */
        for (i in circleList.indices) 
            circlePaint.alpha = circleAlphaList[i];
            var circleWidth = circleList[i];
            canvas.drawCircle(centerXPoint, centerYPoint, circleWidth.toFloat(), circlePaint)
            circleList[i] = DIFFUSE_DISTANCE + circleList[i];

            /**
             * 设置当前透明度
             */
            circleAlphaList[i] = if (circleAlphaList[i] - circleAlphaReduceStep <= 0) 0 else 
                circleAlphaList[i] - circleAlphaReduceStep
            
        

        /**
         * 如果最内圈的宽度大于CENTER_CIRCLE_SIZE,则增加下一个圆
         */
        if (circleList[circleList.lastIndex] > CENTER_CIRCLE_SIZE) 
            addNewCircle();
        

        /**
         * 如果最外圈的宽度大于measuredWidth,则益处
         */
        if (circleList[0] >= measuredWidth - 100) 
            circleList.removeAt(0);
            circleAlphaList.removeAt(0);

        

        /**
         * 延迟75毫秒后开始下一次
         */
        postInvalidateDelayed(50)
    



以上是关于代码片|水波纹的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 HTML5 Canvas 制作水波纹效果

如何使用 HTML5 Canvas 制作水波纹效果

点击水波纹效果

Android自定义水波纹动画Layout

Android——实现渐变色水波纹效果源码

水波纹的动态效果