Android弧形步数进度

阅读: 评论:0

Android弧形步数进度

Android弧形步数进度

Android弧形步数进度

一、效果图

二、代码实现

2.1 自定义属性

    <declare-styleable name="QQStepView"><attr name="outColor" format="color"/><attr name="outWidth" format="dimension"/><attr name="innerColor" format="color"/><attr name="innerWidth" format="dimension"/><attr name="step" format="string"/></declare-styleable>

2.2 代码实现

ample.layoutdemo.widget;t.Context;
t.res.TypedArray;
aphics.Canvas;
aphics.Paint;
aphics.Rect;
aphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;import androidx.annotation.Nullable;ample.layoutdemo.R;public class QQStepView extends View {private static final String TAG = "QQStepView";private int mOuterColor;private int mOuterWidth;private int mInnerColor;private int mInnerWidth;private Paint mOutPaint;private int maxStep = 100;private int currentStep = 20;private Paint mInnerPaint;private Paint mTextPaint;public QQStepView(Context context) {this(context,null);}public QQStepView(Context context, @Nullable AttributeSet attrs) {this(context,attrs,0);}public QQStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context,attrs,defStyleAttr);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QQStepView);mOuterColor = Color(R.styleable.QQStepView_outColor,getResources().lor.black));mOuterWidth = DimensionPixelSize(R.styleable.QQStepView_outWidth,10);mInnerColor = Color(R.styleable.QQStepView_innerColor,getResources().lor.design_default_color_on_primary));mInnerWidth = DimensionPixelSize(R.styleable.QQStepView_innerWidth,5);le();mOutPaint = new Paint();mOutPaint.setAntiAlias(true);mOutPaint.setColor(mOuterColor);mOutPaint.setStyle(Paint.Style.STROKE);mOutPaint.setStrokeWidth(mOuterWidth);mOutPaint.setStrokeCap(Paint.Cap.ROUND);mInnerPaint = new Paint();mInnerPaint.setAntiAlias(true);mInnerPaint.setColor(mInnerColor);mInnerPaint.setStrokeWidth(mInnerWidth);mInnerPaint.setStrokeCap(Paint.Cap.ROUND);mInnerPaint.setStyle(Paint.Style.STROKE);mTextPaint = new Paint();mTextPaint.setAntiAlias(true);mTextPaint.setTextSize(getTextSp(40));}private float getTextSp(float px) {return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,px,getResources().getDisplayMetrics());}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthSize = Size(widthMeasureSpec);int widthMode = Mode(widthMeasureSpec);int heightSize = Size(heightMeasureSpec);int heightMode = Mode(heightMeasureSpec);int size = widthSize < heightSize ? widthSize:heightSize;setMeasuredDimension(size,size);}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {Layout(changed, left, top, right, bottom);}@Overrideprotected void onDraw(Canvas canvas) {Draw(canvas);RectF rectF = new RectF();rectF.left = mOuterWidth/2;rectF.right = getWidth()-mOuterWidth/p = mOuterWidth/2;rectF.bottom = getHeight()-mOuterWidth/2;canvas.drawArc(rectF,135,275,false,mOutPaint);RectF innerRectF = new RectF();innerRectF.left = mOuterWidth/2;innerRectF.right = getWidth()-mOuterWidth/p = mOuterWidth/2;innerRectF.bottom = getHeight()-mOuterWidth/2;float sweepAngle = 275 * ((float)currentStep / maxStep);Log.d(TAG,"sweepAngle"+ sweepAngle);canvas.drawArc(innerRectF,135,sweepAngle,false,mInnerPaint);// 绘制文本String stepText = currentStep + "";Rect textRect = new Rect();TextBounds(stepText,0,stepText.length(),textRect);int dx = getWidth()/2 - textRect.width()/2;Paint.FontMetricsInt fontMetricsInt = FontMetricsInt();int dy = (fontMetricsInt.bottom - p)/2 - fontMetricsInt.bottom;int baseLine = getHeight()/2 + dy;canvas.drawText(stepText,dx,baseLine,mTextPaint);}public synchronized void setMaxStep(int maxStep){this.maxStep = maxStep;}public synchronized void setCurrentStep(int currentStep) {this.currentStep = currentStep;postInvalidate();}
}

2.3 使用控件

 &le.layoutdemo.widget.QQStepViewapp:outWidth="20dp"android:id="@+id/step_view"app:outColor="@color/teal_200"android:layout_width="200dp"app:innerColor="@color/black"app:innerWidth="20dp"android:layout_height="200dp"/>
        final QQStepView qqStepView = findViewById(R.id.step_view);
//        qqStepView.setMaxStep(100);
//        qqStepView.setCurrentStep(80);qqStepView.setMaxStep(3000);ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0,2000);valueAnimator.setDuration(5000);valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float animatedValue = (float) AnimatedValue();qqStepView.setCurrentStep((int)animatedValue);}});valueAnimator.start();

三、源码下载地址

Android自定义View

本文发布于:2024-03-05 01:03:54,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1709617563120809.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:弧形   进度   Android
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23