在圆弧上移动一个点可以在圆弧上移动一定时间,可以停止

问题描述

我必须跟随Ui,当我玩这个小圆圈时,它会以弧形移动,当我停止时它会停止。我还可以设置蓝色小圆圈的旋转时间。

enter image description here

到目前为止,我已经实现了以下代码:这给了我一个圆弧,但我无法将较小的圆圈旋转到较大的圆圈上。

 public class CustoCustomProgressBar : View{
    private var path : Path ? =null
    private var mPrimaryPaint: Paint? = null
    private var mSecondaryPaint: Paint? = null
    private var mBackgroundPaint: Paint? = null
    private var mTextPaint: TextPaint? = null
    private var mProgressDrawable : Drawable ? = null
    private var mRectF: RectF? = null

    private var mDrawText = false
    private var mTextColor = 0

    private var mSecondaryProgressColor = 0
    private var mPrimaryProgressColor = 0
    private var mBackgroundColor = 0

    private var mstrokeWidth = 0
    private var mProgress = 0
    var secodaryProgress = 0
        private set

     private  var firstArcprogress = 0
    private var secondArcProgress = 0
    private var thirdArcProgress = 0

    private var mFristArcCapSize = 0
    private var mSecondArcCapSize = 0
    private var mThirdArcCapSize = 0

    private var isFristCapVisible = false
    private var isSecondCapVisible = false
    private var isThirdCapVisible = false

    private var capColor = 0

    private var mPrimaryCapSize = 0
    private var mSecondaryCapSize = 0
    var isPrimaryCapVisible = false
    var isSecondaryCapVisible = false

    private var x = 0
    private var y = 0
    private var mWidth = 0
    private var mHeight = 0

    constructor(context: Context) : super(context) {
        init(context,null)
    }

    constructor(context: Context,attrs: AttributeSet?) : super(context,attrs) {
        init(context,attrs)
    }

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

    fun init(context: Context,attrs: AttributeSet?) {
        val a: TypedArray
        a = if (attrs != null) {
            context.getTheme().obtainStyledAttributes(
                attrs,R.styleable.CustomProgressBar,0
            )
        } else {
            throw IllegalArgumentException("Must have to pass the attributes")
        }
        try {
            mProgressDrawable = resources.getDrawable(R.drawable.test)
            mDrawText = a.getBoolean(R.styleable.CustomProgressBar_showProgresstext,false)
            mBackgroundColor =
                a.getColor(
                    R.styleable.CustomProgressBar_backgroundColor,resources.getColor(R.color.white)
                )
            mPrimaryProgressColor =
                a.getColor(
                    R.styleable.CustomProgressBar_progressColor,resources.getColor(R.color.white)
                )
            mSecondaryProgressColor =
                a.getColor(
                    R.styleable.CustomProgressBar_secondaryProgressColor,resources.getColor(R.color.black)
                )
            capColor =
                a.getColor(
                    R.styleable.CustomProgressBar_capColor,resources.getColor(R.color.color_9bc6e6_mind)
                )
             firstArcprogress =a.getInt(R.styleable.CustomProgressBar_firstArcProgress,0)
            mProgress = a.getInt(R.styleable.CustomProgressBar_progress,0)
            secodaryProgress = a.getInt(R.styleable.CustomProgressBar_secondaryProgress,0)
            mstrokeWidth = a.getDimensionPixelSize(R.styleable.CustomProgressBar_strokeWidth,10)
            mTextColor = a.getColor(
                R.styleable.CustomProgressBar_textColor,resources.getColor(R.color.black)
            )

            mPrimaryCapSize = a.getInt(R.styleable.CustomProgressBar_primaryCapSize,20)
            mSecondaryCapSize = a.getInt(R.styleable.CustomProgressBar_secodaryCapSize,20)
            isPrimaryCapVisible =
                a.getBoolean(R.styleable.CustomProgressBar_primaryCapVisibility,true)
            isSecondaryCapVisible =
                a.getBoolean(R.styleable.CustomProgressBar_secodaryCapVisibility,true)

            isFristCapVisible = a.getBoolean(R.styleable.CustomProgressBar_firstCapVisibility,true)
            isSecondCapVisible =
                a.getBoolean(R.styleable.CustomProgressBar_secodaryCapVisibility,false)
            isThirdCapVisible =
                a.getBoolean(R.styleable.CustomProgressBar_thirdCapVisibility,false)
        } finally {
            a.recycle()
        }
        mBackgroundPaint = Paint()
        mBackgroundPaint!!.setAntiAlias(true)
        mBackgroundPaint!!.setStyle(Paint.Style.stroke)
        mBackgroundPaint!!.setstrokeWidth(mstrokeWidth.toFloat())
        mBackgroundPaint!!.setColor(mBackgroundColor)

        mPrimaryPaint = Paint()
        mPrimaryPaint!!.setAntiAlias(true)
        mPrimaryPaint!!.setStyle(Paint.Style.stroke)
        mPrimaryPaint!!.setstrokeWidth(mstrokeWidth.toFloat())
        mPrimaryPaint!!.setColor(capColor)



        mSecondaryPaint = Paint()
        mSecondaryPaint!!.setAntiAlias(true)
        mSecondaryPaint!!.setStyle(Paint.Style.stroke)
        mSecondaryPaint!!.setstrokeWidth((mstrokeWidth - 2).toFloat())
        mSecondaryPaint!!.setColor(mSecondaryProgressColor)

        mTextPaint = TextPaint()
        mTextPaint!!.color = mTextColor
        mRectF = RectF()
    }

    override fun onSizeChanged(w: Int,h: Int,oldw: Int,oldh: Int) {
        super.onSizeChanged(w,h,oldw,oldh)
        mRectF!![paddingLeft.toFloat(),paddingTop.toFloat(),(w - paddingRight).toFloat()] =
            (h - paddingBottom).toFloat()
        mTextPaint!!.textSize = (w / 5).toFloat()
        x = w / 2 - (mTextPaint!!.measureText("$mProgress%") / 2).toInt()
        y = (h / 2 - (mTextPaint!!.descent() + mTextPaint!!.ascent()) / 2).toInt()

        mWidth = w
        mHeight = h

        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mPrimaryPaint?.setStyle(Paint.Style.stroke)
        mSecondaryPaint?.setStyle(Paint.Style.stroke)

        // for drawing a full progress .. The background circle
        mRectF?.let {
           mBackgroundPaint?.let { it1 -> canvas.drawArc(it,270F,180F,false,it1) } }
        path = Path()
        path?.arcTo(mRectF,true)

        mRectF?.let {
            mBackgroundPaint?.let { it1 ->
                canvas.drawArc(it,95F,80F,it1)

            }
        }

        mRectF?.let {
            mBackgroundPaint?.let { it1 ->
                canvas.drawArc(it,it1)
            }
        }
//        // for drawing a secondary progress circle
//        val secondarySwipeangle = secodaryProgress * 360 / 100
//        mRectF?.let { mSecondaryPaint?.let { it1 ->
//            canvas.drawArc(it,secondarySwipeangle.toFloat(),//                it1
//            )
//        } }
//
        val firstArcProgresSwipeAngle = firstArcprogress * 180 / 100
//        mRectF?.let {
//            mPrimaryPaint?.let { it1 ->
//                canvas.drawArc(
//                    it,firstArcProgresSwipeAngle.toFloat(),//                    it1
//                )
//            }
//        }

//        // for drawing a main progress circle
//        val primarySwipeangle = mProgress * 360 / 100
//        mRectF?.let { mPrimaryPaint?.let { it1 ->
//            canvas.drawArc(it,primarySwipeangle.toFloat(),//                it1
//            )
//        } }

//        // for cap of secondary progress
//        val r = (height - paddingLeft * 2) / 2 // Calculated from canvas width
//        var Trad = (secondarySwipeangle - 90) * (Math.PI / 180.0) // = 5.1051
//        var x = (r * Math.cos(Trad)).toInt()
//        var y = (r * Math.sin(Trad)).toInt()
//        mSecondaryPaint?.setStyle(Paint.Style.FILL)
//        if (isSecondaryCapVisible) mSecondaryPaint?.let {
//            canvas.drawCircle(
//                (x + mWidth / 2).toFloat(),//                (y + mHeight / 2).toFloat(),//                mSecondaryCapSize.toFloat(),//                it
//            )
//        }

        val r = (height - paddingLeft * 2) / 2
        // for cap of primary progress
        var Trad = (firstArcProgresSwipeAngle - 90) * (Math.PI / 180.0) // = 5.1051
        x = (r * Math.cos(Trad)).toInt()
        y = (r * Math.sin(Trad)).toInt()



        mPrimaryPaint?.setStyle(Paint.Style.FILL)
        if (isPrimaryCapVisible) mPrimaryPaint?.let {
            canvas.drawCircle(
                (x + mWidth / 2).toFloat(),(y + mHeight / 2).toFloat(),mPrimaryCapSize.toFloat(),it
            )
        }
        if (mDrawText) mTextPaint?.let {
            canvas.drawText(
                "$mProgress%",x.toFloat(),y.toFloat(),it
            )
        }
    }

    fun setDrawText(mDrawText: Boolean) {
        this.mDrawText = mDrawText
        invalidate()
    }

    override fun setBackgroundColor(mBackgroundColor: Int) {
        this.mBackgroundColor = mBackgroundColor
        mBackgroundPaint?.setColor(mBackgroundColor)
        invalidate()
    }

    fun setstrokeWidth(mstrokeWidth: Int) {
        this.mstrokeWidth = mstrokeWidth
        invalidate()
    }

    fun setSecondaryProgress(mSecondaryProgress: Int) {
        secodaryProgress = mSecondaryProgress
        invalidate()
    }

    fun setTextColor(mTextColor: Int) {
        this.mTextColor = mTextColor
        mTextPaint!!.color = mTextColor
        invalidate()
    }

    var secondaryProgressColor: Int
        get() = mSecondaryProgressColor
        set(mSecondaryProgressColor) {
            this.mSecondaryProgressColor = mSecondaryProgressColor
            mSecondaryPaint?.setColor(mSecondaryProgressColor)
            invalidate()
        }

    var primaryProgressColor: Int
        get() = mPrimaryProgressColor
        set(mPrimaryProgressColor) {
            this.mPrimaryProgressColor = mPrimaryProgressColor
            mPrimaryPaint?.setColor(mPrimaryProgressColor)
            invalidate()
        }


    var progress: Int
        get() = mProgress
        set(mProgress) {
            while (this.mProgress <= mProgress) {
                postInvalidateDelayed(150)
                this.mProgress++
            }
        }

    fun getBackgroundColor(): Int {
        return mBackgroundColor
    }

    var primaryCapSize: Int
        get() = mPrimaryCapSize
        set(mPrimaryCapSize) {
            this.mPrimaryCapSize = mPrimaryCapSize
            invalidate()
        }

    var secondaryCapSize: Int
        get() = mSecondaryCapSize
        set(mSecondaryCapSize) {
            this.mSecondaryCapSize = mSecondaryCapSize
            invalidate()
        }

     var arcprogress: Int
    get() = firstArcprogress
    set(firstArcprogress) {
        while (this.firstArcprogress <= firstArcprogress) {
            postInvalidateDelayed(150)
            this.firstArcprogress = firstArcprogress
        }
    }

    fun getPath(): Path? {
        return path
    }

    fun getXCOORDINTE(): Float{
        return x.toFloat()
    }

    fun getYCoordinate(): Float{
        return y.toFloat()
    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)