android自定义倒计时控件示例

android自定义倒计时控件示例,第1张

概述自定义TextView控件TimeTextView代码:复制代码代码如下:importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Paint;importandroid.text.Html;importandroid.util.Attrib


自定义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 >"+mday+"</span>
<pre>"+"天</pre>
<span >"+mhour+"</span>
<pre>小时</pre>
<span >"+
 mmin+"</span>
<pre>分钟</pre>
<span >"+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>

总结

以上是内存溢出为你收集整理的android自定义倒计时控件示例全部内容,希望文章能够帮你解决android自定义倒计时控件示例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142453.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存