Eirot Chen

世界上最幸福的事莫过于有能力做自己喜欢的事!

  • 主页
  • 心晴
  • Android
  • Linux
  • Devtools
所有文章 关于我

Eirot Chen

世界上最幸福的事莫过于有能力做自己喜欢的事!

  • 主页
  • 心晴
  • Android
  • Linux
  • Devtools

DeskClock根据时间变换背景颜色

2016-07-06

DeskClock.java是入口Activity,它继承于BaseActivity。那么真正完成根据时间换色的就是在这里了,Let’s have a look !
在BaseActivity的onCreate和onResume中分别都有setBackgroundColor(),这是改变颜色的关键所在!只是需要你决定在什么时候调用这个函数哦,然后就是从数组中取出对应时间点的十六进制颜色值。

DeskClock

好了,直接上关键部分的代码吧~

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 /**
* BaseActivity.java
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

final int currentColor = Utils.getCurrentHourColor();
final int backgroundColor = savedInstanceState == null ? currentColor
: savedInstanceState.getInt(KEY_BACKGROUND_COLOR, currentColor);
setBackgroundColor(backgroundColor, false /* animate */);
}

@Override
protected void onResume() {
super.onResume();

// Register mOnTimeChangedReceiver to update current background color periodically.
if (mOnTimeChangedReceiver == null) {
final IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_TIME_TICK);
filter.addAction(Intent.ACTION_TIME_CHANGED);
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
registerReceiver(mOnTimeChangedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
}
}, filter);
}

// Ensure the background color is up-to-date.
setBackgroundColor(Utils.getCurrentHourColor(), true /* animate */);
}

/**
* Sets the current background color to the provided value and animates the change if desired.
*
* @param color the ARGB value to set as the current background color
* @param animate {@code true} if the change should be animated
*/
protected void setBackgroundColor(int color, boolean animate) {
if (mBackground == null) {
mBackground = new ColorDrawable(color);
getWindow().setBackgroundDrawable(mBackground);
}

if (mBackground.getColor() != color) {
if (animate) {
ObjectAnimator.ofObject(mBackground, "color", AnimatorUtils.ARGB_EVALUATOR, color)
.setDuration(BACKGROUND_COLOR_ANIMATION_DURATION)
.start();
} else {
mBackground.setColor(color);
}
}
}

再来看看Utils.java工具类的实现^_^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 /**
* Utils.java
* Returns the background color to use based on the current time.
*/
public static int getCurrentHourColor() {
return BACKGROUND_SPECTRUM[Calendar.getInstance().get(Calendar.HOUR_OF_DAY)];
}

/**
* The background colors of the app - it changes throughout out the day to mimic the sky.
*/
private static final int[] BACKGROUND_SPECTRUM = {
0xFF212121 /* 12 AM */,
0xFF20222A /* 1 AM */,
0xFF202233 /* 2 AM */,
0xFF1F2242 /* 3 AM */,
0xFF1E224F /* 4 AM */,
0xFF1D225C /* 5 AM */,
0xFF1B236B /* 6 AM */,
0xFF1A237E /* 7 AM */,
0xFF1D2783 /* 8 AM */,
0xFF232E8B /* 9 AM */,
0xFF283593 /* 10 AM */,
0xFF2C3998 /* 11 AM */,
0xFF303F9F /* 12 PM */,
0xFF2C3998 /* 1 PM */,
0xFF283593 /* 2 PM */,
0xFF232E8B /* 3 PM */,
0xFF1D2783 /* 4 PM */,
0xFF1A237E /* 5 PM */,
0xFF1B236B /* 6 PM */,
0xFF1D225C /* 7 PM */,
0xFF1E224F /* 8 PM */,
0xFF1F2242 /* 9 PM */,
0xFF202233 /* 10 PM */,
0xFF20222A /* 11 PM */
};

以下地址是参考DeskColock根据时间换色的Demo,参考请移步github ChangeColor
ChangeColor

赏

随手打赏几注彩票吧~

支付宝
微信
  • DeskClock
  • android

扫一扫,分享到微信

微信分享二维码
在AS上使用纯java方式调用OpenCV
The Android Widget Of Calculator
© 2018 Eirot Chen
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • Android Studio
  • music
  • OpenCV
  • Jni
  • android
  • 自定义View
  • Fragment
  • Activity
  • DeskClock
  • Hexo
  • ffmpeg
  • linux
  • mount
  • matlab
  • Android N Preview
  • 遇见
  • 旅行
  • widget
  • adb
  • Sublime Text 3
  • nginx
  • rtmp
  • 诗词
  • google play
  • apk
  • ccache
  • Shadowsocks
  • 个人小站
  • Markdown
  • devtools

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Android P Camera2 preview work flow

    2018-07-14

    #Android Studio

  • Android Studio上Native方式使用OpenCV

    2016-11-28

    #OpenCV#Jni

  • 在AS上使用纯java方式调用OpenCV

    2016-11-16

    #Android Studio#OpenCV

  • DeskClock根据时间变换背景颜色

    2016-07-06

    #DeskClock

  • The Android Widget Of Calculator

    2016-07-04

    #android#widget

  • Nexus 5X 刷入Android N Preview动手实践

    2016-04-27

    #Android N Preview

  • Ubuntu14.0 Sublime Text 3 安装及配置

    2016-04-22

    #Sublime Text 3

  • Android Studio Tip of the Day

    2016-04-12

    #Android Studio

  • 实用的小众工具,赶紧get吧

    2016-03-28

    #devtools

  • Android 自定义View之Dialog使用RelativeLayout布局无法全屏显示

    2016-03-18

    #android#自定义View

  • 如何下载Google play上的最新应用

    2016-03-08

    #google play#apk

  • Matlab快捷键

    2016-03-07

    #matlab

  • FFmpeg 基本命令

    2016-03-01

    #ffmpeg

  • Hexo博客自己给自己挖的坑之.gitignore

    2016-02-29

    #Hexo

  • adb push XXX.apk成功后,Launcher界面没有图标显示

    2016-02-27

    #adb

  • 如何科学免费地上网?一起翻墙吧

    2016-02-26

    #Shadowsocks

  • 搭建nginx rtmp直播服务器,ffmpeg模拟推流

    2016-02-26

    #ffmpeg#nginx#rtmp

  • OpenCV3.0将彩色视频转换为灰度视频

    2016-02-25

    #OpenCV

  • Add outchain of music.163.com to hexo article

    2016-02-24

    #music

  • 如何加速Android源码的编译

    2016-02-22

    #ccache

  • Linux永久挂载(mount)分区

    2016-02-22

    #linux#mount

  • 快用Hexo动手搭建自己的Blog吧

    2016-02-19

    #Hexo#个人小站

  • 好玩的音乐社区

    2016-02-16

    #music

  • My Valentine's Day of 2016

    2016-02-14

    #遇见#旅行

  • Complete Android Fragment & Activity Lifecycle

    2016-02-01

    #Fragment#Activity

  • 一棵开花的树

    2016-01-29

    #诗词

  • 马克飞象Markdown语法

    2016-01-29

    #Markdown

  • Hello Eirot hexo !

    2016-01-27

    #Hexo

西南大学2011级,90后爱幻想的双鱼座普通男青年。目前就职于深圳天珑无线科技有限公司,任职Camera应用工程师。游弋于代码之间,爱捣鼓,爱瞎想,更爱技术的纯粹。热爱开源技术,CV爱好者~