首页 > 其他 > 详细

自定义控件----倒计时控件

时间:2015-03-13 16:38:19      阅读:486      评论:0      收藏:0      [点我收藏+]

先上效果图:
技术分享
因为时分秒都有自己的背景色等布局,所以重写一个textview 不够灵活,所以我们自定义一个TimeTextView继承自Linearlayout 然后再在里面放几个textview即可。
先看 布局文件吧:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginBottom="@dimen/default_margin"
              android:layout_marginLeft="@dimen/default_margin"
              android:layout_marginRight="@dimen/default_margin"
              android:background="@android:color/transparent"
              android:gravity="center"
              android:orientation="horizontal"
              android:padding="@dimen/default_margin_small">

    <TextView
        android:id="@+id/tv_hours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_btn_red"
        android:padding="@dimen/default_margin_small"
        android:text="24"
        android:textColor="@color/white"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":"
        android:textColor="@color/default_text_red"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:id="@+id/tv_minutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_btn_red"
        android:padding="@dimen/default_margin_small"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=":"
        android:textColor="@color/default_text_red"
        android:textSize="@dimen/text_default"/>

    <TextView
        android:id="@+id/tv_seconds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/background_btn_red"
        android:padding="@dimen/default_margin_small"
        android:text="00"
        android:textColor="@color/white"
        android:textSize="@dimen/text_default"/>
</LinearLayout>

然后就是自定义的linearlayout了,

/**
 * 倒计时 3文本
 * Created By Fangchao On 2015/3/12
 */
public class TimeTextView extends LinearLayout {
    private long mday, mhour, mmin, msecond;//天,小时,分钟,秒
    private boolean run = false; //是否启动了
    Timer timer = new Timer();
    TextView Vhour, Vmin, Vseconds;

    public TimeTextView(Context context) {
        super(context);
        iniUI(context);
    }

    public TimeTextView(Context context, AttributeSet attrs) {

        super(context, attrs);
        iniUI(context);
    }

    public void iniUI(Context context) {
        LayoutInflater mInflater = LayoutInflater.from(context);
        View myView = mInflater.inflate(R.layout.view_time_texviews, null);

        Vhour = (TextView) myView.findViewById(R.id.tv_hours);
        Vmin = (TextView) myView.findViewById(R.id.tv_minutes);
        Vseconds = (TextView) myView.findViewById(R.id.tv_seconds);
        addView(myView);
    }

    public TimeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        iniUI(context);
    }

    private Handler mHandler = new Handler() {

    };

    public boolean isRun() {
        return run;
    }

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

    public void start() {
        if (!isRun()) {
            setRun(true);
            timer.schedule(task, 1000, 1000);
        }
    }

    /**
     * 根据传进来的时间差 为textview 赋值
     *
     * @param duration
     */
    public void setTimes(long duration) {
        Date date = new Date(duration);
        Date date1 = new Date(1L);
        /*mday = duration / 60000 / 60 / 24;
        mhour = (duration - mday * 6000 * 60 * 24) / 3600000;

        mmin = (duration - mhour * 6000 * 60 - mday * 3600000 * 24) / 60000;
        msecond = (duration - mmin * 60000 - mhour * 3600000 - mday * 3600000 * 24) / 60000;*/
//需要修改,测试用
        mday = date.getDay();
        mhour = date.getHours();
        mmin = date.getMinutes();
        msecond = date.getSeconds();
    }

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

    TimerTask task = new TimerTask() {
        @Override
        public void run() {

            mHandler.post(new Runnable() {      // UI thread
                @Override
                public void run() {
                    run = true;
                    ComputeTime();
                    if (mday < 0) {
                        setVisibility(View.GONE);

                        setRun(false);
                    }
                    Vhour.setText(mhour < 10 ? ("0" + mhour) : mhour + "");
                    Vseconds.setText(msecond < 10 ? ("0" + msecond) : msecond + "");
                    Vmin.setText(mmin < 10 ? ("0" + mmin) : mmin + "");
                }
            });
        }
    };
}

用的时候直接在需要的地方直接把TimeTextview 放上就行了,
举个例子吧,可以在adapter中设置倒计时时长。。。。

   @Override
    public void onBindHeaderView(RecyclerView.ViewHolder holder, int position) {
        HeaderViewHolder headViewHolder = (HeaderViewHolder) holder;

        long duration=  1426244976513L + 200 * 1000 - TimeUtils.getCurrentTimeInLong();
        Log.e("long///////", TimeUtils.getCurrentTimeInLong() + "");
        if (!headViewHolder.timeTextView.isRun()) {

            headViewHolder.timeTextView.setTimes(duration);
            headViewHolder.timeTextView.start();
        }
    }

自定义控件----倒计时控件

原文:http://blog.csdn.net/fangchao3652/article/details/44241587

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!