Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the circular countdown display control by android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "android how to customize the circular countdown display control", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "android how to customize the circular countdown display control" this article.

Code block

Properties that the attr.xml control needs to use:

CountDownView.java

Public class CountDownView extends View {/ / Wheel color private int mRingColor; / / Wheel width private float mRingWidth; / / Wheel progress value text size private int mRingProgessTextSize; / / width private int mWidth; / / height private int mHeight; private Paint mPaint; / / rectangular area of the ring private RectF mRectF; / / private int mProgessTextColor; private int mCountdownTime; private float mCurrentProgress; private OnCountDownFinishListener mListener; public CountDownView (Context context) {this (context, null) } public CountDownView (Context context, AttributeSet attrs) {this (context, attrs, 0);} public CountDownView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes (attrs, R.styleable.CountDownView); mRingColor = a.getColor (R.styleable.CountDownView_ringColor, context.getResources (). GetColor (R.color.colorAccent); mRingWidth = a.getFloat (R.styleable.CountDownView_ringWidth, 40) MRingProgessTextSize = a.getDimensionPixelSize (R.styleable.CountDownView_progressTextSize, DisplayUtils.sp2px (context, 20)); mProgessTextColor = a.getColor (R.styleable.CountDownView_progressTextColor, context.getResources (). GetColor (R.color.colorAccent)); mCountdownTime = a.getInteger (R.styleable.CountDownView_countdownTime, 60); a.recycle (); mPaint = new Paint (Paint.ANTI_ALIAS_FLAG); mPaint.setAntiAlias (true); this.setWillNotDraw (false);} public void setCountdownTime (int mCountdownTime) {this.mCountdownTime = mCountdownTime } @ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {super.onLayout (changed, left, top, right, bottom); mWidth = getMeasuredWidth (); mHeight = getMeasuredHeight (); mRectF = new RectF (0 + mRingWidth / 2,0 + mRingWidth / 2, mWidth-mRingWidth / 2, mHeight-mRingWidth / 2);} @ Override protected void onDraw (Canvas canvas) {super.onDraw (canvas); / * * Ring * / Color mPaint.setColor (mRingColor) / / Hollow mPaint.setStyle (Paint.Style.STROKE); / / width mPaint.setStrokeWidth (mRingWidth); canvas.drawArc (mRectF,-90, mCurrentProgress-360, false, mPaint); / / draw text Paint textPaint = new Paint (); textPaint.setAntiAlias (true); textPaint.setTextAlign (Paint.Align.CENTER); String text = mCountdownTime-(int) (mCurrentProgress / 360 f * mCountdownTime) + "; textPaint.setTextSize (mRingProgessTextSize); textPaint.setColor (mProgessTextColor) / / display Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt (); int baseline = (int) ((mRectF.bottom + mRectF.top-fontMetrics.bottom-fontMetrics.top) / 2); canvas.drawText (text, mRectF.centerX (), baseline, textPaint);} private ValueAnimator getValA (long countdownTime) {ValueAnimator valueAnimator = ValueAnimator.ofFloat (0,100); valueAnimator.setDuration (countdownTime); valueAnimator.setInterpolator (new LinearInterpolator ()); valueAnimator.setRepeatCount (0); return valueAnimator } / * start countdown * / public void startCountDown () {setClickable (false); ValueAnimator valueAnimator = getValA (mCountdownTime * 1000); valueAnimator.addUpdateListener (new ValueAnimator.AnimatorUpdateListener () {@ Override public void onAnimationUpdate (ValueAnimator animation) {float I = Float.valueOf (String.valueOf (animation.getAnimatedValue (); mCurrentProgress = (int) (360 * (I / 100f)); invalidate ();}}); valueAnimator.start () ValueAnimator.addListener (new AnimatorListenerAdapter () {@ Override public void onAnimationEnd (Animator animation) {super.onAnimationEnd (animation); / / countdown end callback if (mListener! = null) {mListener.countDownFinished ();} setClickable (true);}});} public void setAddCountDownListener (OnCountDownFinishListener mListener) {this.mListener = mListener;} public interface OnCountDownFinishListener {void countDownFinished ();}}

MainActivity.java

Package com.ouyuan.demo.myapplication;import android.animation.ValueAnimator;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {CountDownView cdv; TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); cdv = (CountDownView) findViewById (R.id.countDownView) Cdv.setAddCountDownListener (new CountDownView.OnCountDownFinishListener () {@ Override public void countDownFinished () {Toast.makeText (MainActivity.this, "countdown ends", Toast.LENGTH_SHORT). Show ();}); cdv.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {cdv.startCountDown ();}});}}

The above is all the content of the article "how to customize the circular countdown display control by android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report