文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

android自定义倒计时控件示例

2022-06-06 10:28

关注


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

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>

您可能感兴趣的文章:android自定义圆形倒计时显示控件Android自定义圆环倒计时控件Android倒计时控件 Splash界面5秒自动跳转Android倒计时的开始与停止 剩余时分秒的展示Android仿Keep运动休息倒计时圆形控件android实现倒计时功能代码Android实现计时与倒计时的常用方法小结Android实现加载广告图片和倒计时的开屏布局Android 实现闪屏页和右上角的倒计时跳转实例代码android实现倒计时功能(开始、暂停、0秒结束)


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-移动开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯