当前位置: 移动技术网 > IT编程>移动开发>Android > Android中使用TextView实现高仿京东淘宝各种倒计时效果

Android中使用TextView实现高仿京东淘宝各种倒计时效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

红辣椒地毯求婚,fad3小游戏,邪魅王子赖定你

今天给大家带来的是仅仅使用一个textview实现一个高仿京东、淘宝、唯品会等各种电商app的活动倒计时。最近公司一直加班也没来得及时间去整理,今天难得休息想把这个分享给大家,只求共同学习,以及自己后续的复习。为什么会想到使用一个textview来实现呢?因为最近公司在做一些优化的工作,其中就有一个倒计时样式,原来开发的这个控件的同事使用了多个textview拼接在一起的,实现的代码冗余比较大,故此项目经理就说:小宏这个就交给你来优化了,并且还要保证有一定的扩展性,当时就懵逼了。不知道从何处开始优化。然后我就查看京东,饿了么,唯品会等各个app的倒计时,并在开发者中打开层级界面显示,发现他们都有一个共同的特点就是一个view,没有使用多个textview来拼接。相信大家都知道仅仅使用一个textview比使用多个textview拼接去实现的优势吧,下面不妨来看看几个界面就知道了。


看到这个,大家心里自然就想到了自定义view来实现吧。对,自定义view确实可以实现这样的效果。但是今天我们不采用自定义view来做。而是使用一个textview来实现。

由于项目经理要求此次优化的代码具有可扩展性。所以此次代码的设计加了一些面向对象的知识。有一些自己的设计和架构的思路。

此次demo的设计思路:

          1、编写一个倒计时的基类作为实现最普通和最基本的倒计时的功能,没有任何样式,让这个基类去继承countdowntimer类,并且在该基类中

保存一个textview的对象,并且把每次倒计时的数据,显示在textview中,然后公布一个getmdatetv()方法返回一个textview对象即可。然后只要拿到这个textview对象显示界面的布局中即可。非常方便。

          2、然后不同样式的倒计时,只需要编写不同的子类去继承最普通的倒计时基类即可,然后重写其中的设置数据和设置样式的两个方法即可,然后就能给最普通的倒计时添加不同的样式。下次如果需要扩展新的倒计时样式,不需要改变其他类的代码,只需编写一个普通倒计时的派生类重写两个方法即可,使得可扩展性更灵活。

          3、然后通过一个timerutils管理类,去集中承担子类和父类压力,让子类和父类所需实现功能分担到timerutils类中,并且该timerutils管理类是与客户端唯一打交道的类,比如获得倒计时对象以及获得倒计时的textview对象都通过这个管理类分配,避免客户端直接与倒计时的基类和子类打交道。从而使得类的封装性和隐藏性得到体现。

下面可以看下这个demo设计的简单的uml类图:


通过以上思路分析下面我们就看看此次demo的实现需要用到哪些知识点.

 1、countdowntimer类的用法。

    2、spannablestring的用法。

    3、mikyoucountdowntimer的封装。

    4、自定义mikyoubackgroundspan的实现。

一、通过以上的分析我们首先得复习一下有关countdowntimer的知识,countdowntimer是一个很简单的类我们可以看下的它的源码,它的用法自然就知道了。

countdowntimer是一个抽象类。

// 
// source code recreated from a .class file by intellij idea 
// (powered by fernflower decompiler) 
// 
package android.os; 
public abstract class countdowntimer { 
public countdowntimer(long millisinfuture, long countdowninterval) { 
throw new runtimeexception("stub!"); 
} 
public final synchronized void cancel() { 
throw new runtimeexception("stub!"); 
} 
public final synchronized countdowntimer start() { 
throw new runtimeexception("stub!"); 
} 
public abstract void ontick(long var1); 
public abstract void onfinish(); 
}

可以看到倒计时的总时长就是millisfuture,和countdowninterval间隔步长默认是1000ms,所以数据都是通过其构造器进行初始化,然后需要去重写一个回调方法ontick,里面的一个参数就是每隔相应的步长后剩余的时间毫秒数。然后我们只需要在ontick方法中将每隔1000ms时间毫秒数进行时间格式化即可得到相应时间格式的倒计时这就是实现了最基本倒计时样式。格式化倒计时格式采用的是apache中的common的lang包中durationformatutils类中的formatduration,通过传入一个时间格式就会自动将倒计时转换成相应的mtimepattern的样式(hh:mm:ss或dd天hh时mm分ss秒).

二、复习一下有关spannablestring的用法。

在android中edittext用于编辑文本,textview用于显示文本,但是有时候我们需要对其中的文本进行样式等方面的设置。android为我们提供了spannablestring类来对指定文本进行处理。

1) foregroundcolorspan 文本颜色

private void setforegroundcolorspan() {
spannablestring spanstring = new spannablestring("前景色");
foregroundcolorspan span = new foregroundcolorspan(color.blue);
spanstring.setspan(span, 0, 3, spannable.span_exclusive_exclusive);
tv.append(spanstring);
}

2) backgroundcolorspan 文本背景色

private void setbackgroundcolorspan() {
spannablestring spanstring = new spannablestring("背景色");
backgroundcolorspan span = new backgroundcolorspan(color.yellow);
spanstring.setspan(span, 0, 3, spannable.span_exclusive_exclusive);
tv.append(spanstring);
}

3) stylespan 字体样式:粗体、斜体等

private void setstylespan() { 
spannablestring spanstring = new spannablestring("粗体斜体"); 
stylespan span = new stylespan(typeface.bold_italic); 
spanstring.setspan(span, 0, 4, spannable.span_exclusive_exclusive); 
tv.append(spanstring); 
}

4) relativesizespan 相对大小

private void setrelativefontspan() { 
spannablestring spanstring = new spannablestring("字体相对大小"); 
spanstring.setspan(new relativesizespan(2.5f), 0, 6,spannable.span_inclusive_exclusive); 
tv.append(spanstring); 
}

5) typefacespan 文本字体

private void settypefacespan() { 
spannablestring spanstring = new spannablestring("文本字体"); 
spanstring.setspan(new typefacespan("monospace"), 0, 4, spannable.span_exclusive_exclusive); 
tv.append(spantext); 
}

6) urlspan 文本超链接

private void addurlspan() { 
spannablestring spanstring = new spannablestring("超链接"); 
urlspan span = new urlspan("http://www.baidu.com"); 
spanstring.setspan(span, 0, 3, spannable.span_exclusive_exclusive); 
tv.append(spanstring); 
}

7) imagespan 图片

private void addimagespan() { 
spannablestring spanstring = new spannablestring(" "); 
drawable d = getresources().getdrawable(r.drawable.ic_launcher); 
d.setbounds(0, 0, d.getintrinsicwidth(), d.getintrinsicheight()); 
imagespan span = new imagespan(d, imagespan.align_baseline); 
spanstring.setspan(span, 0, 1, spannable.span_exclusive_exclusive); 
tv.append(spanstring); 
}

8) clickablespan 文本有点击事件

private textview textview; 
textview = (textview)this.findviewbyid(r.id.textview); 
string text = "显示activity"; 
spannablestring spannablestring = new spannablestring(text); 
spannablestring.setspan(new clickablespan() { 
@override 
public void onclick(view widget) { 
intent intent = new intent(main.this,otheractivity.class); 
startactivity(intent); 
} 
// 表示点击整个text的长度都有效触发这个事件 
}, 0, text.length(), spanned.span_exclusive_exclusive); 
textview.settext(spannablestring); 
textview.setmovementmethod(linkmovementmethod.getinstance());

9) underlinespan 下划线

private void addunderlinespan() { 
spannablestring spanstring = new spannablestring("下划线"); 
underlinespan span = new underlinespan(); 
spanstring.setspan(span, 0, 3, spannable.span_exclusive_exclusive); 
tv.append(spanstring); 
}

10) strikethroughspan

删除线

private void addstrikespan() { 
spannablestring spanstring = new spannablestring("删除线"); 
strikethroughspan span = new strikethroughspan(); 
spanstring.setspan(span, 0, 3, spannable.span_exclusive_exclusive); 
tv.append(spanstring); 
}

11) suggestionspan

相当于占位符

12) maskfilterspan

修饰效果,如模糊(blurmaskfilter)、浮雕(embossmaskfilter)

13) rasterizerspan

光栅效果

14) absolutesizespan

绝对大小(文本字体)

private void setabsolutefontspan() { 
spannablestring spannablestring = new spannablestring("40号字体"); 
absolutesizespan absolutesizespan = new absolutesizespan(40); 
spannablestring.setspan(absolutesizespan, 0, 5, spanned.span_exclusive_exclusive); 
edittext.append(spannablestring); 
}

15) dynamicdrawablespan 设置图片,基于文本基线或底部对齐。

16) textappearancespan

文本外貌(包括字体、大小、样式和颜色)

private void settextappearancespan() { 
spannablestring spanstring = new spannablestring("文本外貌"); 
textappearancespan textappearancespan = new textappearancespan(this, android.r.style.textappearance_medium); 
spanstring.setspan(textappearancespan, 0, 4, spannable.span_exclusive_exclusive); 
tv.append(spanstring); 
}

好了,通过以上的复习知识点,现在我们就可以来真正开始demo的实现,然后我们一起来一步一步封装我们的倒计时。

一、编写一个mikyoucountdowntimer基类,让它去继承countdowntimer类,并且公布出initspandata和setbackgroundspan方法用于其他样式倒计时的子类使用,它可以实现最基本倒计时的功能。

package com.mikyou.countdowntimer.bean; 
import android.content.context; 
import android.os.countdowntimer; 
import android.text.style.foregroundcolorspan; 
import android.widget.textview; 
import com.mikyou.countdowntimer.myview.mikyoubackgroundspan; 
import com.mikyou.countdowntimer.utils.timerutils; 
import org.apache.commons.lang.time.durationformatutils; 
import java.util.arraylist; 
import java.util.list; 
/** 
* created by mikyou on 16-10-22. 
*/ 
public class mikyoucountdowntimer extends countdowntimer{ 
private context mcontext;//传入的上下文对象 
protected textview mdatetv;//一个textview实现倒计时 
private long mgaptime;//传入设置的时间间隔即倒计时的总时长 
private long mcount = 1000;//倒计时的步长 一般为1000代表每隔1s跳一次 
private string mtimepattern = "hh:mm:ss";//timepattern 传入的时间的样式 如: hh:mm:ss hh时mm分ss秒 dd天hh时mm分ss秒 
private string mtimestr; 
protected list<mikyoubackgroundspan> mbackspanlist; 
protected list<foregroundcolorspan> mtextcolorspanlist; 
private int mdrawableid; 
private boolean flag = false;//设置标记flag,用于控制使得初始化span的数据一次 
protected string[] numbers;//此数组用于保存每个倒计时字符拆分后的天,时,分,秒的数值 
protected char[] nonnumbers;//保存了天,时,分,秒之间的间隔("天","时","分","秒"或者":") 
//用于倒计时样式的内间距,字体大小,字体颜色,倒计时间隔的颜色 
private int mspanpaddingleft,mspanpaddingright,mspanpaddingtop,mspanpaddingbottom; 
private int mspantextsize; 
private int mspantextcolor; 
protected int mgapspancolor; 
public mikyoucountdowntimer(context mcontext, long mgaptime, string mtimepattern,int mdrawableid) { 
this(mcontext,mgaptime,1000,mtimepattern,mdrawableid); 
} 
public mikyoucountdowntimer(context mcontext, long mgaptime, int mcount, string mtimepattern,int mdrawableid) { 
super(mgaptime,mcount); 
this.mcontext = mcontext; 
this.mgaptime = mgaptime;//倒计时总时长 
this.mcount = mcount;//每次倒计时的步长,默认是1000 
this.mdrawableid= mdrawableid;//用于设置背景的drawable的id 
this.mtimepattern = mtimepattern;//时间的格式:如hh:mm:ss或者dd天hh时mm分ss秒等 
mbackspanlist = new arraylist<>(); 
mtextcolorspanlist = new arraylist<>(); 
mdatetv = new textview(mcontext,null); 
} 
//公布这些设置倒计时样式的方法,供外部调用,从而灵活定制倒计时的样式 
public mikyoucountdowntimer settimertextsize(int textsize){ 
this.mspantextsize = textsize; 
return this; 
} 
public mikyoucountdowntimer settimerpadding(int left,int top,int right,int bottom){ 
this.mspanpaddingleft = left; 
this.mspanpaddingbottom = bottom; 
this.mspanpaddingright = right; 
this.mspanpaddingtop = top; 
return this; 
} 
public mikyoucountdowntimer settimertextcolor(int color){ 
this.mspantextcolor = color; 
return this; 
} 
public mikyoucountdowntimer settimergapcolor(int color){ 
this.mgapspancolor = color; 
return this; 
} 
//设置倒计时的span的样式,公布出给各个子类实现 
public void setbackgroundspan(string timestr) { 
if (!flag){ 
initspandata(timestr); 
flag = true; 
} 
mdatetv.settext(timestr); 
} 
//设置倒计时的span的数据,公布出给各个子类实现 
public void initspandata(string timestr) { 
numbers = timerutils.getnumintimerstr(timestr); 
nonnumbers = timerutils.getnonnumintimerstr(timestr); 
} 
protected void initbackspanstyle(mikyoubackgroundspan mbackspan) { 
mbackspan.settimerpadding(mspanpaddingleft,mspanpaddingtop,mspanpaddingright,mspanpaddingbottom); 
mbackspan.settimertextcolor(mspantextcolor); 
mbackspan.settimertextsize(mspantextsize); 
} 
@override 
public void ontick(long l) { 
if (l > 0) { 
mtimestr = durationformatutils.formatduration(l, mtimepattern); 
//这是apache中的common的lang包中durationformatutils类中的formatduration,通过传入 
//一个时间格式就会自动将倒计时转换成相应的mtimepattern的样式(hh:mm:ss或dd天hh时mm分ss秒) 
setbackgroundspan(mtimestr); 
} 
} 
@override 
public void onfinish() { 
mdatetv.settext("倒计时结束"); 
} 
//用于返回显示倒计时的textview的对象 
public textview getmdatetv() { 
starttimer(); 
return mdatetv; 
} 
public void canceltimer(){ 
this.cancel(); 
} 
public void starttimer(){ 
this.start(); 
} 
public string getmtimestr() { 
return mtimestr; 
} 
}

timerutils类用于保存不同倒计时的格式,例如hh:mm:ss、hh时mm分ss秒、dd天hh时mm分ss秒等。现在我们可以来看下简单的基本样式。

二、自定义mikyoubackgroundspan去继承imagespan,这个类非常重要是用于给倒计时的textview加样式,为什么可以使用一个textview来实现呢

别忘了还有个很强悍的类就是spannablestring类,这个类就是可以设置一段字符串中的每个字符的样式,很多样式。最后通过textview中有个setspan方法即可传入
一个spannablestring对象完成设置。但是为什么需要自定义一个span呢?这是因为很奇怪为什么android中的那么多span样式中没有一个可以直接设置一个drawable对象文件呢,所以上网找了很多都没有找到,最后在stackoverflow上找到了一个外国人给了一个解决办法,就是重写imagespan最后就可以实现了设置drawable文件即可

package com.mikyou.countdowntimer.myview; 
import android.graphics.canvas; 
import android.graphics.color; 
import android.graphics.paint; 
import android.graphics.rect; 
import android.graphics.drawable.drawable; 
import android.text.style.imagespan; 
/** 
* created by mikyou on 16-10-22. 
*/ 
public class mikyoubackgroundspan extends imagespan { 
private rect mtextbound; 
private int maxheight = 0; 
private int maxwidth = 0; 
private int mpaddingleft = 20; 
private int mpaddingright = 20; 
private int mpaddingtop = 20; 
private int mpaddingbottom = 20; 
private int mtextcolor = color.green; 
private int mtextsize = 50; 
public mikyoubackgroundspan(drawable d, int verticalalignment) { 
super(d, verticalalignment); 
mtextbound = new rect(); 
} 
public mikyoubackgroundspan settimertextcolor(int mtextcolor) { 
this.mtextcolor = mtextcolor; 
return this; 
} 
public mikyoubackgroundspan settimertextsize(int textsize){ 
this.mtextsize = textsize; 
return this; 
} 
public mikyoubackgroundspan settimerpadding(int left,int top,int right,int bottom){ 
this.mpaddingleft = left; 
this.mpaddingright = right; 
this.mpaddingbottom = bottom; 
this.mpaddingtop = top; 
return this; 
} 
@override 
public void draw(canvas canvas, charsequence text, int start, int end, float x, int top, int y, int bottom, paint paint) { 
//绘制文本的内容的背景 
paint.settextsize(mtextsize); 
//测量文本的宽度和高度,通过mtextbound得到 
paint.gettextbounds(text.tostring(), start, end, mtextbound); 
//设置文本背景的宽度和高度,传入的是left,top,right,bottom四个参数 
maxwidth = maxwidth < mtextbound.width() ? mtextbound.width() : maxwidth; 
maxheight = maxheight < mtextbound.height() ? mtextbound.height() : maxheight; 
//设置最大宽度和最大高度是为了防止在倒计时在数字切换的过程中会重绘,会导致倒计时边框的宽度和高度会抖动, 
// 所以每次取得最大的高度和宽度而不是每次都去取测量的高度和宽度 
getdrawable().setbounds(0,0, maxwidth+mpaddingleft+mpaddingright,mpaddingtop+mpaddingbottom+maxheight); 
//绘制文本背景 
super.draw(canvas, text, start, end, x, top, y, bottom, paint); 
//设置文本的颜色 
paint.setcolor(mtextcolor); 
//设置字体的大小 
paint.settextsize(mtextsize); 
int mgapx = (getdrawable().getbounds().width() - maxwidth)/2; 
int mgapy= (getdrawable().getbounds().height() - maxheight)/2; 
//绘制文本内容 
canvas.drawtext(text.subsequence(start, end).tostring(), x + mgapx , y - mgapy + maxheight/3, paint); } 
}

三、样式一的倒计时实现,样式一指的是例如:12时36分27秒或者12:36:27就是将数值和时、分、秒或者":"分隔开,然后去自定义每块数值(12 36 27)和间隔(时 分 秒 或 :)的样式,包括给数值块加背景和边框。在mikyoucountdowntimer中的number数组中保存着[12 36 27]而nonumer数组中保存着[时 分 秒 ]或[ : :]d的间隔字符。

package com.mikyou.countdowntimer.bean; 
import android.content.context; 
import android.text.spannablestring; 
import android.text.method.linkmovementmethod; 
import android.text.style.foregroundcolorspan; 
import android.text.style.imagespan; 
import com.mikyou.countdowntimer.myview.mikyoubackgroundspan; 
import com.mikyou.countdowntimer.utils.timerutils; 
/** 
* created by mikyou on 16-10-22. 
*/ 
public class jdcountdowntimer extends mikyoucountdowntimer { 
private spannablestring mspan; 
private context mcontext; 
private int mdrawableid; 
public jdcountdowntimer(context mcontext, long mgaptime, string mtimepattern,int mdrawableid) { 
super(mcontext, mgaptime, mtimepattern,mdrawableid); 
this.mcontext = mcontext; 
this.mdrawableid = mdrawableid; 
} 
/** 
* 重写父类的initspandata方法 
* 通过number数组得到每块数值对应的自定义mikyoubackgroundspan对象 
* 然后通过mikyoubackgroundspan对象定义每块数值的样式包括背景,边框,边框圆角样式,然后将这些对象加入到集合中去 
* 通过nonnumber数组得到每个间隔的foregroundcolorspan对象 
* 然后通过这些对象就可以定义每个间隔块的样式,因为只定义了foregroundcolorspan所以只能定义 
* 每个间隔块的字体颜色,setmgapspancolor方式也是供外部自由定制每个间隔的样式 
* 实际上还可以定义其他的span,同理实现也是很简单的。 
* */ 
@override 
public void initspandata(string timestr) { 
super.initspandata(timestr); 
for (int i = 0; i<numbers.length;i++){ 
mikyoubackgroundspan mbackspan = new mikyoubackgroundspan(mcontext.getdrawable(mdrawableid), imagespan.align_bottom); 
initbackspanstyle(mbackspan); 
mbackspanlist.add(mbackspan); 
} 
for (int i= 0; i<nonnumbers.length;i++){ 
foregroundcolorspan mgapspan = new foregroundcolorspan(mgapspancolor); 
mtextcolorspanlist.add(mgapspan); 
} 
} 
/** 重写父类的setbackgroundspan方法 
* 我们知道设置span的样式主要是控制两个变量start,end索引 
* 以确定设置start到end位置的字符串的子串的样式 
* mgaplen = 1,表示一个间隔块的长度, 
* 例如:12时36分27秒的"时","分","秒"的间隔长度 
* 所以通过遍历span集合,给字符串设置span, 
* 通过分析不难得出每个数值块的span的start索引:start = i*numbers[i].length() + i*mgaplen; 
* end = start + numbers[i].length(); 
* */ 
@override 
public void setbackgroundspan(string timestr) { 
super.setbackgroundspan(timestr); 
int mgaplen = 1; 
mspan = new spannablestring(timestr); 
for (int i = 0;i<mbackspanlist.size();i++){ 
int start = i*numbers[i].length() + i*mgaplen; 
int end = start + numbers[i].length(); 
timerutils.setcontentspan(mspan,mbackspanlist.get(i),start,end); 
if (i < mtextcolorspanlist.size()){//这里为了就是防止12:36:27这种样式,这种样式间隔只有2个所以需要做判断,防止数组越界 
timerutils.setcontentspan(mspan,mtextcolorspanlist.get(i),end,end + mgaplen); 
} 
} 
mdatetv.setmovementmethod(linkmovementmethod.getinstance());//此方法很重要需要调用,否则绘制出来的倒计时就是重叠的样式 
mdatetv.settext(mspan); 
} 
}

四、样式二的倒计时实现,样式二不同于样式一的是例如:12时36分27秒或者12:36:27就是将每个数值和时、分、秒或者":"分隔开,然后去自定义每块数值(1 2 3 6 2 7)和间隔(时 分 秒 或 :)的样式,包括给数值块加背景和边框。在mikyoucountdowntimer中的vipnumber数组中保存着[1 2 3 6 2 7]而vipnonnumer数组中保存着[时 分 秒 ]或[ : :]d的间隔字符。

package com.mikyou.countdowntimer.bean; 
import android.content.context; 
import android.text.spannablestring; 
import android.text.method.linkmovementmethod; 
import android.text.style.foregroundcolorspan; 
import android.text.style.imagespan; 
import com.mikyou.countdowntimer.myview.mikyoubackgroundspan; 
import com.mikyou.countdowntimer.utils.timerutils; 
import java.util.arraylist; 
import java.util.list; 
/** 
* created by mikyou on 16-10-22. 
*/ 
public class vipcountdowntimer extends mikyoucountdowntimer { 
private spannablestring mspan; 
private context mcontext; 
private int mdrawableid; 
private list<mikyoubackgroundspan> mspanlist; 
private string[] vipnumbers; 
private char[] vipnonnumbers; 
public vipcountdowntimer(context mcontext, long mgaptime, string mtimepattern,int mdrawableid) { 
super(mcontext, mgaptime, mtimepattern,mdrawableid); 
this.mcontext = mcontext; 
this.mdrawableid = mdrawableid; 
mspanlist = new arraylist<>(); 
} 
/** 重写父类的setbackgroundspan方法 
* 我们知道设置span的样式主要是控制两个变量start,end索引 
* 以确定设置start到end位置的字符串的子串的样式,表示每个数字子串在整个字符串中的位置范围 
* mgaplen = 1,表示一个间隔块的长度, 
* 例如:12时36分27秒的"时","分","秒"的间隔长度 
* 所以通过遍历span集合,给字符串设置span, 
* 通过分析不难得出每个数值块的span的start索引:start = i*numbers[i].length() + i*mgaplen; 
* end = start + numbers[i].length(); 
* */ 
@override 
public void setbackgroundspan(string timestr) { 
int mgaplen = 1; 
mspan = new spannablestring(timestr); 
initspandata(timestr); 
int start = 0 ; 
int count =0; 
for (int i=0;i<vipnumbers.length;i++){ 
for (int j=start;j<start + vipnumbers[i].tochararray().length;j++,count++){ 
timerutils.setcontentspan(mspan,mspanlist.get(count),j,j+mgaplen); 
} 
//此时表示遍历完了某一块的数值,从而需要将此时该块数值去更新start变量 
start = start + vipnumbers[i].tochararray().length; 
if (i < nonnumbers.length){ 
timerutils.setcontentspan(mspan,mtextcolorspanlist.get(i),start,start+mgaplen); 
start = start +mgaplen;//如果是个间隔还得去加上每个间隔长度最后去更新start变量 
} 
} 
mdatetv.setmovementmethod(linkmovementmethod.getinstance()); 
mdatetv.settext(mspan); 
} 
/** 
* 重写父类的initspandata方法 
* 通过number数组得到每块数值对应的自定义mikyoubackgroundspan对象 
* 然后通过mikyoubackgroundspan对象定义每块数值的样式包括背景,边框,边框圆角样式,然后将这些对象加入到集合中去 
* 通过nonnumber数组得到每个间隔的foregroundcolorspan对象 
* 然后通过这些对象就可以定义每个间隔块的样式,因为只定义了foregroundcolorspan所以只能定义 
* 每个间隔块的字体颜色,setmgapspancolor方式也是供外部自由定制每个间隔的样式 
* 实际上还可以定义其他的span,同理实现也是很简单的。 
* */ 
@override 
public void initspandata(string timestr) { 
super.initspandata(timestr); 
vipnumbers = timerutils.getnumintimerstr(timestr);//得到每个数字注意不是每块数值,并加入数组 
vipnonnumbers = timerutils.getnonnumintimerstr(timestr);//得到每个间隔字符,并加入到数组 
for (int i=0;i<vipnumbers.length;i++){ 
for (int j=0;j<vipnumbers[i].tochararray().length;j++){//因为需要得到每个数字所以还得遍历每块数值中的每个数字,所以需要二层循环 
mikyoubackgroundspan mspan = new mikyoubackgroundspan(mcontext.getdrawable(mdrawableid), imagespan.align_bottom); 
initbackspanstyle(mspan); 
mspanlist.add(mspan); 
} 
} 
for (int i= 0; i<vipnonnumbers.length;i++){ 
foregroundcolorspan mgapspan = new foregroundcolorspan(mgapspancolor); 
mtextcolorspanlist.add(mgapspan); 
} 
} 
}

四、timerutils管理类,主要是提供不同样式的倒计时的对象给客户端,所以这个类直接与客户端建立关系,从而实现倒计时子类和基类对外界的隐藏体现了封装性。

package com.mikyou.countdowntimer.utils; 
import android.content.context; 
import android.graphics.color; 
import android.text.spannablestring; 
import android.text.spanned; 
import android.text.style.foregroundcolorspan; 
import com.mikyou.countdowntimer.bean.jdcountdowntimer; 
import com.mikyou.countdowntimer.bean.mikyoucountdowntimer; 
import com.mikyou.countdowntimer.bean.vipcountdowntimer; 
/** 
* created by mikyou on 16-10-22. 
*/ 
public class timerutils { 
public static final int jd_style = 0; 
public static final int vip_style = 1; 
public static final int default_style = 3; 
public static final string time_style_one = "hh:mm:ss"; 
public static final string time_style_two = "hh时mm分ss秒"; 
public static final string time_style_three = "dd天hh时mm分ss秒"; 
public static final string time_style_four = "dd天hh时mm分"; 
public static mikyoucountdowntimer gettimer(int style,context mcontext, long mgaptime, string mtimepattern, int mdrawableid){ 
mikyoucountdowntimer mcountdowntimer = null; 
switch (style){ 
case jd_style: 
mcountdowntimer = new jdcountdowntimer(mcontext,mgaptime,mtimepattern,mdrawableid); 
break; 
case vip_style: 
mcountdowntimer = new vipcountdowntimer(mcontext,mgaptime,mtimepattern,mdrawableid); 
break; 
case default_style: 
mcountdowntimer = new mikyoucountdowntimer(mcontext,mgaptime,mtimepattern,mdrawableid); 
break; 
} 
return mcountdowntimer; 
} 
//得到倒计时字符串中的数值块部分 
public static string[] getnumintimerstr(string mtimerstr){ 
return mtimerstr.split("[^\\d]"); 
} 
//得到倒计时中字符串中的非数值的字符串,并把数值过滤掉重新组合成一个字符串,并把字符串拆分字符数组,也就是保存倒计时中间的间隔 
public static char[] getnonnumintimerstr(string mtimerstr){ 
return mtimerstr.replaceall("\\d","").tochararray(); 
} 
//设置字体颜色 
public static foregroundcolorspan gettextcolorspan(string color){ 
foregroundcolorspan mspan = null; 
if (mspan == null){ 
mspan = new foregroundcolorspan(color.parsecolor(color)); 
} 
return mspan; 
} 
//设置内容的span 
public static void setcontentspan(spannablestring mspan, object span, int start, 
int end) { 
mspan.setspan(span, start, end, spanned.span_exclusive_exclusive); 
} 
}

现在我们就来测试下我们使用一个textview实现的倒计时。

使用该倒计时非常简单非常方便只需要一行代码就能实现一个高仿京东和各种电商的app的倒计时样式。

package com.mikyou.countdowntimer; 
import android.graphics.color; 
import android.os.bundle; 
import android.support.v7.app.appcompatactivity; 
import android.view.gravity; 
import android.widget.linearlayout; 
import android.widget.textview; 
import com.mikyou.countdowntimer.utils.timerutils; 
public class mainactivity extends appcompatactivity { 
private linearlayout parent; 
private int padding =10; 
private int textsize = 40; 
@override 
protected void oncreate(bundle savedinstancestate) { 
super.oncreate(savedinstancestate); 
setcontentview(r.layout.activity_main); 
parent = (linearlayout) findviewbyid(r.id.parent); 
//默认样式倒计时每种样式下又对应四种时间的格式 
/** 
* 默认+时间格式1:default_style <--> time_style_one = "hh:mm:ss" 
* */ 
textview tv = timerutils.gettimer(timerutils.default_style,this,120000000,timerutils.time_style_one,0) 
.getmdatetv(); 
parent.addview(tv); 
setmlayoutparams(tv); 
/** 
* 默认+时间格式2:default_style <--> time_style_two = "hh时mm分ss秒" 
* */ 
textview tv1 = timerutils.gettimer(timerutils.default_style,this,120000000,timerutils.time_style_two,0) 
.getmdatetv(); 
parent.addview(tv1); 
setmlayoutparams(tv1); 
/** 
* 默认+时间格式3:default_style <--> time_style_three = "dd天hh时mm分ss秒" 
* */ 
textview tv2 = timerutils.gettimer(timerutils.default_style,this,120000000,timerutils.time_style_three,0) 
.getmdatetv(); 
parent.addview(tv2); 
setmlayoutparams(tv2); 
/** 
* 默认+时间格式4:default_style <--> time_style_four = "dd天hh时mm分" 
* */ 
textview tv3 = timerutils.gettimer(timerutils.default_style,this,120000000,timerutils.time_style_four,0) 
.getmdatetv(); 
parent.addview(tv3); 
setmlayoutparams(tv3); 
//样式一倒计时,就是每块数值和每个间隔分开的样式,每种样式下又对应四种时间的格式 
/** 
* 样式一+时间格式1:jd_style <--> time_style_one = "hh:mm:ss" 
* */ 
textview tv4= timerutils.gettimer(timerutils.jd_style,this,120000000,timerutils.time_style_one,r.drawable.timer_shape) 
.settimerpadding(10,10,10,10)//设置内间距 
.settimertextcolor(color.black)//设置字体颜色 
.settimertextsize(40)//设置字体大小 
.settimergapcolor(color.black)//设置间隔的颜色 
.getmdatetv();//拿到textview对象 
parent.addview(tv4); 
setmlayoutparams(tv4); 
/** 
* 样式一+时间格式2:jd_style <--> time_style_two = "hh时mm分ss秒" 
* */ 
textview tv5= timerutils.gettimer(timerutils.jd_style,this,120000000,timerutils.time_style_two,r.drawable.timer_shape2) 
.settimerpadding(10,10,10,10) 
.settimertextcolor(color.white) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv5); 
setmlayoutparams(tv5); 
/** 
* 样式一+时间格式3:jd_style <-->time_style_three = "dd天hh时mm分ss秒" 
* */ 
textview tv6= timerutils.gettimer(timerutils.jd_style,this,120000000,timerutils.time_style_three,r.drawable.timer_shape2) 
.settimerpadding(10,10,10,10) 
.settimertextcolor(color.yellow) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv6); 
setmlayoutparams(tv6); 
/** 
* 样式一+时间格式4:jd_style <-->time_style_four = "dd天hh时mm分" 
* */ 
textview tv7= timerutils.gettimer(timerutils.jd_style,this,120000000,timerutils.time_style_four,r.drawable.timer_shape2) 
.settimerpadding(15,15,15,15) 
.settimertextcolor(color.blue) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv7); 
setmlayoutparams(tv7); 
/** 
* 样式二+时间格式1:vip_style <-->time_style_one = "hh:mm:ss" 
* */ 
textview tv8= timerutils.gettimer(timerutils.vip_style,this,120000000,timerutils.time_style_one,r.drawable.timer_shape) 
.settimerpadding(15,15,15,15) 
.settimertextcolor(color.black) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv8); 
setmlayoutparams(tv8); 
/** 
* 样式二+时间格式2:vip_style <-->time_style_two = "hh时mm分ss秒" 
* */ 
textview tv9= timerutils.gettimer(timerutils.vip_style,this,120000000,timerutils.time_style_two,r.drawable.timer_shape2) 
.settimerpadding(15,15,15,15) 
.settimertextcolor(color.white) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv9); 
setmlayoutparams(tv9); 
/** 
* 样式二+时间格式3:vip_style <-->time_style_three = "dd天hh时mm分ss秒" 
* */ 
textview tv10= timerutils.gettimer(timerutils.vip_style,this,120000000,timerutils.time_style_three,r.drawable.timer_shape2) 
.settimerpadding(15,15,15,15) 
.settimertextcolor(color.yellow) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv10); 
setmlayoutparams(tv10); 
/** 
* 样式二+时间格式4:vip_style <-->time_style_four = "dd天hh时mm分" 
* */ 
textview tv11= timerutils.gettimer(timerutils.vip_style,this,120000000,timerutils.time_style_four,r.drawable.timer_shape2) 
.settimerpadding(15,15,15,15) 
.settimertextcolor(color.blue) 
.settimertextsize(40) 
.settimergapcolor(color.black) 
.getmdatetv(); 
parent.addview(tv11); 
setmlayoutparams(tv11); 
} 
private void setmlayoutparams(textview tv) { 
tv.setgravity(gravity.center_horizontal); 
linearlayout.layoutparams params = (linearlayout.layoutparams) tv.getlayoutparams(); 
params.setmargins(20,20,20,20); 
tv.setlayoutparams(params); 
} 
}

两个drawable文件:

带边框样式

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" 
> 
<corners android:radius="5px"/> 
<stroke android:color="#88000000" android:width="1dp"/> 
</shape>

带背景和边框样式

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" 
> 
<corners android:radius="10px"/> 
<solid android:color="#000000"/> 
</shape>

现在就看看我们运行的成果吧。

看看运行结果还不错吧,其实它的样式还可以定义很多种主要看自己的创意和想法了,这个倒计时封装如果还有什么不足之处,请多多提出建议。但是现在使用还是蛮方便和简单的,一行代码就能就能解决。这个倒计时用到的地方还是蛮多的,大家有需要的话可以直接引入到自己的项目中。

以上所述是小编给大家介绍的android中使用textview实现高仿京东淘宝各种倒计时效果,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网