1. 夜间模式
所谓的夜间模式,就是能够根据不同的设定,呈现不同风格的界面给用户,而且晚上看着不伤眼睛。特别是一些新闻类App实现夜间模式是非常人性化的,增强用户体验。
2. 我根据网上的资料 以及自己代码亲测,总结如下两种方法:
(1)降低屏幕亮度
(2)替换theme
3. 夜间模式之 降低屏幕亮度:
(1)创建一个Android工程,命名为"夜间模式_利用屏幕亮度(App)",如下:
(2)首先我们来到主布局之中,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.himi.screenlight.MainActivity" > <Button
android:onClick="SetAppBrightness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置应用程序内亮度" /> </LinearLayout>
(3)来到MainActivity,如下:
package com.himi.screenlight; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
/**
*
* 降低屏幕的亮度
* 该方法缺点是不能将亮度值保存起来,可以通过SharedPreferences来保存数据。
* @author hebao
*
*/
public class MainActivity extends Activity { private static int init = 1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} /****************设置应用程序的亮度*****************/
public void SetAppBrightness(View view) {
switch(init % 5){
case 0:
setBrightness(50); break;
case 1:
setBrightness(100); break;
case 2:
setBrightness(150); break;
case 3:
setBrightness(200); break;
case 4:
setBrightness(255); break; }
init ++;
} public void setBrightness(int brightness) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(lp);
} }
运行到手机上是可以改变屏幕亮度的,这种方法实际开发中使用得不多。
上面关于设置屏幕亮度的核心代码如下:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = brightness / 255.0f;
getWindow().setAttributes(lp);
4. 夜间模式之 替换theme:
(1)创建一个Android工程,命名为"夜间模式_利用theme",如下:
(2)在工程 res/values/attrs.xml 文件中,增加自定义属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NightMode">
<attr name="day_night_background" format="color" />
<attr name="day_night_text_color" format="color" />
</declare-styleable>
</resources>
(3)在工程res/values/colors.xml文件中,增加自定义属性用到的颜色。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="night_color">#000000</color>
<color name="light_color">#ffffff</color>
</resources>
(4)在工程 res/values/styles.xml 文件中,增加"AppSunTheme" 和"AppNightTheme",parent 均为AppBaseTheme. 同时在这两个styles中一一对应的加入attrs.xml文件中的属性。
<resources> <!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style> <!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style> <!-- 自定义的 Application theme. -->
<style name="AppSunTheme" parent="AppBaseTheme">
<item name="day_night_background">@color/light_color</item>
<item name="day_night_text_color">@color/night_color</item>
</style> <style name="AppNightTheme" parent="AppBaseTheme">
<item name="day_night_background">@color/night_color</item>
<item name="day_night_text_color">@color/light_color</item>
</style> </resources>
(5)来到主布局文件之中,将主布局activity_main.xml中需要根据主题改变的元素的background 和 color 设为自定义attrs中的属性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/day_night_background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <Button
android:id="@+id/btn_theme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/night"
android:layout_below="@+id/night"
android:layout_marginTop="28dp"
android:text="使用Theme实现夜间模式"
android:textColor="?attr/day_night_text_color" /> </RelativeLayout>
(6)在工程中加入NightModeUtils类来配置应用主题。因为这里要改变整个APP的主题,所以传给NightModeUtils的Context应该是Application的Context。
package com.himi.screen; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.TextView; public class NightModeUtils {
public final static int THEME_SUN = 1; public final static int THEME_NIGHT = 2; /**
* Set the theme of the Activity, and restart it by creating a new Activity
* of the same type.
*/
public static void changeToTheme(Activity activity) {
int theme1=getDayNightMode(activity);
int theme =( theme1 == THEME_SUN ? THEME_NIGHT : THEME_SUN);
setDayNightMode(activity, theme); activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
} /** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity) {
int theme = getDayNightMode(activity);
switch (theme) {
case THEME_SUN:
activity.setTheme(R.style.AppSunTheme);
break;
case THEME_NIGHT:
activity.setTheme(R.style.AppNightTheme);
break;
default:
break;
}
} public static void setBackGroundColor(Context context, View view, int theme) {
int color = context.getResources().getColor(
theme == THEME_SUN ? R.color.light_color : R.color.night_color);
view.setBackgroundColor(color);
} public static void setTextColor(Context context, View view, int theme) {
int color = context.getResources().getColor(
theme == THEME_SUN ? R.color.night_color : R.color.light_color);
TextView textView = (TextView)view;
textView.setTextColor(color);
} public static int getSwitchDayNightMode(Context context) {
int mode = getDayNightMode(context);
return mode == THEME_SUN ? THEME_NIGHT : THEME_SUN;
}
60 //设置theme属性值存入xml文件之中
public static void setDayNightMode(Context context, int mode) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
sharedPreferencesEditor.putInt("SUN_NIGHT_MODE", mode);
sharedPreferencesEditor.apply();
}
//获取xml文件中theme属性值
public static int getDayNightMode(Context context) {
SharedPreferences sharedPreferences = getSharedPreferences(context);
return sharedPreferences.getInt("SUN_NIGHT_MODE", THEME_SUN);
}
72 //设置xml文件
private static SharedPreferences getSharedPreferences(Context context) {
return context.getSharedPreferences("NightModeDemo", Context.MODE_APPEND);
}
}
(7) 在每个Activity中增加调用nightModeUtils类的设置主题方法,注意要加在setContentView方法之前。
setTheme方法只能在onCreate方法中实现,所以如果要改变当前Activity的注意要将当前Activity先finish,再重新启动Activity。
package com.himi.screen; import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { private Button btn_theme; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
18 // 设置启动时默认theme
NightModeUtils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_main); btn_theme = (Button) findViewById(R.id.btn_theme); btn_theme.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 获取当前theme
int theme = NightModeUtils.getDayNightMode(MainActivity.this); Context context = getApplicationContext(); if (theme == NightModeUtils.THEME_SUN)
NightModeUtils.setDayNightMode(context,
NightModeUtils.THEME_NIGHT);
else
NightModeUtils.setDayNightMode(context,
NightModeUtils.THEME_SUN); // 注意改过主题后一定要,把activity finish在重开一遍,因为更改主题只能在oncreat中进行
finish();
startActivity(new Intent(MainActivity.this, MainActivity.this.getClass())); }
});
} }
注意的是:当我们替换主题的时候,我们需要finish掉旧Activity,重新使用startActivity开启新Activity,这是因为我们只是替换主题属性,界面并没有重新绘制,需要Activity重新渲染界面,所以需要根据新的属性,重新绘制Activity。
(8)将Mainfest配置文件中Application 的theme设为默认的AppSunTheme:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.himi.screen"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="21" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppSunTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
上面设置了应用程序初始状态的主题样式为AppSunTheme
总结如下:
(9)部署程序到手机上测试,如下:
本文示例代码下载地址:http://download.csdn.net/detail/hebao5201314/9591112