Android簡單定時器的實現

來源:互聯網
上載者:User

標籤:定時器

這是一個簡單的定時器,分別為3S,5S和10S。倒計時的過程中,介面上會顯示數字,數位顯示用了一些漸層動畫,效果看起來還不錯。

這個程式是自己寫的,所以只是在UI線程中做的,實際項目上使用肯定需要不斷完善。

xml代碼如下:

<span style="font-family:SimSun;font-size:14px;"><span style="font-family:SimSun;font-size:14px;"><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></span></span>
Java代碼如下:

<span style="font-family:SimSun;font-size:14px;"><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開始,沒有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);    }}</span></span>
效果如下:


由於數位變化有個漸層的過程,所以不太好,具體效果大家可以看真機。

其次這個程式沒有考慮到一些case,比如,在倒計時的過程中,就不允許再點擊按鈕等等。

Android簡單定時器的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.