Android 自定义View 绘制一条颜色渐变,粗细渐变的线

自定义View 绘制一条颜色渐变,粗细渐变的线

效果图如下:
Android 自定义View 绘制一条颜色渐变,粗细渐变的线_第1张图片
自定义View 代码

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.View


public class GradientWidthAndColorLine : View {
    private var mPaint : Paint ?= null
    private var mContext :Context ?= null
    private var mAttrs :AttributeSet? = null
    private var mStartSize  = 50f
    private var mEndSize = 10f
    private var mStartColor = 0
    private var mEndColor = 0
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(
        context,
        attrs,
        0
    )

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int
    ) : super(context, attrs, defStyleAttr) {
        mContext = context
        mAttrs = attrs
        init()
    }


    @SuppressLint("Recycle")
    private fun init(){
       mAttrs?.let {
           val obtainStyledAttributes =
               mContext!!.obtainStyledAttributes(mAttrs, R.styleable.GradientWidthAndColorLine)
           mStartColor = obtainStyledAttributes.getColor(R.styleable.GradientWidthAndColorLine_gradient_start_color, 0)
           mEndColor = obtainStyledAttributes.getColor(R.styleable.GradientWidthAndColorLine_gradient_end_color,0)
           mStartSize =  obtainStyledAttributes.getDimension(R.styleable.GradientWidthAndColorLine_gradient_start_size, 50f)
           mEndSize = obtainStyledAttributes.getDimension(R.styleable.GradientWidthAndColorLine_gradient_end_size,10f)
           obtainStyledAttributes.recycle()
       }
        mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mPaint?.let {
            it.style = Paint.Style.FILL
            it.color = mStartColor
        }
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        mPaint?.let {
            val linearGradient = LinearGradient(
                0f, 0f, 0f, measuredHeight.toFloat(), intArrayOf(
                    mStartColor,
                    mEndColor
                ), floatArrayOf(0.0f, 1.0f), Shader.TileMode.CLAMP
            )
            it.shader = linearGradient
            val path = Path()
            if(mStartSize  > mEndSize){

                Log.e("Glide","big")
                path.moveTo(0f,0f)
                path.lineTo( (mStartSize - mEndSize)/2 ,measuredHeight.toFloat())
                path.lineTo(((mStartSize - mEndSize)/2 + mEndSize) ,measuredHeight.toFloat())
                path.lineTo(mStartSize,0f)
                path.lineTo(0f,0f)
            }else{

                Log.e("Glide","small")
                path.moveTo(0f,measuredHeight.toFloat())
                path.lineTo(mEndSize,measuredHeight.toFloat())
                path.lineTo((mEndSize - mStartSize)/ 2  + mStartSize, 0f)
                path.lineTo((mEndSize - mStartSize)/ 2,0f)
                path.lineTo(0f,measuredHeight.toFloat())
            }

            path.close()
            canvas!!.drawPath(path,it)
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val executeMeasure = executeMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(executeMeasure[0],executeMeasure[1])
    }



    private fun executeMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) :ArrayList{
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)

        var width = 0

        width = if(widthMode == MeasureSpec.EXACTLY){
            // 测量模式为MatchParent 或者 赋予了具体的值

            if(mStartSize > mEndSize) {
                mStartSize.toInt()
            }else{
                mEndSize.toInt()
            }

        }else{
            // 测量模式为WrapContent
            if(mStartSize > mEndSize) {
                mStartSize.toInt()
            }else{
                mEndSize.toInt()
            }
        }
        return arrayListOf(width,heightSize)
    }


    fun setStartSize(startSize :Float,refresh :Boolean ?= false){
        mStartSize = startSize
        if(refresh!!){
            requestLayout()
            invalidate()
        }
    }


    fun setEndSize(endSize :Float,refresh :Boolean ?= false){
        mEndSize = endSize
        if(refresh!!){
            requestLayout()
            invalidate()
        }

    }

    fun setStartColor(startColor :Int,refresh :Boolean ?= false){
        mStartColor = startColor
        if(refresh!!){
            requestLayout()
            invalidate()
        }

    }



    fun setEndColor(endColor :Int,refresh :Boolean ?= false){
        mEndColor = endColor
        if(refresh!!){
            requestLayout()
            invalidate()
        }
    }
}

自定义属性

  
        
        
        
        
    

在布局中添加代码使用

  

你可能感兴趣的:(笔记,Android,安卓)