Implement countdown animation effects and countdown animation Effects

Source: Internet
Author: User

Implement countdown animation effects and countdown animation Effects

The project requires a gif like this.


The View code is

Package com. example; import android. content. context; import android. graphics. canvas; import android. graphics. paint; import android. graphics. paint. style; import android. graphics. rectF; import android. OS. handler; import android. util. attributeSet; import android. util. log; import android. view. view;/*** the default value is light green * @ author SCSI **/public class TimerCountdownView extends View {int mMaxSeconds = 0; float mRateAngle = 0; private float mMaxAngle = 0;/** outer ring **/private float mOutCircleWidth = 2; private int mOutCircleColor = 0xff01BCB3; // start angle private float mOutStartAngle = 145; // scanning angle private float mOutSweepAngle = 250;/** Inner Ring **/private float mInCircleWidth = 10; private int mInCircleColor = 0xffF5F5F5; // starting angle private float mInStartAngle = 145; // scanning angle: private float mInSweepAngle = 250;/** distance between the outer ring and the inner ring **/private float mOutAndIn Padding = 12; // The interval between the foreign aid ring and the circle rainbow // initiate the return command private int mActionHeartbeat = 1; // The interval private int mDelayTime = 1*1000; private CountdownTimerListener mListener; public TimerCountdownView (Context context, AttributeSet attrs) {super (context, attrs);} private Handler mHandler = new Handler () {public void handleMessage (android. OS. message msg) {if (msg. what = mActionHeartbeat) {mMaxAngle = mMaxAngle-mRateAngl E; mMaxSeconds = mMaxSeconds-1; if (mMaxSeconds> = 0) {invalidate (); mHandler. sendEmptyMessageDelayed (mActionHeartbeat, mDelayTime); if (mListener! = Null) {mListener. onCountDown (showTheTimer (mMaxSeconds); mListener. onTimeArrive (false);} // Log. d ("", "remaining" + mMaxSeconds + "second" + "remaining angle:" + mMaxAngle);} else {mListener. onTimeArrive (true) ;}};}; public void updateView () {mHandler. sendEmptyMessage (mActionHeartbeat);} public void destroy () {mHandler. removeMessages (100) ;}@ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); drawInCircle (canvas); drawOutCircle (canvas);} public void drawInCircle (Canvas canvas) {Paint paint = new Paint (); paint. setColor (mInCircleColor); paint. setAntiAlias (true); paint. setStyle (Style. STROKE); paint. setStrokeWidth (mInCircleWidth); float left = mOutAndInPadding + mOutCircleWidth; float top = left; float right = getWidth ()-left; float bottom = getHeight ()-top; rectF oval = new RectF (left, top, right, bottom); canvas. drawArc (oval, mInStartAngle, mInSweepAngle, false, paint);} public void drawOutCircle (Canvas canvas) {Paint paint = new Paint (); paint. setColor (mOutCircleColor); paint. setStyle (Style. STROKE); paint. setAntiAlias (true); paint. setStrokeWidth (mOutCircleWidth); float left = 1; float top = left; float right = getWidth ()-left; float bottom = getHeight ()-top; rectF oval = new RectF (left + mOutCircleWidth, top + mOutCircleWidth, right-mOutCircleWidth, bottom-mOutCircleWidth); canvas. drawArc (oval, mOutStartAngle, mMaxAngle, false, paint);}/*** set the initial maximum time * @ param minute Unit */public void setMaxTime (int minute) {mMaxAngle = mOutSweepAngle; mMaxSeconds = minute * 60; mRateAngle = mMaxAngle/mMaxSeconds;} public void setOutCicleColor (int color) {mOutCircleColor = color;} public void trim (int width) {mOutCircleWidth = width;} public void setInCicleColor (int color) {mInCircleColor = color;} public void setInCicleWidth (int width) {mInCircleWidth = width;} public void setOuterAndInerPadding (int padding) {mOutAndInPadding = padding;} public String showTheTimer (int seconds) {String timer = ""; String sminute = ""; String ssecond = ""; if (seconds> = 0) {int minute = seconds/60; int second = seconds % 60; if (minute <10) {sminute = "0" + minute + ":";} else {sminute = minute + ":";} if (second <10) {ssecond = "0" + second;} else {ssecond = second + "";} timer = sminute + ssecond;} else {timer = "00:00";} return timer ;} public interface CountdownTimerListener {// text format of the current countdown calculation mm-sspublic void onCountDown (String time); // whether the countdown has reached public void onTimeArrive (boolean isArrive );} public void addCountdownTimerListener (CountdownTimerListener listener) {mListener = listener ;}}

Xml Code


<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: background = "@ android: color/white"> <com. example. timerCountdownView android: id = "@ + id/view2" android: layout_width = "120dip" android: layout_height = "120dip" android: layout_centerInParent = "true"/> <TextView android: id = "@ + id/timer" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: textColor = "#01BCB3" android: textSize = "30sp" android: text = "00:00"/> <TextView android: id = "@ + id/timer_hint" android: layout_below = "@ + id/timer" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: textColor = "# cccccc" android: textSize = "13sp" android: layout_marginBottom = "10dip" android: text = "/minute \ n countdown"/> </RelativeLayout>

Activity call code

Package com. example; import android. app. activity; import android. content. context; import android. OS. bundle; import android. widget. textView; import android. widget. toast; import com. example. timerCountdownView. countdownTimerListener; import com. example. circleprogressbar. r; public class SimpleActivity extends Activity {private Context mContext; TimerCountdownView view; TextView mTimer; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_base); mContext = this; view = (TimerCountdownView) findViewById (R. id. view2); mTimer = (TextView) findViewById (R. id. timer); view. setMaxTime (1); view. updateView (); view. addCountdownTimerListener (litener);} CountdownTimerListener litener = new CountdownTimerListener () {@ Overridepublic void onCountDown (String time) {mTimer. setText (time) ;}@ Overridepublic void onTimeArrive (boolean isArrive) {if (isArrive) {Toast. makeText (mContext, "Time to", Toast. LENGTH_SHORT ). show ();}}};}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.