This repository was archived by the owner on Nov 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathShapeAttributeView.kt
134 lines (115 loc) · 4.15 KB
/
ShapeAttributeView.kt
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.material.materialthemebuilder.widget
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import androidx.core.view.ViewCompat
import androidx.appcompat.widget.AppCompatTextView
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import io.material.materialthemebuilder.R
/**
* A composite view to display a text label and a shape preview view.
*
* The shape preview view is used to visualize the properties of shapeAppearance theme attributes.
* The shape preview view is a TextView with it's background set to a MaterialShapeDrawable,
* inflated according to this view's app:shapeAppearanceAttr property.
*/
class ShapeAttributeView @JvmOverloads constructor(
context: Context,
private val attrs: AttributeSet? = null,
private val defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : FrameLayout(context, attrs, defStyleAttr, defStyleRes) {
private val shapeAttributeTextView: AppCompatTextView
private val shapePreviewView: AppCompatTextView
var shapeAttrText: String = "?shapeAppearanceSmallComponent"
set(value) {
shapeAttributeTextView.text = value
field = value
}
private var shape = MaterialShapeDrawable().apply {
strokeWidth = DEFAULT_SHAPE_STROKE_WIDTH
}
var shapeAppearanceRes: Int = R.attr.shapeAppearanceSmallComponent
set(value) {
shape.shapeAppearanceModel = ShapeAppearanceModel.builder(
context,
attrs,
defStyleAttr,
getShapeAppearanceDefaultRes(value)
).build()
ViewCompat.setBackground(shapePreviewView, shape)
field = value
}
var shapeFillColor: Int = Color.LTGRAY
set(value) {
shape.fillColor = ColorStateList.valueOf(value)
field = value
}
var shapeStrokeColor: Int = Color.DKGRAY
set(value) {
shape.strokeColor = ColorStateList.valueOf(value)
field = value
}
var shapeLetter: String = context.getString(R.string.shape_appearance_small_label)
set(value) {
shapePreviewView.text = value
field = value
}
init {
val view = View.inflate(context, R.layout.shape_attribute_view_layout, this)
shapeAttributeTextView = view.findViewById(R.id.shape_attribute)
shapePreviewView = view.findViewById(R.id.shape_preview)
val a = context.theme.obtainStyledAttributes(
attrs,
R.styleable.ShapeAttributeView,
defStyleAttr,
defStyleRes
)
shapeAttrText = a.getString(
R.styleable.ShapeAttributeView_android_text
) ?: shapeAttrText
shapeFillColor = a.getColor(
R.styleable.ShapeAttributeView_shapeFillColor,
Color.LTGRAY
)
shapeStrokeColor = a.getColor(
R.styleable.ShapeAttributeView_shapeStrokeColor,
Color.DKGRAY
)
shapeAppearanceRes = a.getResourceId(
R.styleable.ShapeAttributeView_shapeAppearanceAttr,
R.attr.shapeAppearanceSmallComponent
)
shapeLetter = a.getString(
R.styleable.ShapeAttributeView_shapeSizeLetter
) ?: shapeLetter
a.recycle()
}
private fun getShapeAppearanceDefaultRes(shapeAppearanceRes: Int) = when (shapeAppearanceRes) {
R.attr.shapeAppearanceMediumComponent -> R.style.Widget_MaterialComponents_CardView
R.attr.shapeAppearanceLargeComponent -> R.style.Widget_MaterialComponents_NavigationView
else -> R.style.Widget_MaterialComponents_Button
}
companion object {
private const val DEFAULT_SHAPE_STROKE_WIDTH = 2F
}
}