Android定时器

这个一个简单的倒计时定时器,分别为3S,5S和10S。倒计时的过程中,界面上会显示数字,,数字的显示用来了一些动画,看起来效果非常炫。

倒计时完成后,会弹出一个Toast,提示用户倒计时结束。

这个程序是自己写的,大家可以在自己的程序中集成来使用,然后在一些状态下(比如,倒计时结束)来完成自己的动作。

xml代码如下:

<span style="font-family:SimSun;font-size:14px;"><LinearLayout xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"android:background="@android:color/background_dark" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/timer_three_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="3S倒计时"/><Buttonandroid:id="@+id/timer_five_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="5S倒计时"/><Buttonandroid:id="@+id/timer_ten_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="10S倒计时"/></LinearLayout><LinearLayoutandroid:id="@+id/timer_root_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:layout_marginTop="100dp" ></LinearLayout></LinearLayout></span>

Java代码如下:

<span style="font-family:SimSun;font-size:14px;">package com.jackie.timer;import android.os.Handler;import android.os.Message;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.ScaleAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Toast;public class MainActivity extends ActionBarActivity implements View.OnClickListener {private LinearLayout mTimerRootView;private Button mThreeBtn;private Button mFiveBtn;private Button mTenBtn;private ImageView mTimerView;private AnimationSet mAnimationSet;//用来记录倒计时的时间private int mCurrentValue = 0;private final int SECOND_1 = 1000;/*** @fields TIMER_ICON_IDS : TODO 倒计时事件图片*/public static final int[] TIMER_ICON_IDS = new int[]{0, //从1开始,没有0R.drawable.camera_timer_icon_1,R.drawable.camera_timer_icon_2,R.drawable.camera_timer_icon_3,R.drawable.camera_timer_icon_4,R.drawable.camera_timer_icon_5,R.drawable.camera_timer_icon_6,R.drawable.camera_timer_icon_7,R.drawable.camera_timer_icon_8,R.drawable.camera_timer_icon_9,R.drawable.camera_timer_icon_10,};private static final int MSG_UPDATE_TIMER_VALUE = 0;private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_UPDATE_TIMER_VALUE:remindTimeValue();break;}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTimerRootView = (LinearLayout) findViewById(R.id.timer_root_view);mThreeBtn = (Button) findViewById(R.id.timer_three_second);mFiveBtn = (Button) findViewById(R.id.timer_five_second);mTenBtn = (Button) findViewById(R.id.timer_ten_second);mThreeBtn.setOnClickListener(this);mFiveBtn.setOnClickListener(this);mTenBtn.setOnClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.timer_three_second:mCurrentValue = 3;break;case R.id.timer_five_second:mCurrentValue = 5;break;case R.id.timer_ten_second:mCurrentValue = 10;break;}mHandler.sendEmptyMessage(MSG_UPDATE_TIMER_VALUE);}private void remindTimeValue() {if (mCurrentValue <= 0) {Toast.makeText(this, "倒计时结束", Toast.LENGTH_LONG).show();} else {//更新界面showTimerRemind();mCurrentValue–;//每个1S再发送消息mHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIMER_VALUE, SECOND_1);}}private void showTimerRemind() {runOnUiThread(new Runnable() {@Overridepublic void run() {//创建倒计时显示的ViewcreatTimerView();//创建动画initAnimation();if(mCurrentValue > 0 && mCurrentValue < TIMER_ICON_IDS.length){//【功能说明】该方法用于设置一个动画效果执行完毕后,View对象保留在终止的位置。// 该方法的执行,需要首先通过setFillEnabled方法使能填充效果,否则设置无效。mAnimationSet.setFillAfter(true);mTimerView.setImageDrawable(getDrawable(TIMER_ICON_IDS[mCurrentValue]));mTimerView.setVisibility(View.VISIBLE);mTimerView.startAnimation(mAnimationSet);}}});}private void creatTimerView() {if(mTimerView == null) {mTimerView = new ImageView(MainActivity.this);mTimerRootView.addView(mTimerView);}}private void initAnimation() {ScaleAnimation mScale = new ScaleAnimation(1.0f, 0.5f,//X from 1 to 0.51.0f, 0.5f,//Y from 1 to 0.5Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);AlphaAnimation mAlpha = new AlphaAnimation(0f, 1f);mAnimationSet = new AnimationSet(true);mAnimationSet.addAnimation(mScale);mAnimationSet.addAnimation(mAlpha);mAnimationSet.setDuration(600);mAnimationSet.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {if(mTimerView != null){mTimerView.setVisibility(View.INVISIBLE);mAnimationSet.setFillAfter(false);}}@Overridepublic void onAnimationEnd(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}});mAnimationSet.setFillAfter(true);}}</span>运行的界面如下:

未经一番寒彻骨,焉得梅花扑鼻香

Android定时器

相关文章:

你感兴趣的文章:

标签云: