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

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

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。



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

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

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

效果图:右上角的时间

//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>


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118809.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 静态分析工具之-AXMLPrinter2.jar的使用方法

    静态分析工具之-AXMLPrinter2.jar的使用方法

    2021年10月1日
    39
  • poj 2408 Anagram Groups(hash)

    poj 2408 Anagram Groups(hash)

    2022年1月2日
    39
  • 花生日记

    花生日记花生日记app是一款优秀的导购软件它能带你找到淘宝天猫隐藏的大额优惠券!领取优惠券、交易都是在【淘宝天猫】上完成的,绝对安全!

    2022年6月1日
    59
  • 逆矩阵的伴随阵的求法_伴随矩阵与原矩阵的特征值

    逆矩阵的伴随阵的求法_伴随矩阵与原矩阵的特征值1、简单介绍         一个方阵A如果满足,则A可逆,且

    2022年8月21日
    6
  • 数据库三大范式(重要)

    数据库三大范式(重要)转自微信上周四下午的 VIP 试听课是由芒果给大家介绍的 MySQL 的一些基础知识 在这里芒果给大家分享其中的一部分内容 对数据库三大范式做个小介绍 范式 数据库的设计范式 是符合某一种级别的关系模式的集合 构造数据库必须遵循一定的规则 在关系数据库中 这种规则就是范式 关系数据库中的关系必须满足一定的要求 即满足不同的范式 范式 对于软件的性能 数据库的维护都是我们软件从业人员必须掌握的内容

    2025年6月15日
    5
  • Linux下Centos7对外开放端口

    Linux下Centos7对外开放端口概要最近在docker下搭建MySQL和Redis环境,远程linux主机内部网络都走通了,但是就是外网无法连接远程服务器的MySQL和Redis。经过一番查找和学习,终于找到了问题,不仅远程服务器上docker要做好内部和外部端口的映射,关键还要对对外开放的端口添加到防火墙中。内容介绍的逻辑是:本篇文章先记录Centos7下查看防火墙状态和网络状态命令;下一篇将介绍通过docker…

    2025年9月30日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号