一种Android 类似电影倒计时效果
2013-10-13 16:07:25 来源:WEB开发网核心提示:1、Activityimport java.util.Timer;import java.util.TimerTask;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import a
1、Activity
import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class Test extends Activity { private long mlCount = 50; private long mCount = 0; TextView tvTime; private Button startbuttondaoji; private Timer timer = null; private TimerTask task = null; private Handler handler = null; private Message msg = null; private ImageView min_progress, min_progress_hand; Animation rotateAnimation; float predegree = 0; boolean okclear = false; @SuppressLint("HandlerLeak") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); } private void init() { setContentView(R.layout.test); tvTime = (TextView) findViewById(R.id.duocitvTime); startbuttondaoji = (Button) findViewById(R.id.startbuttonduoci); min_progress = (ImageView) this.findViewById(R.id.duocimin_progress); min_progress_hand = (ImageView) this .findViewById(R.id.duocimin_progress_hand); tvTime.setText("4"); SaveRun.setisjishi(false); handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case 1: if(mlCount>1){ mlCount--; mCount++; }else{ break; } int totalSec = 0; totalSec = (int) (mlCount / 10); int sec = (totalSec % 60); try { rotateAnimation = new RotateAnimation(predegree, (float) (36 * mCount), Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); rotateAnimation.setDuration(100); rotateAnimation.setFillAfter(false); min_progress_hand.startAnimation(rotateAnimation); min_progress.startAnimation(rotateAnimation); tvTime.setText(String.format("%1$2d", sec)); predegree = (float) (36 * mCount); } catch (Exception e) { tvTime.setText( sec +""); e.printStackTrace(); } break; default: break; } super.handleMessage(msg); } }; } @Override protected void onStart() { star(); super.onStart(); } private void star() { startbuttondaoji.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startbuttondaoji.setVisibility(View.GONE); if (null == timer) { if (null == task) { SaveRun.setisjishi(true); okclear = false; min_progress.setVisibility(View.VISIBLE); task = new TimerTask() { @Override public void run() { if (null == msg) { msg = new Message(); } else { msg = Message.obtain(); } msg.what = 1; handler.sendMessage(msg); } }; } timer = new Timer(true); timer.schedule(task, 100, 100); } } }); } View.OnClickListener startPauseListener = new View.OnClickListener() { @Override public void onClick(View v) { if (null == timer) { if (null == task) { SaveRun.setisjishi(true); okclear = false; min_progress.setVisibility(View.VISIBLE); task = new TimerTask() { @Override public void run() { if (null == msg) { msg = new Message(); } else { msg = Message.obtain(); } msg.what = 1; handler.sendMessage(msg); } }; } timer = new Timer(true); timer.schedule(task, 100, 100); } else { try { SaveRun.setisjishi(false); okclear = true; task.cancel(); task = null; timer.cancel(); timer.purge(); timer = null; handler.removeMessages(msg.what); } catch (Exception e) { e.printStackTrace(); } } } }; }
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接