android中倒计时动画,Android实现倒计时效果
本文实例为大家分享了Android实现倒计时效果的具体代码,供大家参考,具体内容如下
一个倒计时的效果
先看效果图:
直接上代码:
这里是关于倒计时 …天时分秒…的逻辑判断
/**
* 倒计时计算
*/
private void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
if (mHour < 0) {
// 倒计时结束
mHour = 23;
mDay--;
if(mDay < 0){
// 倒计时结束
mDay = 0;
mHour= 0;
mMin = 0;
mSecond = 0;
}
}
}
}
}
定时器主要代码如下…当然也可以开线程或者开后台服务来处理…只是没那种必要…定时器就可以搞定容易控制…毕竟倒计时时间起点…你总得后台获取吧,不是做时钟闹钟…如果是做时钟闹钟…拿你也不用考虑后台服务或者自己开线程…而是使用AlarmManager来实现
/**
* 开启倒计时
* //time为Date类型:在指定时间执行一次。
* timer.schedule(task, time);
* //firstTime为Date类型,period为long,表示从firstTime时刻开始,每隔period毫秒执行一次。
* timer.schedule(task, firstTime,period);
* //delay 为long类型:从现在起过delay毫秒执行一次。
* timer.schedule(task, delay);
* //delay为long,period为long:从现在起过delay毫秒以后,每隔period毫秒执行一次。
* timer.schedule(task, delay,period);
*/
private void startRun() {
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
}
};
mTimer.schedule(mTimerTask,0,1000);
}
修改界面,利用handler来提醒更新界面
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
computeTime();
mDays_Tv.setText(mDay+"");//天数不用补位
mHours_Tv.setText(getTv(mHour));
mMinutes_Tv.setText(getTv(mMin));
mSeconds_Tv.setText(getTv(mSecond));
if (mSecond == 0 && mDay == 0 && mHour == 0 && mMin == 0 ) {
mTimer.cancel();
}
}
}
};
private String getTv(long l){
if(l>=10){
return l+"";
}else{
return "0"+l;//小于10,,前面补位一个"0"
}
}
附带主activity的代码…
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private RelativeLayout countDown;
// 倒计时
private TextView mDays_Tv, mHours_Tv, mMinutes_Tv, mSeconds_Tv;
private long mDay = 23;// 天
private long mHour = 11;//小时,
private long mMin = 56;//分钟,
private long mSecond = 32;//秒
private Timer mTimer;
private Handler timeHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
computeTime();
mDays_Tv.setText(mDay+"");//天数不用补位
mHours_Tv.setText(getTv(mHour));
mMinutes_Tv.setText(getTv(mMin));
mSeconds_Tv.setText(getTv(mSecond));
if (mSecond == 0 && mDay == 0 && mHour == 0 && mMin == 0 ) {
mTimer.cancel();
}
}
}
};
private String getTv(long l){
if(l>=10){
return l+"";
}else{
return "0"+l;//小于10,,前面补位一个"0"
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimer = new Timer();
countDown = (RelativeLayout) findViewById(R.id.countdown_layout);
mDays_Tv = (TextView) findViewById(R.id.days_tv);
mHours_Tv = (TextView) findViewById(R.id.hours_tv);
mMinutes_Tv = (TextView) findViewById(R.id.minutes_tv);
mSeconds_Tv = (TextView) findViewById(R.id.seconds_tv);
startRun();
}
/**
* 开启倒计时
* //time为Date类型:在指定时间执行一次。
* timer.schedule(task, time);
* //firstTime为Date类型,period为long,表示从firstTime时刻开始,每隔period毫秒执行一次。
* timer.schedule(task, firstTime,period);
* //delay 为long类型:从现在起过delay毫秒执行一次。
* timer.schedule(task, delay);
* //delay为long,period为long:从现在起过delay毫秒以后,每隔period毫秒执行一次。
* timer.schedule(task, delay,period);
*/
private void startRun() {
TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
Message message = Message.obtain();
message.what = 1;
timeHandler.sendMessage(message);
}
};
mTimer.schedule(mTimerTask,0,1000);
}
/**
* 倒计时计算
*/
private void computeTime() {
mSecond--;
if (mSecond < 0) {
mMin--;
mSecond = 59;
if (mMin < 0) {
mMin = 59;
mHour--;
if (mHour < 0) {
// 倒计时结束
mHour = 23;
mDay--;
if(mDay < 0){
// 倒计时结束
mDay = 0;
mHour= 0;
mMin = 0;
mSecond = 0;
}
}
}
}
}
}
附带xml的代码
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/countdown_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center" >
android:id="@+id/daojishi_rl"
android:layout_width="match_parent"
android:layout_height="40.0dip"
android:layout_marginLeft="10.0dip"
android:layout_marginRight="10.0dip"
android:gravity="center" >
android:id="@+id/describe_iv"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/img"
android:scaleType="fitXY"
android:gravity="center_vertical" />
android:id="@+id/describe_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="5.0dip"
android:layout_toRightOf="@+id/describe_iv"
android:text="距离开团还有"
android:textSize="25sp" />
android:id="@+id/days_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="4dp"
android:layout_toRightOf="@+id/describe_tv"
android:background="#c2c2c2"
android:gravity="center"
android:text=""
android:textSize="20sp" />
android:id="@+id/colon0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5.0dip"
android:layout_marginRight="3.0dip"
android:layout_toRightOf="@+id/days_tv"
android:text="天"
android:textSize="20sp"
android:textStyle="bold" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/daojishi_rl"
android:gravity="center_horizontal" >
android:id="@+id/hours_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/colon1"
android:background="#c2c2c2"
android:gravity="center"
android:text="23"
android:padding="3dp"
android:textSize="20sp" />
android:id="@+id/colon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="3.0dip"
android:layout_toLeftOf="@+id/minutes_tv"
android:text=":"
android:textSize="20sp"
android:textStyle="bold" />
android:id="@+id/minutes_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/colon2"
android:background="#c2c2c2"
android:gravity="center"
android:text="59"
android:padding="3dp"
android:textSize="20sp" />
android:id="@+id/colon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="3.0dip"
android:layout_toLeftOf="@+id/seconds_tv"
android:text=":"
android:textSize="20sp"
android:textStyle="bold" />
android:id="@+id/seconds_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#c2c2c2"
android:gravity="center"
android:text="59"
android:padding="3dp"
android:textSize="20sp" />
完美实现,直接用就可以了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
android中倒计时动画,Android实现倒计时效果相关推荐
- Android中的动画
视频课:https://edu.csdn.net/course/play/7621 学习内容 Ø 帧动画 Ø 补间动画 Ø 动画方式切换组件 能力目标 Ø 掌握Android中动画的基本概念及分类 Ø ...
- Android中具有动画效果的图片资源
Android动画和Transition系列文章 初识属性动画--使用Animator创建动画 再谈属性动画--介绍以及自定义Interpolator插值器 三谈属性动画--Keyframe以及Vie ...
- 【Android笔记25】Android中的动画效果之逐帧动画
这篇文章,主要介绍Android中的动画效果之逐帧动画. 目录 一.逐帧动画 1.1.什么是逐帧动画 1.2.逐帧动画的使用 (1)创建drawable动画资源<
- Android中的动画有哪几类?各自的特点和区别是什么?
在 android.view.animation包中有四种基本的动画 ,透明/伸缩/移动/旋转. 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 ...
- Android中的动画详析-kotlin的demo
Android中的动画可以分为三种,View动画,帧动画,以及属性动画,实际上帧动画也是View动画的一种,只不过二者表现形式不同,View动画是通过不断地对场景里的动画做图像转换从而产生动画效果是一 ...
- Android中的动画总结
Android 动画 三种总结: 属性动画:动态的改变属性产生动画效果:改变动画的属性,两个重要的类: 1.ValueAnimator 类是先改变值,然后 手动赋值 给对象的属性从而实现动画:是 间接 ...
- android 吐泡泡动画,android仿摩拜贴纸碰撞|气泡碰撞
转载请注明出处 准备 气泡碰撞最重要的就是边缘检测,气泡的运动涉及到重力,方向,重心,线速度,角速度,,等等一系列因素,想要在android 用view描述现实世界中的气泡实在是难度很大.网上查找资料 ...
- android studio实现动画,android studio上的基本动画实现(第一篇)
hello,各位小伙伴们,在不少小伙伴们刚刚开始学习android的时候,经常会有一些project里面须要有一些基本动画的插入,那么具体是要怎么实现呢?咱们接下一块儿分析一下在android中的几种 ...
- Android中获取天气,android中获取即刻天气
android中获取即时天气 先看效果: 需求非常简单,在菜单中加入天气查询的按钮,点击后显示即时天气. 准备工作: 1.下载华为能力SDK:http://imax.vmall.com/nj-camp ...
最新文章
- 图解Ubuntu 9.10 Alpha 6
- Oracle—dmp表的导入导出
- android studio 优化提速方法,Android Studio速度慢(如何加速)?
- Silverlight实用窍门系列:51.Silverlight页面控件的放大缩小、Silverlight和Html控件的互相操作...
- RocketMQ集群知识介绍
- SurfaceGo使用体验(使用半年后)
- 【腾讯游戏面试题】面向对象的设计原则是什么?
- linux系统故障排除
- javaCRC8计算的坑
- python flag用法_python flag什么意思
- 淘宝奇门接口的签名方法
- 分布式主动感知在智能运维中的实践
- 理光Ricoh Pro 907EX 一体机驱动
- 日记(2007年7月3日)
- 百度地图level对应距离(比例尺级别对应的多少米)
- 如何使用JavaScript导入和导出Excel文件
- 专业计算机术语中英文对照(二)
- JMX Connectors
- mac如何卸载python2.7_mac python环境的安装与卸载
- Strut 2的值栈