android自定义倒计时控件示例

这篇文章主要介绍了Android秒杀倒计时自定义TextView示例,大家参考使用吧

自定义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-08-24 07:27:35

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

Android自定义倒计时控件

序: 最近越来越多的APP都是用手机号注册,一是为了方便用户记忆,二是为了增加用户账户的安全性.在我们进行交易操作或者修改密码等操作的时候,这时候需要输入短信验证码.这个控件会需要有倒计时的功能,这里主要总结常见的几种实现方式. 1.Android中实现倒计时的方法 第一种:直接用Handler的消息机制来实现 这种方式感觉是最原始的,这里不多说. 第二种:Timer和TimerTask 基本使用:获得Timer和TimerTask对象,然后启动,倒计时的逻辑写在handler里面 privat

Android 自定义组合控件小结

引言 接触Android UI开发的这段时间以来,对自定义组合控件有了一定的了解,为此小结一下,本文小结内容主要讨论的是如何使用Android SDK提供的布局和控件组成一个功能完整组合控件并将其封装为面向对象的类,而并非讨论如何继承自SDK提供的控件类(比如TextView),对其进行自定义扩展的问题. 进入正题前,我们先来看一组功能需求 假设在手机需求上,那么如上三个界面我们可以使用三个Activity,每个Activity一个布局文件,实现起来比较独立,但是假设在Android pad上要

Android自定义用户控件简单范例(一)

一款优秀的移动应用需要具有自己独特统一的风格,通常情况下UI设计师会根据产品需求和使用人群的特点,设计整体的风格,界面的元素和控件的互效果.而原生态的Android控件为开发人员提供的是最基本的积木元素,如果要准确地传递统一的视觉效果和交互体验,对控件的自定义使用是非常有必要的. 这篇文章通过一个简单的从Java后台程序中进行创建的示例来说明Android自定义控件的运行原理. <LinearLayout xmlns:android="http://schemas.android.com/

android中倒计时控件CountDownTimer分析

android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { LogUtil.i(TAG, "seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { LogUtil.i(TAG, "done!"

Android自定义用户控件简单范例(二)

对于完全由后台定制的控件,并不是很方便其他人的使用,因为我们常常需要看到控件放到xml界面上的效果,并根据效果进行布局的调整,这就需要一个更加标准的控件制作流程: 我们的自定义控件和其他的控件一样,应该写成一个类,而这个类的属性是是有自己来决定的. 我们要在res/values目录下建立一个attrs.xml的文件,并在此文件中增加对控件的属性的定义. 使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量与attrs.xml中的属性连接起来. 在自定义控件类中使

Android自定义UI控件(简单方便版,但不灵活)

这种方法的优点就是简单,容易理解,适合开发一些不经常用到的自定义UI控件 缺点就是比较不灵活,如果其他应用想使用这个控件的话得改很多 简单来说,这个方法是用来做成品的,下一篇的方法是用来做模板的. 先看成品,这是一个标题栏控件: 由左右两个按钮和中一个TextView组成: 实现方法: 第一步:定义一个xml文件,用来设计你自定义控件的雏形 示例代码:文件名为title 1 <?xml version="1.0" encoding="utf-8"?> 2

Android 自定义View控件

一.简介 在自定义View时,我们通常会重写onDraw()方法来绘制View的显示内容.如果,该View还需要使用wrap_content属性,那么还必须重写onMeasure()方法.另外,通过自定义attrs属性,还可以设置新的属性配置值. 在View中通常有以下一些比较重要的回调方法: onFinisInflate():从XML加载组件后回调: onSizeChanged():组件大小改变时回调: onMeasure():回调该方法来进行测量: onLayout():回调该方法来确定显示

Android自定义组合控件--底部多按钮切换

效果图: 现在市场上大多数软件都是类似于上面的结构,底部有几个按钮用于切换到不同的界面.基于OOP思想,我想把下面的一整块布局封装成一个类,也就是我们的自定义组合控件-底部多按钮切换布局,我把它叫做BottomLayout 看上面的布局,几个按钮横向排列,我们先看一下布局 最外面LinearLayout 方向 horizontal,然后5个weight相同的RelativeLayout,每个RelativeLayout里面有一个Button(用了显示选中状态)个ImageView(用来显示红点)

android 自定义组合控件

自定义控件是一些android程序员感觉很难攻破的难点,起码对我来说是这样的,但是我们可以在网上找一些好的博客关于自定义控件好好拿过来学习研究下,多练,多写点也能找到感觉,把一些原理弄懂,今天就讲下自定义组合控件,这个特别适合在标题栏或者设置界面,看下面图: 就非常适合使用组合控件了,现在写一个玩玩: activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"