码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-30 11:57:33      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   java   使用   os   



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

反编译 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>




经常使用的自己定义UI组件- 一:TimeView,布布扣,bubuko.com

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

标签:android   style   blog   http   color   java   使用   os   

原文地址:http://www.cnblogs.com/yxwkf/p/3877387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!