经常使用的自己定义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)
上一篇 2021年12月1日 上午6:00
下一篇 2021年12月1日 上午6:00


相关推荐

  • Shift 后门 简单学习

    Shift 后门 简单学习https://www.cnblogs.com/BOHB-yunying/p/11695140.html过程参考上文链接提出问题:整个过程并不复杂,但要实现攻击的前提条件,是已经打入目标主机,将C:\windows\system32\sethc.exe替换成C:\windows\system32\cmd.exe,所以这一步到底要如何做到?明白shift后门是如何产生的就是windows系统自带的5次shift键强制打开C:\windows\system32\sethc.exe,为了..

    2026年1月17日
    8
  • n8n基础教程系列(五)code节点介绍

    n8n基础教程系列(五)code节点介绍

    2026年3月15日
    3
  • git 清除用户名密码

    清空所有用户名和密码:gitconfig–system–unsetcredential.helper只用这一个命令就可以,如果不好使可以参照下面命令查看config配置:gitconfig–list查看git用户名:gitconfiguser.name清除缓存的用户名和密码:gitcredential-manageruninstall更改全局用户名:g…

    2022年4月8日
    452
  • PCR雷达传感器感应_倒车雷达传感器在哪里

    PCR雷达传感器感应_倒车雷达传感器在哪里一.设备唤醒i》检测人靠近设备ii》无视穿越的人员iii》可做手势识别应用场景:智能音箱;笔记本;广告机;投影仪;灯具;控制面板开关独特算法:1》 检测静止不动的人员,内置检测人的呼吸信号。图示为雷达传感器抓取人呼吸的信号在0.3-0.35hz效果。2》 可过滤快速移动物体干扰,内置仅对慢速移动检测,图示效果为雷达传感器过滤风扇对测试的影响。二.车内人员检测欧洲新车评估计划(EuroNCAP)计划在2022年将儿童存在检测纳入全面评级。测试评估分析:1岁婴儿睡在儿童保护座椅上

    2026年4月18日
    5
  • 还在为 idea中文显示乱码 而烦恼?解决方法总结在这里了

    还在为 idea中文显示乱码 而烦恼?解决方法总结在这里了归纳总结多种 IDEA 中文乱码问题的解决方法 彻底解决 IntellijIDEA 常见中文乱码问题 包括标题乱码 console 控制台乱码 properties 文件乱码 修改 IDEA 编码导致中文乱码等中文乱码问题 建议收藏

    2026年3月18日
    1
  • SpringBoot学习目录

    SpringBoot目录SpringBoot入门篇SpringBootWeb篇SpringBootRedis篇SpringBootMybatis篇SpringBootRabbitMQ篇SpringBoot的定时器SpringBoot集成邮件服务SpringBoot集成MongoDBSpringBoot集成ShiroSpringBoot文件上传Spring…

    2022年4月14日
    39

发表回复

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

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