實現倒計時的動畫效果,倒計時動畫效果

來源:互聯網
上載者:User

實現倒計時的動畫效果,倒計時動畫效果

項目要求做一個這樣子的gif


View的代碼是

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;/** * 預設是淺綠色 * @author lq * */public class TimerCountdownView extends View {int mMaxSeconds = 0 ;float mRateAngle = 0 ;private float mMaxAngle = 0;/**外圈相關**/private float mOutCircleWidth = 2;private int mOutCircleColor = 0xff01BCB3;//起始角度private float mOutStartAngle = 145;//掃掠角度度private float mOutSweepAngle = 250;/**內圈相關**/private float mInCircleWidth = 10;private int mInCircleColor = 0xffF5F5F5;//起始角度private float mInStartAngle = 145;//掃掠角度度private float mInSweepAngle = 250;/**外圈與內圈的距離**/private float mOutAndInPadding = 12; //外援環和小圓環虹之間的間隔//發起重回命令private int mActionHeartbeat = 1 ;//間隔時間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 - mRateAngle;mMaxSeconds = mMaxSeconds - 1;if (mMaxSeconds >= 0) {invalidate();mHandler.sendEmptyMessageDelayed(mActionHeartbeat,mDelayTime);if(mListener!=null){mListener.onCountDown(showTheTimer(mMaxSeconds));mListener.onTimeArrive(false);}//Log.d("", "剩餘"+mMaxSeconds+"秒" +"  剩餘角度:"+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);}/** * 設定初始最大時間 * @param minute 單位分 */public void setMaxTime(int minute){mMaxAngle = mOutSweepAngle ;mMaxSeconds = minute * 60 ;mRateAngle = mMaxAngle / mMaxSeconds ;}public void setOutCicleColor(int color){mOutCircleColor = color ;}public void setOutCicleWidth(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{//當前倒計時計算的文本  格式 mm-sspublic void onCountDown(String time);//倒計時是否到達public void onTimeArrive(boolean isArrive);}public void addCountdownTimerListener(CountdownTimerListener listener){mListener = listener ;}}

xml代碼


<?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="/分鐘\n倒計時"/></RelativeLayout>

Activity調用代碼

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, "時間到", Toast.LENGTH_SHORT).show();}}}; }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.