当前位置: 代码迷 >> Android >> Android容易定时器的实现
  详细解决方案

Android容易定时器的实现

热度:40   发布时间:2016-04-28 01:47:59.0
Android简单定时器的实现

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

这个程序是自己写的,所以只是在UI线程中做的,实际项目上使用肯定需要不断完善。

xml代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity"    android:background="@android:color/background_dark" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">        <Button            android:id="@+id/timer_three_second"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="3S倒计时"/>        <Button            android:id="@+id/timer_five_second"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="5S倒计时"/>        <Button            android:id="@+id/timer_ten_second"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="10S倒计时"/>    </LinearLayout>    <LinearLayout        android: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>

Java代码如下:

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开始,没有0            R.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() {        @Override        public void handleMessage(Message msg) {            switch (msg.what) {                case MSG_UPDATE_TIMER_VALUE:                    remindTimeValue();                    break;            }        }    };    @Override    protected 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);    }    @Override    public 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;    }    @Override    public 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 SimplifiableIfStatement        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    @Override    public 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() {            @Override            public void run() {                //创建倒计时显示的View                creatTimerView();                //创建动画                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.5                1.0f, 0.5f,                         //Y from 1 to 0.5                Animation.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() {            @Override            public void onAnimationStart(Animation animation) {                if(mTimerView != null){                    mTimerView.setVisibility(View.INVISIBLE);                    mAnimationSet.setFillAfter(false);                }            }            @Override            public void onAnimationEnd(Animation animation) {            }            @Override            public void onAnimationRepeat(Animation animation) {            }        });        mAnimationSet.setFillAfter(true);    }}

效果如下:


由于数字的变化有个渐变的过程,所以不太好截图,具体效果大家可以看真机。

其次这个程序没有考虑到一些case,比如,在倒计时的过程中,就不允许再点击按钮等等。

  相关解决方案