Skip to content

Commit dd5d998

Browse files
committed
增加 WebView 打开网页的夜间模式
1 parent a47549c commit dd5d998

5 files changed

Lines changed: 115 additions & 2 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.cxz.wanandroid.utils
2+
3+
import android.graphics.Color
4+
import android.support.annotation.ColorInt
5+
import android.support.annotation.FloatRange
6+
import android.support.annotation.IntRange
7+
8+
/**
9+
* @author chenxz
10+
* @date 2019/11/24
11+
* @desc ColorUtil
12+
*/
13+
object ColorUtil {
14+
15+
/**
16+
* 计算颜色
17+
*
18+
* @param color color值
19+
* @param alpha alpha值
20+
* @return 最终的状态栏颜色
21+
*/
22+
fun alphaColor(@ColorInt color: Int, @IntRange(from = 0, to = 255) alpha: Int): Int {
23+
val red = Color.red(color)
24+
val green = Color.green(color)
25+
val blue = Color.blue(color)
26+
return Color.argb(alpha, red, green, blue)
27+
}
28+
29+
/**
30+
* 计算颜色
31+
*
32+
* @param color color值
33+
* @param alpha alpha值[0-1]
34+
* @return 最终的状态栏颜色
35+
*/
36+
fun alphaColor(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) alpha: Float): Int {
37+
return alphaColor(color, (alpha * 255).toInt())
38+
}
39+
40+
/**
41+
* 根据fraction值来计算当前的颜色
42+
*
43+
* @param colorFrom 起始颜色
44+
* @param colorTo 结束颜色
45+
* @param fraction 变量
46+
* @return 当前颜色
47+
*/
48+
fun changingColor(@ColorInt colorFrom: Int, @ColorInt colorTo: Int, @FloatRange(from = 0.0, to = 1.0) fraction: Float): Int {
49+
val redStart = Color.red(colorFrom)
50+
val blueStart = Color.blue(colorFrom)
51+
val greenStart = Color.green(colorFrom)
52+
val alphaStart = Color.alpha(colorFrom)
53+
54+
val redEnd = Color.red(colorTo)
55+
val blueEnd = Color.blue(colorTo)
56+
val greenEnd = Color.green(colorTo)
57+
val alphaEnd = Color.alpha(colorTo)
58+
59+
val redDifference = redEnd - redStart
60+
val blueDifference = blueEnd - blueStart
61+
val greenDifference = greenEnd - greenStart
62+
val alphaDifference = alphaEnd - alphaStart
63+
64+
val redCurrent = (redStart + fraction * redDifference).toInt()
65+
val blueCurrent = (blueStart + fraction * blueDifference).toInt()
66+
val greenCurrent = (greenStart + fraction * greenDifference).toInt()
67+
val alphaCurrent = (alphaStart + fraction * alphaDifference).toInt()
68+
69+
return Color.argb(alphaCurrent, redCurrent, greenCurrent, blueCurrent)
70+
}
71+
72+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.cxz.wanandroid.widget
2+
3+
import android.content.Context
4+
import android.graphics.Canvas
5+
import android.graphics.Color
6+
import android.support.design.widget.CoordinatorLayout
7+
import android.support.v4.content.ContextCompat
8+
import android.util.AttributeSet
9+
import com.cxz.wanandroid.R
10+
import com.cxz.wanandroid.utils.ColorUtil
11+
import com.cxz.wanandroid.utils.SettingUtil
12+
13+
/**
14+
* @author chenxz
15+
* @date 2019/11/24
16+
* @desc WebContainer
17+
*/
18+
class WebContainer @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0)
19+
: CoordinatorLayout(context, attrs, defStyleAttr) {
20+
21+
private var mDarkTheme: Boolean = false
22+
23+
private var mMaskColor = Color.TRANSPARENT
24+
25+
init {
26+
mDarkTheme = SettingUtil.getIsNightMode()
27+
if (mDarkTheme) {
28+
mMaskColor = ColorUtil.alphaColor(ContextCompat.getColor(getContext(), R.color.mask_color), 0.6f)
29+
}
30+
}
31+
32+
override fun dispatchDraw(canvas: Canvas) {
33+
super.dispatchDraw(canvas)
34+
if (mDarkTheme) {
35+
canvas.drawColor(mMaskColor)
36+
}
37+
}
38+
39+
}

app/src/main/res/layout/activity_content.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<com.cxz.wanandroid.widget.WebContainer xmlns:android="http://schemas.android.com/apk/res/android"
33
android:id="@+id/cl_main"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
@@ -10,4 +10,4 @@
1010

1111
<!--<include layout="@layout/container" />-->
1212

13-
</android.support.design.widget.CoordinatorLayout>
13+
</com.cxz.wanandroid.widget.WebContainer>

app/src/main/res/values-night/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<color name="windowBackground">@color/Grey900</color>
99
<color name="line_divider">@color/Grey700</color>
1010
<color name="list_divider">@color/Grey600</color>
11+
<color name="mask_color">#111111</color>
1112

1213
<color name="common_color">@color/Grey300</color>
1314
<color name="item_title">@color/Grey300</color>

app/src/main/res/values/colors.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<color name="windowBackground">@color/Grey100</color>
99
<color name="line_divider">@color/Grey300</color>
1010
<color name="list_divider">@color/Grey400</color>
11+
<color name="mask_color">#f5f5f5</color>
1112

1213
<!-- item -->
1314
<color name="common_color">#19191B</color>

0 commit comments

Comments
 (0)