android自定义倒计时控件示例

自定义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>

时间: 2024-10-10 16:33:20

android自定义倒计时控件示例的相关文章

android自定义倒计时控件示例_Android

自定义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;

Android自定义组合控件之自定义下拉刷新和左滑删除实例代码_Android

绪论 最近项目里面用到了下拉刷新和左滑删除,网上找了找并没有可以用的,有比较好的左滑删除,但是并没有和下拉刷新上拉加载结合到一起,要不就是一些比较水的结合,并不能在项目里面使用,小编一着急自己组合了一个,做完了和QQ的对比了一下,并没有太大区别,今天分享给大家,其实并不难,但是不知道为什么网上没有比较好的Demo,当你的项目真的很急的时候,又没有比较好的Demo,那么"那条友谊的小船儿真是说翻就翻啊",好了,下面先来具体看一下实现后的效果吧:   代码已经上传到Github上了,小伙伴

Android自定义View控件实现刷新效果_Android

三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b. LayoutInflater localinflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); c. LayoutInflater inflater = LayoutInflater.from(context); onDraw 方法

Android 自定义Button控件实现按钮点击变色_Android

效果图如下所示: 一.shape 样式:(在drawable新建-->new-->Drawable resource file 在父级标签selector添加Item ) <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item and

Android自定义View控件实现刷新效果

三种得到LinearInflater的方法 a. LayoutInflater inflater = getLayoutInflater(); b. LayoutInflater localinflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); c. LayoutInflater inflater = LayoutInflater.from(context); onDraw 方法

Android 自定义Button控件实现按钮点击变色

效果图如下所示: 一.shape 样式:(在drawable新建-->new-->Drawable resource file 在父级标签selector添加Item ) <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item and

android倒计时控件示例_Android

本文为大家分享了android倒计时控件,供大家参考,具体代码如下 /* * Copyright (C) 2012 The * Project * All right reserved. * Version 1.00 2012-2-11 * Author veally@foxmail.com */ package com.ly.sxh.view; import android.content.Context; import android.database.ContentObserver; im

android倒计时控件示例

本文为大家分享了android倒计时控件,供大家参考,具体代码如下 /* * Copyright (C) 2012 The * Project * All right reserved. * Version 1.00 2012-2-11 * Author veally@foxmail.com */ package com.ly.sxh.view; import android.content.Context; import android.database.ContentObserver; im

Android自定义表格控件满足人们对视觉的需求_Android

Android平台已经给我们提供了很多标准的组件,如:TextView.EditView.Button.ImageView.Menu等,还有许多布局控件,常见的有:AbsoluteLayout.LinerLayout.RelativeLayout.TableLayout等.但随着人们对视觉的需求,基本组件已无法满足人们求新求异的要求,于是我们常常会自定义组件,用来实现更美观的UI界面. 实现自定义控件通常有两种途径,一种是继承View类,重写其中的重要方法,另一种是继承ViewGroup类,通过