FMSurfaceView.kt 10.7 KB
Newer Older
徐健 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
package com.yidianling.fm.widget

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述:  FM播放页面动画效果
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/05/08
 */
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import com.yidianling.common.tools.RxDeviceTool
import java.lang.Exception
import java.util.*

class FMSurfaceView(context: Context?, attrs: AttributeSet?) : SurfaceView(context, attrs), SurfaceHolder.Callback {

    private var mHolder: SurfaceHolder? = null

    private var paint: Paint? = null
    private var path: Path? = null
    private var canvas: Canvas? = null

    private var timer: Timer? = null
    private var timerTask: TimerTask? = null

    private var minRadius: Float = (RxDeviceTool.getScreenWidth(context) * 150 / 375 / 2).toFloat() // 初始化最小六边形所在圆半径
    private var ringRadius: Float = (RxDeviceTool.getScreenWidth(context) * 145 / 375 / 2).toFloat() // 初始化内部圆环半径
    private var ringWidth: Float = (RxDeviceTool.getScreenWidth(context) * 10 / 375 / 2).toFloat() // 初始化内部圆环宽度
    private var presentRadius: Float = minRadius // 初始化当前最小六边形所在圆半径
    private var presentRadiusChangeNumber: Float = 1f
    private var maxRadius: Float = (RxDeviceTool.getScreenWidth(context) * 198 / 375 / 2).toFloat() // 初始化六边形所在圆最大时的半径

    private var circleSpace: Float = maxRadius - minRadius // 初始化圆边之间的距离
    private var clockwise: Boolean = true  // 默认最小的六边形为顺时针旋转
    private var hintColor: String = "FFFFFF" // 默认不透明色
    private var percentColorNum: Float = 105f // 初始化初始颜色透明度 255f的时候最小的六边形为纯色没有透明度,颜色为hintColor
    private var cornerRadius: Float = (RxDeviceTool.getScreenWidth(context) * 150 / 375 / 2).toFloat() // paint初始拐角半径
    private var periodTime: Long = 30 // 定时任务间隔时间
    private var firstPlay: Boolean = false
    private var xfermode: PorterDuffXfermode? = null

    init {
        initPaint()
        initPath()

        mHolder = holder
        mHolder?.addCallback(this)

        //该行代码为设置透明
        setZOrderOnTop(true)
        mHolder?.setFormat(PixelFormat.TRANSLUCENT)

        xfermode = PorterDuffXfermode(PorterDuff.Mode.XOR)//绘制交集,显示非交集
    }

    private fun initPath() {
        path = Path()
    }

    private fun initPaint() {
        paint = Paint(Color.GRAY)
        paint?.color = Color.WHITE
        paint?.isAntiAlias = true //抗锯齿
        paint?.style = Paint.Style.FILL //设置画笔为填充模式
        paint?.strokeCap = Paint.Cap.ROUND //画笔拐角为圆弧
        paint?.pathEffect = CornerPathEffect(cornerRadius) //设置path拐角半径
    }

    fun playTimer() {
        if (null == timer) {
            timer = Timer()
            timerTask = timerTask ?: object : TimerTask() {
                override fun run() {
                    drawAnimate()
                }
            }
            timer?.let {
                timer!!.schedule(timerTask, 0, periodTime)
            }
        }
    }

    private fun drawAnimate() {
徐健 committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        synchronized(this) {
                mHolder?.let {
                    canvas = mHolder?.lockCanvas()
                    canvas?.let {
                        //清空画布,进行重绘
                        canvas?.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

                        //重置最小六边形半径
                        resetPresentRadius()

                        paint?.style = Paint.Style.FILL //设置画笔为填充模式

                        //绘制四个变化的六边形
                        drawMinHexagon()
                        drawMiddleHexagon()
                        drawMaxHexagon()
                        drawMMaxHexagon()

                        //绘制图片外面包围的一层圆
                        canvas?.let {
                            paint?.strokeWidth = ringWidth + 0.5f
                            paint?.style = Paint.Style.STROKE //设置画笔为线模式
                            paint?.color = Color.parseColor("#D0FFFFFF")
                            paint?.setShadowLayer(ringWidth, 1F, 1F, Color.parseColor("#D0FFFFFF"))
                            canvas!!.drawCircle((canvas?.width!! / 2).toFloat(), (canvas?.height!! / 2).toFloat(), ringRadius + 0.5f, paint!!)
                            paint?.setShadowLayer(0F, 0F, 0F, Color.WHITE)
                        }

                        presentRadius += presentRadiusChangeNumber //每次循环presentRadius+presentRadiusChangeNumber
                    }
                    //这里有些机型上面会出现异常,暴力try一次,也可以用 mHolder?.surface?.isValid先进行一次判断后进行try
                    try {
                        mHolder?.unlockCanvasAndPost(canvas) // android 4.3 会有IllegalArgumentException
                    } catch (e: Exception) {
                    }
徐健 committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
                }
        }
    }

    private fun drawPath(point: Point, pointRadius: Float) {
        path = Path()
        path?.moveTo(point.x + canvas?.width!! / 2, point.y + canvas?.height!! / 2)
        for (i in 1..5) {
            path?.lineTo((point.x * Math.cos(getRadian(60 * i)) - point.y * Math.sin(getRadian(60 * i))).toFloat() + canvas?.width!! / 2,
                    (point.x * Math.sin(getRadian(60 * i)) + point.y * Math.cos(getRadian(60 * i))).toFloat() + canvas?.height!! / 2)
        }
        path?.close()

        path?.addCircle((canvas?.width!! / 2).toFloat(), (canvas?.height!! / 2).toFloat(), minRadius, Path.Direction.CCW)
        path?.fillType = Path.FillType.EVEN_ODD

        /**
         * 这边是2,1,根号3的三角形, 这部分代码绘制了从圆到六边形的变化
         */
        val mRadius = (pointRadius * 1.7320508075689 / 2).toFloat()
        if (mRadius > minRadius) {
            //设置paint 拐角半径
            if (mRadius - minRadius < 50f)
                paint?.pathEffect = CornerPathEffect(cornerRadius - (mRadius - minRadius) * 2)
            else
                paint?.pathEffect = CornerPathEffect(cornerRadius - 100f)

            canvas?.drawPath(path!!, paint!!)
        }
    }

    private fun getRotatePoint(point: Point, angle: Int): Point {
        return Point(
                (point.x * Math.cos(getRadian(angle)) - point.y * Math.sin(getRadian(angle))).toFloat(),
                (point.x * Math.sin(getRadian(angle)) + point.y * Math.cos(getRadian(angle))).toFloat()
        )
    }

    /**
     * 绘制最小的六边形
     */
    private fun drawMinHexagon() {
        val radius: Float = presentRadius
        var percentOfColor: String = getColorPercent(radius)
        percentOfColor = if (percentOfColor.length == 2) percentOfColor else "0${percentOfColor[0]}"
        paint?.color = Color.parseColor("#$percentOfColor$hintColor")
        canvas?.let {
            if (clockwise) {
                drawPath(getRotatePoint(Point(0f, radius), (radius - minRadius).toInt()), radius)
            } else {
                drawPath(getRotatePoint(Point(0f, radius), -(radius - minRadius).toInt()), radius)
            }
        }
    }

    /**
     * 绘制中间的六边形
     */
    private fun drawMiddleHexagon() {
        val radius: Float = presentRadius + circleSpace
        var percentOfColor: String = getColorPercent(radius)
        percentOfColor = if (percentOfColor.length == 2) percentOfColor else "0${percentOfColor[0]}"
        paint?.color = Color.parseColor("#$percentOfColor$hintColor")
        canvas?.let {
            if (clockwise) {
                drawPath(getRotatePoint(Point(0f, radius), -(radius - minRadius).toInt()), radius)
            } else {
                drawPath(getRotatePoint(Point(0f, radius), (radius - minRadius).toInt()), radius)
            }
        }
    }

    /**
     * 绘制最大的六边形
     */
    private fun drawMaxHexagon() {
        val radius: Float = presentRadius + circleSpace * 2
        var percentOfColor: String = getColorPercent(radius)
        percentOfColor = if (percentOfColor.length == 2) percentOfColor else "0${percentOfColor[0]}"
        paint?.color = Color.parseColor("#$percentOfColor$hintColor")
        canvas?.let {
            if (clockwise) {
                drawPath(getRotatePoint(Point(0f, radius), (radius - minRadius).toInt()), radius)
            } else {
                drawPath(getRotatePoint(Point(0f, radius), -(radius - minRadius).toInt()), radius)
            }
        }
    }

    /**
     * 绘制最大的六边形之再来一个更大的
     */
    private fun drawMMaxHexagon() {
        val radius: Float = presentRadius + circleSpace * 3
        var percentOfColor: String = getColorPercent(radius)
        percentOfColor = if (percentOfColor.length == 2) percentOfColor else "0${percentOfColor[0]}"
        paint?.color = Color.parseColor("#$percentOfColor$hintColor")
        canvas?.let {
            if (clockwise) {
                drawPath(getRotatePoint(Point(0f, radius), -(radius - minRadius).toInt()), radius)
            } else {
                drawPath(getRotatePoint(Point(0f, radius), (radius - minRadius).toInt()), radius)
            }
        }
    }

    private fun getColorPercent(radius: Float): String {
        return Integer.toHexString(((1f - (radius - minRadius) / (maxRadius + circleSpace * 3 - minRadius)) * percentColorNum).toInt())
    }

    /**
     * 重置最小半径
     */
    private fun resetPresentRadius() {
        if (presentRadius >= maxRadius) {
            presentRadius = minRadius
            clockwise = !clockwise
            firstPlay = true
        }
    }

    fun stopTimer() {
        releaseResource()
    }

    /**
     * 清除定时任务,进行资源释放
     */
    private fun releaseResource() {
        timer?.let {
            timer?.cancel()
            timerTask?.cancel()
            timerTask = null
            timer = null
        }
    }

    /**
     * 根据角度获取弧度值
     */
    private fun getRadian(angle: Int): Double {
        return angle * (Math.PI / 180)
    }

    override fun surfaceDestroyed(holder: SurfaceHolder?) {
        //mHolder?.removeCallback(this)
        releaseResource()
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
    }

    override fun surfaceCreated(holder: SurfaceHolder?) {
        this.setLayerType(View.LAYER_TYPE_SOFTWARE, paint)
        drawAnimate()
    }

    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
    }

    class Point(val x: Float, val y: Float)

}