在res/values 文件夹中新建xx.xml,内容如下
<?xml version="1.0" enCoding="utf-8"?><resources> <!--自定义QQ步数控件--> <declare-styleable name="QQStepVIEw"> <!--外圈的颜色--> <attr name="outercolor" format="color" /> <!--内圈的额颜色--> <attr name="innercolor" format="color" /> <!--外圈的宽度--> <attr name="outerWIDth" format="dimension" /> <!--内圈的宽度--> <attr name="innerWIDth" format="dimension" /> <!--最大步数--> <attr name="stepMax" format="integer" /> <!--中间文字的大小--> <attr name="stepTextSize" format="dimension" /> <!--中间文字的颜色--> <attr name="stepTextcolor" format="color" /> </declare-styleable></resources>
2.2继承vIEw实现java代码import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.Rect;import androID.graphics.RectF;import androID.util.AttributeSet;import androID.util.Log;import androID.vIEw.VIEw;import androIDx.annotation.Nullable;/** * Created by DongKing on 2020/11/6 * Version 1.0 * Describe:qq步数 */class QQStepVIEw extends VIEw { private int mStepMax = 10000;//默认最大值 private int mCurrentStep = 0;//当前值 private int mOutercolor = color.RED; private int mInnercolor = color.BLUE; private int mOuterWIDth = 30; private int mInnerWIDth = 18; private int mStepTextSize = 18; private int mStepTextcolor = color.LTGRAY; Paint mInnerPaint;//画内圆的画笔 Paint mOuterPaint;//画外圆的画笔 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 array = context.obtainStyledAttributes(attrs, R.styleable.QQStepVIEw); mOutercolor = array.getcolor(R.styleable.QQStepVIEw_outercolor, mOutercolor); mInnercolor = array.getcolor(R.styleable.QQStepVIEw_innercolor, mInnercolor); mOuterWIDth = (int) array.getDimension(R.styleable.QQStepVIEw_outerWIDth, mOuterWIDth); mInnerWIDth = (int) array.getDimension(R.styleable.QQStepVIEw_innerWIDth, mInnerWIDth); mStepTextSize = array.getDimensionPixelSize(R.styleable.QQStepVIEw_stepTextSize, mStepTextSize); mStepTextcolor = array.getcolor(R.styleable.QQStepVIEw_stepTextcolor, mStepTextcolor); mStepMax = array.getInt(R.styleable.QQStepVIEw_stepMax, mStepMax); array.recycle(); //初始化画笔 mOuterPaint = new Paint(); mOuterPaint.setAntiAlias(true); mOuterPaint.setstrokeCap(Paint.Cap.ROUND); mOuterPaint.setcolor(mOutercolor); mOuterPaint.setstrokeWIDth(mOuterWIDth); mOuterPaint.setStyle(Paint.Style.stroke); mInnerPaint = new Paint(); mInnerPaint.setAntiAlias(true); mInnerPaint.setstrokeCap(Paint.Cap.ROUND); mInnerPaint.setcolor(mInnercolor); mInnerPaint.setstrokeWIDth(mInnerWIDth); mInnerPaint.setStyle(Paint.Style.stroke); mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(mStepTextSize); mTextPaint.setcolor(mStepTextcolor); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec, heightmeasureSpec); //保证是一个正方形 int wIDth = MeasureSpec.getSize(wIDthMeasureSpec); int height = MeasureSpec.getSize(heightmeasureSpec); setMeasuredDimension(wIDth > height ? height : wIDth, wIDth > height ? height : wIDth); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); int center = getWIDth() / 2; //减去比较大的那个的一半 -->防止超出边界 int radius = center - (mOuterWIDth > mInnerWIDth ? mOuterWIDth : mInnerWIDth) / 2; RectF rectF = new RectF(center - radius, center - radius, center + radius, center + radius); //1.画里面的圆弧 canvas.drawArc(rectF, 135, 270, false, mInnerPaint); //2.画外面的圆弧 if (mStepMax == 0) { return; } float sweepAngle = (float) mCurrentStep / mStepMax * 270; canvas.drawArc(rectF, 135, sweepAngle, false, mOuterPaint); //3.画文字 Rect rect = new Rect(); String stepText = mCurrentStep + ""; mTextPaint.getTextBounds(stepText, 0, stepText.length(), rect); int dx = getWIDth() / 2 - rect.wIDth() / 2; Paint.FontMetricsInt FontMetricsInt = mTextPaint.getFontMetricsInt(); int dy = (FontMetricsInt.bottom - FontMetricsInt.top) / 2 - FontMetricsInt.bottom; int baseline = getHeight() / 2 + dy; canvas.drawText(stepText, dx, baseline, mTextPaint); } public synchronized voID setCurrentStep(int currentStep) { this.mCurrentStep = currentStep; invalIDate(); }}
3.使用3.1 布局文件中<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:myapp="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <com.nb.customvIEw.QQStepVIEw androID:ID="@+ID/qq_step_vIEw" androID:layout_wIDth="180dp" androID:layout_height="180dp" androID:layout_gravity="center_horizontal" myapp:innercolor="@androID:color/holo_green_dark" myapp:innerWIDth="10dp" myapp:outercolor="@androID:color/holo_blue_light" myapp:outerWIDth="12dp" myapp:stepMax="10000" myapp:stepTextcolor="@androID:color/holo_blue_light" myapp:stepTextSize="30sp" /> </linearLayout>
3.2 activity中public class MainActivity extends AppCompatActivity { QQStepVIEw mQqStepVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); mQqStepVIEw = findVIEwByID(R.ID.qq_step_vIEw); init(); } private voID init() { ValueAnimator valueAnimator = ObjectAnimator.ofInt(0, 8000); valueAnimator.setDuration(3000); valueAnimator.setInterpolator(new DecelerateInterpolator()); valueAnimator.addUpdateListener(animation -> { int currentStep = (int) animation.getAnimatedValue(); mQqStepVIEw.setCurrentStep(currentStep); }); valueAnimator.start(); }}
总结 以上是内存溢出为你收集整理的2.自定义view-QQ运动步数全部内容,希望文章能够帮你解决2.自定义view-QQ运动步数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)