当前位置: 移动技术网 > IT编程>移动开发>Android > android自定义倒计时控件示例

android自定义倒计时控件示例

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

柳市镇一中,百草影院,官眷这差事


自定义textview控件timetextview代码:

复制代码 代码如下:

import android.content.context;
import android.content.res.typedarray;
import android.graphics.paint;
import android.text.html;
import android.util.attributeset;
import android.widget.textview;

import com.new0315.r;
/**
 * 自定义倒计时文本控件
 * @author administrator
 *
 */
public class timetextview extends textview implements runnable{

    paint mpaint; //画笔,包含了画几何图形、文本等的样式和颜色信息

    private long[] times;

    private long mday, mhour, mmin, msecond;//天,小时,分钟,秒

    private boolean run=false; //是否启动了

    public timetextview(context context, attributeset attrs) {
        super(context, attrs);
        mpaint=new paint();
        typedarray array = context.obtainstyledattributes(attrs, r.styleable.timetextview);

        array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
    }

    public timetextview(context context, attributeset attrs, int defstyle) {
        super(context, attrs, defstyle);
        mpaint=new paint();
        typedarray array = context.obtainstyledattributes(attrs, r.styleable.timetextview);

        array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响
    }

    public timetextview(context context) {
        super(context);
    }

    public long[] gettimes() {
        return times;
    }

    public void settimes(long[] times) {
        this.times = times;
        mday = times[0];
        mhour = times[1];
        mmin = times[2];
        msecond = times[3];

    }

    /**
     * 倒计时计算
     */
    private void computetime() {
        msecond--;
        if (msecond < 0) {
            mmin--;
            msecond = 59;
            if (mmin < 0) {
                mmin = 59;
                mhour--;
                if (mhour < 0) {
                    // 倒计时结束
                    mhour = 59;
                    mday--;

                }
            }

        }

    }

    public boolean isrun() {
        return run;
    }

    public void setrun(boolean run) {
        this.run = run;
    }

    @override
    public void run() {
        //标示已经启动
        run=true;

        computetime();

        string strtime="还剩</pre>
<span style="color: red;">"+mday+"</span>
<pre>"+"天</pre>
<span style="color: red;">"+mhour+"</span>
<pre>小时</pre>
<span style="color: red;">"+
 mmin+"</span>
<pre>分钟</pre>
<span style="color: red;">"+msecond+"</span>
<pre>秒";
        this.settext(html.fromhtml(strtime));

        postdelayed(this, 1000);

    }

}

属性atts.xml

复制代码 代码如下:

<declare-styleable name="timetextview">
</declare-styleable>

adapter调用代码:

复制代码 代码如下:

import java.text.decimalformat;
import java.util.list;

import android.content.context;
import android.graphics.paint;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
import android.widget.linearlayout;
import android.widget.textview;

import com.new0315.r;
import com.new0315.entity.specialgoods;
import com.new0315.utils.correctspecialdataformhttp;
import com.new0315.utils.datetools;
import com.new0315.widgets.timetextview;
import com.nostra13.universalimageloader.core.imageloader;

public class specialgoodsadapter extends baseadapter {

    private context context;
    private list list;
    private long sumtime;

    public specialgoodsadapter(context context) {

        this.context = context;
    }

    public void setlist(list list) {
        this.list = list;
    }

    @override
    public int getcount() {
        // todo auto-generated method stub
        return list.size();
    }

    @override
    public object getitem(int arg0) {
        // todo auto-generated method stub
        return null;
    }

    @override
    public long getitemid(int arg0) {
        // todo auto-generated method stub
        return 0;
    }

    @override
    public view getview(int arg0, view convertview, viewgroup arg2) {
        //开始计时,性能测试用nanotime会更精确,因为它是纳秒级的
        long starttime = system.nanotime();
        log.d("position","getview " + arg0 + " " + convertview);
        viewholder viewholder;
        if(convertview == null)
        {
            convertview = layoutinflater.from(context).inflate(
                    r.layout.item_temai_list, null);
            viewholder = new viewholder();
            viewholder.goodname = (textview) convertview
                    .findviewbyid(r.id.temai_name);
            viewholder.price = (textview) convertview
                    .findviewbyid(r.id.temai_yuanjia_text);

            viewholder.specialprice = (textview) convertview
                    .findviewbyid(r.id.temai_xiajia_text);
            //特卖倒计时控件
            viewholder.mtimetext = (timetextview) convertview
                    .findviewbyid(r.id.temai_timetextview);

            viewholder.showdate = (textview) convertview
                    .findviewbyid(r.id.index_temai_day);
            viewholder.showdate_l = (linearlayout) convertview
                    .findviewbyid(r.id.temai_weikaishi);
            viewholder.showtime = (linearlayout) convertview
                    .findviewbyid(r.id.temai_yikaishi);
            viewholder.koukou = (textview) convertview
                    .findviewbyid(r.id.temai_zhekou_text);
            viewholder.image = (imageview) convertview
                    .findviewbyid(r.id.index_temai_image);
            log.d("googleio","new position:"+viewholder.goodname.gettext());

            convertview.settag(viewholder);

        }else {
            viewholder = (viewholder) convertview.gettag();
            resetviewholder(viewholder);
        }
        //setdata
        string off = getoff(list.get(arg0).getgoods_price(), list.get(arg0)
                .getgoods_specialprice());
        viewholder.goodname.settext(list.get(arg0).getgoods_name());
        viewholder.price.settext(list.get(arg0).getgoods_price());
        viewholder.price.getpaint().setflags(
                paint.strike_thru_text_flag | paint.anti_alias_flag);
        viewholder.specialprice.settext(list.get(arg0).getgoods_specialprice());
        viewholder.koukou.settext(off + "折");

        if (datetools.isstart(list.get(arg0).getspecialfrom())) {
            //特卖倒计时开始
            viewholder.mtimetext.settimes(datetools.getdate(correctspecialdataformhttp
                    .correctdata((list.get(arg0).getspecialend()))));
            //已经在倒计时的时候不再开启计时
            if(!viewholder.mtimetext.isrun())
            {
                viewholder.mtimetext.run();
            }
            viewholder.showdate_l.setvisibility(view.gone);
            viewholder.showtime.setvisibility(view.visible);
        } else {
            viewholder.showtime.setvisibility(view.gone);
            viewholder.showdate_l.setvisibility(view.visible);
            viewholder.showdate.settext(datetools.getday(list.get(arg0).getspecialfrom())
                    + "");
        }

        imageloader.getinstance().displayimage(list.get(arg0).getgoods_pic(),viewholder.image);

        //停止计时
        long endtime = system.nanotime();
        //耗时
        long spendtime = (endtime - starttime);

        sumtime += spendtime;
//        log.d("googleio", "position at:"+arg0+"--sumtime:"+string.valueof(sumtime));
        return convertview;
    }

    public string getoff(string price, string specialprice) {

        double off = double.parsedouble(specialprice)
                / double.parsedouble(price) * 10;

        decimalformat df = new decimalformat("0.0");
        string off_string = df.format(off);

        if (off_string.equals("nan") || off_string.equals("1")) {
            off_string = "10";
        }
        return off_string;
    }

    static class viewholder {
        imageview image;
        textview goodname;
        textview price;
        textview specialprice;
        textview koukou;
        timetextview mtimetext;
        textview showdate;
        linearlayout showdate_l;
        linearlayout showtime;

    }

    protected void resetviewholder(viewholder viewholder) {
        viewholder.image.setimagebitmap(null);
        viewholder.goodname.settext("");
        viewholder.price.settext("");
        viewholder.specialprice.settext("");
        viewholder.koukou.settext("");
        viewholder.mtimetext.settext("");
        viewholder.showdate.settext("");

    }
}

layout使用代码

复制代码 代码如下:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/home_panicbuying_background"
android:orientation="vertical" >

<!-- 免单 -->

<relativelayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margintop="5dp" >

<framelayout
android:id="@+id/index_temai_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centervertical="true"
android:layout_margin="5dp" >

<imageview
android:id="@+id/index_temai_image"
android:layout_width="80dp"
android:layout_height="80dp" />

<imageview
android:id="@+id/index_temai_discount_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|left"
android:background="@drawable/app_limit_buy_sale"
android:src="@drawable/app_limit_buy_begin" />
</framelayout>

<linearlayout
android:id="@+id/temai_date_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_torightof="@id/index_temai_image_layout"
android:orientation="vertical" >

<relativelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<linearlayout
android:id="@+id/temai_weikaishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="2dp"
android:orientation="horizontal" >

<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="距离开始还有"
android:textcolor="@color/black"
android:textsize="@dimen/small_text_size"
android:textstyle="bold" />

<textview
android:id="@+id/index_temai_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="99"
android:textcolor="@color/red"
android:textsize="@dimen/small_text_size"
android:textstyle="bold" />

<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="天"
android:textcolor="@color/black"
android:textsize="@dimen/small_text_size"
android:textstyle="bold" />
</linearlayout>

<linearlayout
android:id="@+id/temai_yikaishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margintop="2dp"
android:orientation="horizontal" >

<com.new0315.widgets.timetextview
android:id="@+id/temai_timetextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textcolor="@android:color/black"
android:textsize="@dimen/small_text_size"
/>

</linearlayout>
</relativelayout>

<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginbottom="5dp"
android:layout_marginright="20dp"
android:layout_margintop="5dp"
android:orientation="horizontal" >

<textview
android:id="@+id/temai_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="2"
android:text="大众甲壳虫,豪华款,曾全套汽车配件,十年加油卡,车库补贴,十年车险,五年以旧换新服务,比提供五年免费待架服务"
android:textcolor="@color/black"
android:textsize="12sp" />
</linearlayout>

<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/index_raw_price"
android:textcolor="@color/darkgray"
android:textsize="@dimen/small_text_size" />

<textview
android:id="@+id/temai_yuanjia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="5dp"
android:textcolor="@color/darkgray"
android:textsize="@dimen/small_text_size" />
</linearlayout>
</linearlayout>
</relativelayout>

<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:background="@drawable/app_limit_buy_sale_bg"
android:gravity="center_vertical" >

<linearlayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="30dp"
android:layout_margintop="3dp"
android:orientation="horizontal" >

<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="特卖价:"
android:textcolor="#919263"
android:textsize="13sp" />

<textview
android:id="@+id/temai_xiajia_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="5dp"
android:layout_marginright="5sp"
android:text="¥400"
android:textcolor="@color/red"
android:textsize="13sp" />

<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="折扣:"
android:textcolor="#919263"
android:textsize="13sp" />

<textview
android:id="@+id/temai_zhekou_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="5dp"
android:layout_marginright="5sp"
android:text="5.0折"
android:textcolor="@color/green"
android:textsize="13sp" />
</linearlayout>
</linearlayout>

</linearlayout>

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

相关文章:

验证码:
移动技术网