• Android-自定义三角形评分控件


    效果图

    序言

    移动应用开发中,显示数据的方式多种多样,直观的图形展示常常能带给用户更好的体验。本文将介绍如何使用Flutter创建一个自定义三角形纬度评分控件,该控件可以通过动画展示评分的变化,让应用界面更加生动。

    实现思路及步骤

    1. 定义控件属性:首先需要定义控件的基本属性,如宽度、高度、最大评分以及每个顶点的评分值。
    2. 自定义绘制:使用自定义View绘制三角形和评分三角形,并在顶点处绘制空心圆点。
    3. 实现动画效果:使用属性动画ValueAnimator来控制评分动画,使每个顶点的评分从0逐渐增加到对应的评分值。

    代码实现

    定义自定义属性和布局文件

    在res/values/attrs.xml中定义自定义属性:

       <declare-styleable name="TriangleRatingAnimView">
            <attr name="maxRating" format="integer" />
            <attr name="upRating" format="integer" />
            <attr name="leftRating" format="integer" />
            <attr name="rightRating" format="integer" />
            <attr name="strokeColor" format="color" />
            <attr name="strokeWidth" format="dimension" />
            <attr name="ratingStrokeColor" format="color" />
            <attr name="ratingStrokeWidth" format="dimension" />
        </declare-styleable>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    创建自定义View类

    首先,创建一个自定义View类TriangleRatingAnimView,用于绘制三角形和动画效果。

    package com.yxlh.androidxy.demo.ui.rating
    
    import android.animation.ValueAnimator
    import android.content.Context
    import android.graphics.Canvas
    import android.graphics.Color
    import android.graphics.Paint
    import android.graphics.Path
    import android.util.AttributeSet
    import android.util.TypedValue
    import android.view.View
    import androidx.core.content.withStyledAttributes
    import androidx.core.graphics.ColorUtils
    import androidx.interpolator.view.animation.LinearOutSlowInInterpolator
    import com.yxlh.androidxy.R
    
    
    fun Context.dpToPx(dp: Float): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics)
    }
    
    /**
     * 三角形评分控件
     * https://github.com/yixiaolunhui/AndroidXY
     */
    class TriangleRatingAnimView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0,
    ) : View(context, attrs, defStyleAttr) {
    
        var maxRating: Int = 5
            set(value) {
                field = value
                invalidate()
            }
        var upRating: Int = 0
            set(value) {
                field = value
                animateRating()
            }
        var leftRating: Int = 0
            set(value) {
                field = value
                animateRating()
            }
        var rightRating: Int = 0
            set(value) {
                field = value
                animateRating()
            }
        private var strokeColor: Int = Color.GRAY
        private var strokeWidth: Float = context.dpToPx(1.5f)
        private var ratingStrokeColor: Int = Color.RED
        private var ratingStrokeWidth: Float = context.dpToPx(2.5f)
        private var animatedUpRating = 0
        private var animatedLeftRating = 0
        private var animatedRightRating = 0
    
        private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.STROKE
            color = strokeColor
            strokeWidth = this@TriangleRatingAnimView.strokeWidth
        }
    
        private val outerPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.STROKE
            color = ratingStrokeColor
            strokeWidth = this@TriangleRatingAnimView.ratingStrokeWidth
        }
    
        private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.FILL
            color = ColorUtils.setAlphaComponent(ratingStrokeColor, (0.3 * 255).toInt())
        }
        private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.STROKE
            color = ratingStrokeColor
            strokeWidth = context.dpToPx(1.5f)
        }
        private val circleFillPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            style = Paint.Style.FILL
            color = Color.WHITE
        }
    
        init {
            context.withStyledAttributes(attrs, R.styleable.TriangleRatingAnimView) {
                maxRating = getInt(R.styleable.TriangleRatingAnimView_maxRating, 5)
                upRating = getInt(R.styleable.TriangleRatingAnimView_upRating, 0)
                leftRating = getInt(R.styleable.TriangleRatingAnimView_leftRating, 0)
                rightRating = getInt(R.styleable.TriangleRatingAnimView_rightRating, 0)
                strokeColor = getColor(R.styleable.TriangleRatingAnimView_strokeColor, Color.GRAY)
                strokeWidth = context.dpToPx(getDimension(R.styleable.TriangleRatingAnimView_strokeWidth, 2f))
                ratingStrokeColor = getColor(R.styleable.TriangleRatingAnimView_ratingStrokeColor, Color.RED)
                ratingStrokeWidth = context.dpToPx(getDimension(R.styleable.TriangleRatingAnimView_ratingStrokeWidth, 4f))
            }
        }
    
        private fun animateRating() {
            val animator = ValueAnimator.ofFloat(0f, 1f).apply {
                duration = 300
                interpolator = LinearOutSlowInInterpolator()
                addUpdateListener { animation ->
                    val animatedValue = animation.animatedValue as Float
                    animatedUpRating = (upRating * animatedValue).toInt()
                    animatedLeftRating = (leftRating * animatedValue).toInt()
                    animatedRightRating = (rightRating * animatedValue).toInt()
                    invalidate()
                }
            }
            animator.start()
        }
    
        override fun onDraw(canvas: Canvas) {
            super.onDraw(canvas)
            val width = measuredWidth.toFloat()
            val height = measuredHeight.toFloat()
            val circleRadius = context.dpToPx(5f)
            val padding = circleRadius + context.dpToPx(2f)
    
            val p1 = width / 2 to padding
            val p2 = padding to height - padding
            val p3 = width - padding to height - padding
    
            // 绘制外部三角形
            val path = Path().apply {
                moveTo(p1.first, p1.second)
                lineTo(p2.first, p2.second)
                lineTo(p3.first, p3.second)
                close()
            }
            canvas.drawPath(path, paint)
    
            val centroidX = (p1.first + p2.first + p3.first) / 3
            val centroidY = (p1.second + p2.second + p3.second) / 3
    
            // 绘制顶点到重心的连线
            canvas.drawLine(p1.first, p1.second, centroidX, centroidY, paint)
            canvas.drawLine(p2.first, p2.second, centroidX, centroidY, paint)
            canvas.drawLine(p3.first, p3.second, centroidX, centroidY, paint)
    
            val dynamicP1 =
                centroidX + (p1.first - centroidX) * (animatedUpRating / maxRating.toFloat()) to centroidY + (p1.second - centroidY) * (animatedUpRating / maxRating.toFloat())
            val dynamicP2 =
                centroidX + (p2.first - centroidX) * (animatedLeftRating / maxRating.toFloat()) to centroidY + (p2.second - centroidY) * (animatedLeftRating / maxRating.toFloat())
            val dynamicP3 =
                centroidX + (p3.first - centroidX) * (animatedRightRating / maxRating.toFloat()) to centroidY + (p3.second - centroidY) * (animatedRightRating / maxRating.toFloat())
    
            // 绘制内部动态三角形
            val ratingPath = Path().apply {
                moveTo(dynamicP1.first, dynamicP1.second)
                lineTo(dynamicP2.first, dynamicP2.second)
                lineTo(dynamicP3.first, dynamicP3.second)
                close()
            }
            canvas.drawPath(ratingPath, outerPaint)
            canvas.drawPath(ratingPath, fillPaint)
    
            // 绘制动态点上的空心圆
            canvas.drawCircle(dynamicP1.first, dynamicP1.second, circleRadius, circlePaint)
            canvas.drawCircle(dynamicP1.first, dynamicP1.second, circleRadius - context.dpToPx(1.5f), circleFillPaint)
            canvas.drawCircle(dynamicP2.first, dynamicP2.second, circleRadius, circlePaint)
            canvas.drawCircle(dynamicP2.first, dynamicP2.second, circleRadius - context.dpToPx(1.5f), circleFillPaint)
            canvas.drawCircle(dynamicP3.first, dynamicP3.second, circleRadius, circlePaint)
            canvas.drawCircle(dynamicP3.first, dynamicP3.second, circleRadius - context.dpToPx(1.5f), circleFillPaint)
        }
    }
    
    
    • 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
    • 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
    • 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

    定义Activity界面xml文件

    在res/layout/activity_rating.xml中使用自定义View:

    
    
    
        
    
            
    
            
    
            
    
            
    
        
    
        
    定义RatingActivity
    package com.yxlh.androidxy.demo.ui.rating
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    import com.yxlh.androidxy.databinding.ActivityRatingBinding
    import kotlin.random.Random
    
    class RatingActivity : AppCompatActivity() {
    
        private var binding: ActivityRatingBinding? = null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            binding = ActivityRatingBinding.inflate(layoutInflater)
            setContentView(binding?.root)
            binding?.randomizeButton?.setOnClickListener {
                randomizeRatings()
            }
        }
    
        private fun randomizeRatings() {
            val random = Random(System.currentTimeMillis())
            val maxRating = 5 + random.nextInt(6)
            val upRating = 1 + random.nextInt(maxRating)
            val leftRating = 1 + random.nextInt(maxRating)
            val rightRating = 1 + random.nextInt(maxRating)
            binding?.triangleRatingAnimView?.apply {
                this.maxRating = maxRating
                this.upRating = upRating
                this.leftRating = leftRating
                this.rightRating = rightRating
                invalidate()
            }
        }
    }
    
    
    • 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

    通过以上步骤和代码,我们可以创建一个带动画效果的三角形纬度评分控件,使评分展示更加生动和直观。

    详情可见:github.com/yixiaolunhui/AndroidXY

  • 相关阅读:
    C语言实现malloc与free函数完成内存管理
    sqlibs安装及复现
    【C语言】指针和数组的深入理解(第二期)
    文件名太长,批量改名不求人:轻松解决文件名问题
    YOLOv5、YOLOv8改进:C3STR(Swin Transformer)
    2023京东口腔护理赛道行业数据分析(京东销售数据分析)
    制造业生产类数据都有哪些分类,如何分析?
    Toronto Research Chemicals农药检测丨Naled-d6
    游戏引擎,脚本管理模块
    c# var vs dynamic
  • 原文地址:https://blog.csdn.net/u014741977/article/details/139033869