-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStyleUtils.kt
More file actions
119 lines (113 loc) · 5.2 KB
/
StyleUtils.kt
File metadata and controls
119 lines (113 loc) · 5.2 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.handy.basic.utils
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
/**
* @title: StyleUtils
* @projectName HandyBasicKT
* @description: 样式效果工具类
* @author LiuJie https://github.com/Handy045
* @date Created in 2019-11-04 16:28
*/
class StyleUtils private constructor() {
companion object {
/**
* 通过代码设置Selector效果
*
* @param context 上下文
* @param idNormal 默认样式(或者图片和颜色的资源ID)
* @param idPressed 点击样式(或者图片和颜色的资源ID)
* @return Selector样式
*/
fun getStateDrawable(context: Context, @DrawableRes idNormal: Int, @DrawableRes idPressed: Int): StateListDrawable {
return getStateDrawable(context, idNormal, idPressed, idPressed)
}
/**
* 通过代码设置Selector效果
*
* @param context 上下文
* @param idNormal 默认样式(或者图片和颜色的资源ID)
* @param idPressed 点击样式(或者图片和颜色的资源ID)
* @param idFocused 焦点样式(或者图片和颜色的资源ID)
* @return Selector样式
*/
fun getStateDrawable(context: Context, @DrawableRes idNormal: Int, @DrawableRes idPressed: Int, @DrawableRes idFocused: Int): StateListDrawable {
val normal = if (idNormal == -1) null else ContextCompat.getDrawable(context, idNormal)
val pressed =
if (idPressed == -1) null else ContextCompat.getDrawable(context, idPressed)
val focus = if (idFocused == -1) null else ContextCompat.getDrawable(context, idFocused)
return getStateDrawable(normal, pressed, focus)
}
/**
* 通过代码设置Selector效果
*
* @param context 上下文
* @param idNormal 默认样式(或者图片和颜色的资源ID)
* @param idPressed 点击样式(或者图片和颜色的资源ID)
* @param idFocused 焦点样式(或者图片和颜色的资源ID)
* @return Selector样式
*/
fun getStateDrawable(
normal: Drawable?,
pressed: Drawable?,
focus: Drawable?
): StateListDrawable {
val stateListDrawable = StateListDrawable()
//注意该处的顺序,只要有一个状态与之相配,背景就会被换掉
//所以不要把大范围放在前面了,如果sd.addState(new[]{},normal)放在第一个的话,就没有什么效果了
stateListDrawable.addState(
intArrayOf(
android.R.attr.state_enabled,
android.R.attr.state_focused
), focus
)
stateListDrawable.addState(
intArrayOf(
android.R.attr.state_pressed,
android.R.attr.state_enabled
), pressed
)
stateListDrawable.addState(intArrayOf(android.R.attr.state_focused), focus)
stateListDrawable.addState(intArrayOf(android.R.attr.state_pressed), pressed)
stateListDrawable.addState(intArrayOf(android.R.attr.state_enabled), normal)
stateListDrawable.addState(intArrayOf(), normal)
return stateListDrawable
}
/**
* 通过代码设置Selector效果
*
* @param context 上下文
* @param idNormal 默认样式(或者图片和颜色的资源ID)
* @param idPressed 点击样式(或者图片和颜色的资源ID)
* @param idFocused 焦点样式(或者图片和颜色的资源ID)
* @return Selector样式
*/
fun getStateColor(context: Context, @ColorRes idNormal: Int, @ColorRes idPressed: Int, @ColorRes idFocused: Int): ColorStateList {
val normal = ContextCompat.getColor(context, idNormal)
val pressed = ContextCompat.getColor(context, idPressed)
val focus = ContextCompat.getColor(context, idFocused)
return getStateColor(normal, pressed, focus)
}
/**
* 通过代码设置Selector效果
*
* @return Selector样式
*/
fun getStateColor(@ColorInt normal: Int, @ColorInt pressed: Int, @ColorInt focus: Int): ColorStateList {
val colors = intArrayOf(focus, pressed, focus, pressed, normal, normal)
val states = arrayOfNulls<IntArray>(6)
states[0] = intArrayOf(android.R.attr.state_enabled, android.R.attr.state_focused)
states[1] = intArrayOf(android.R.attr.state_pressed, android.R.attr.state_enabled)
states[2] = intArrayOf(android.R.attr.state_focused)
states[3] = intArrayOf(android.R.attr.state_pressed)
states[4] = intArrayOf(android.R.attr.state_enabled)
states[5] = intArrayOf()
return ColorStateList(states, colors)
}
}
}