首页 > 代码库 > 常用的自定义UI组件- 一:TimeView

常用的自定义UI组件- 一:TimeView



最近做蛋疼的机顶盒项目,以后遇到哪些常用的组件,记录于此。

反编译 youku视频TV偷来的。。也希望各位童鞋多学习别人的代码,为己所用。

当然还有其他的办法,比如监听系统发出的广播等等。等有时间再把那个贴上来。


效果图:右上角的时间

http://img.blog.csdn.net/20140630144648750?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGVlaHUxOTg3/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center


TimeView.java


package com.youku.tv.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Calendar;
import java.util.Date;


public class TimeView extends LinearLayout{
    class ClockHandler extends Handler {

        private void post()
        {
            sendMessageDelayed(obtainMessage(0), 1000 * (60 - Calendar.getInstance().get(13)));
        }

        public void handleMessage(Message message){
            super.handleMessage(message);
            if(!mStopped){
                updateClock();
                post();
            }
        }

        public void startScheduleUpdate(){
            mStopped = false;
            post();
        }

        public void stopScheduleUpdate(){
            mStopped = true;
            removeMessages(0);
        }

        private boolean mStopped;
        final TimeView this$0;

        ClockHandler(){
            super();
            this$0 = TimeView.this;
        }
    }


    public TimeView(Context context, AttributeSet attributeset){
        super(context, attributeset);
        mContext = getContext();
        ((LayoutInflater)context.getSystemService("layout_inflater")).inflate(R.layout.time_simple, this);
        mHour = (TextView)findViewById(R.id.system_hour);
        mMinute = (TextView)findViewById(R.id.system_minute);
        mClockUpdater = new ClockHandler();
    }


    protected void onAttachedToWindow(){
        super.onAttachedToWindow();
        updateClock();
        mClockUpdater.startScheduleUpdate();
    }

    protected void onDetachedFromWindow(){
        super.onDetachedFromWindow();
        mClockUpdater.stopScheduleUpdate();
    }

    protected void onVisibilityChanged(View view, int i)
    {
        super.onVisibilityChanged(view, i);
    }

    void updateClock()
    {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int k = calendar.get(5);
        int l = calendar.get(11);
        int i1 = calendar.get(12);
        if(mHour != null)
        {
            TextView textview2 = mHour;
            Object aobj2[] = new Object[1];
            aobj2[0] = Integer.valueOf(l);
            textview2.setText(String.format("%2d:", aobj2));
        }
        if(mMinute != null)
            if(i1 < 10)
            {
                TextView textview1 = mMinute;
                Object aobj1[] = new Object[1];
                aobj1[0] = Integer.valueOf(i1);
                textview1.setText(String.format("0%d", aobj1));
            } else
            {
                TextView textview = mMinute;
                Object aobj[] = new Object[1];
                aobj[0] = Integer.valueOf(i1);
                textview.setText(String.format("%2d", aobj));
            }
        invalidate();
    }

    private static final String TAG = "TimeView";
    private ClockHandler mClockUpdater;
    private Context mContext;
    private TextView mHour;
    private TextView mMinute;

}

time_simple.xml布局文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@id/system_minute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="01"
        android:textColor="@color/timecolor"
        android:textSize="@dimen/px42" />

    <TextView
        android:id="@id/system_hour"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/system_minute"
        android:text="55"
        android:textColor="@color/timecolor"
        android:textSize="@dimen/px42" />

</RelativeLayout>