Effective implementation of high performance dynamic layout, drawable for Android via kotlin dsl.
In your build.gradle
:
allprojects {
repositories {
mavenCentral()
}
}
In your module/build.gradle
:
dependencies {
implementation 'com.weicools:fluent-ui:1.0.1'
}
val rootView = constraintLayout {
layoutParams = defaultParams(matchParent, 64.dp)
imageView(R.id.iconId) {
layoutParams = constraintParams(40.dp, 40.dp) {
leftToLeft = parentId
centerVerticalOf = parentId
leftMargin = 16.dp
}
imageResource = R.drawable.ic_arrow
}
textView(R.id.textId) {
layoutParams = constraintParams(0, wrapContent) {
leftToRight = R.id.iconId
rightToRight = parentId
centerVerticalOf = parentId
leftMargin = 8.dp
rightMargin = 16.dp
}
text = "layout title"
}
}
Note 📢 The main process is automatically initialized context by default, Multiple processes need to be initialized manually: [FluentUiInitializer.initialize] Avoid using
xxxResourceOf
methods (such ascolorResourceOf
,dimenResourceOf
, etc.) in View/Layout (which internally depends on appContext). Using them directly can cause the View/Layout to fail to preview because during preview, appContext is not initialized and cannot access the relevant resources.
com.weikeet.ui
├── animation -> 动画: Kotlin dsl 构建动画
├── app -> 应用: app context 以及通过 app context 获取相关资源
├── graphics -> 图形: Kotlin dsl 构建 drawable
├── res -> 资源: color, dimen, drawable, font, string
├── unit -> 单位: dp, sp, px 转换
├── view -> 视图: Kotlin dsl 构建 view, viewgroup
├── widget -> 控件: Kotlin 控件属性扩展
└── window -> 窗口: WindowInsets(StatusBar, NavigationBar, ime) 相关监听
快速创建动画 animator
startFloatAnimation(0f, 1f) {
doOnFloatUpdate { animatedValue ->
// do something
}
doOnStart {
// do something
}
doOnEnd {
// do something
}
duration = 240
// repeatCount = 0
// repeatMode =
// startDelay = 0
// interpolator = LinearInterpolator()
}
more uses see Animator.kt
避免在 View/Layout 直接使用
dpOf(xx)
和xxxResourceOf
方法 (如colorResourceOf
,dimenResourceOf
等), 直接使用会导致 View/Layout 预览失败, 因为预览时 appContext 未初始化, 无法访问相关资源. 应该使用 xx.dp, dip(xx),xxxResources
方法 (如colorResources
,dimenResources
等)
资源获取
// get resource using app context
val color = colorResourceOf(R.color.colorPrimary)
val colorList = colorStateListOf(R.color.colorPrimary)
val dimen16 = dimenResourceOf(R.dimen.padding_16)
val dimenSize16 = dimenSizeResourceOf(R.dimen.padding_16)
val dimenOffset16 = dimenOffsetResourceOf(R.dimen.padding_16)
val drawable = drawableResourceOf(R.drawable.ic_arrow)
val font = fontResourceOf(R.font.font_name)
val string = stringResourceOf(R.string.app_name)
Unit 转换
// Convert dp to px using app context
val dp10 = dpOf(10)
val dp10f = dpOf(10f)
使用 Kotlin dsl 像 xml 一样构建 drawable
// create drawable using kotlin dsl
val drawable = shapeDrawable {
corners {
radius = 20f.dp
}
solidColor = 0xff00ff00.toInt()
}
more uses see
Context/Fragment/View 快捷获取资源
// get color using context/fragment/view resource
val color = colorResources(R.color.colorPrimary)
val colorList = colorStateList(R.color.colorPrimary)
val dimen16 = dimenResources(R.dimen.padding_16)
val dimenSize16 = dimenSizeResources(R.dimen.padding_16)
val dimenOffset16 = dimenOffsetResources(R.dimen.padding_16)
val drawable = drawableResources(R.drawable.ic_arrow)
val font = fontResources(R.font.font_name)
val string = stringResources(R.string.app_name)
dp, px 转换
// Convert dp to px using context/fragment/view resource
val dp10 = dp(10)
val dp10f = dp(10f)
// Convert sp to px using system resource
val dp20 = 20.dp
val dp20f = 20f.dp
使用 Kotlin dsl 像 xml 一样构建 View, ViewGroup
val rootView = constraintLayout {
layoutParams = defaultParams(matchParent, 64.dp)
imageView(R.id.iconId) {
layoutParams = constraintParams(40.dp, 40.dp) {
// ...
}
}
textView(R.id.textId) {
layoutParams = constraintParams(0, wrapContent) {
// ...
}
}
}
more uses see ui/view
kotlin 控件属性扩展
context.textView {
textColorResource = R.color.colorPrimary
stringResource = R.string.app_name
// ...
}
context.imageView {
imageResource = R.drawable.ic_arrow
// ...
}
more uses see ui/widget
WindowInsets 相关监听
// adapt edge to edge
WindowInsetsEdgeDelegate(activity).start()
// listen window insets
view.doOnApplyWindowInsets { windowInsets ->
// do something
// eg: view.updatePadding(top = windowInsets.systemBarTop)
}
more uses see ui/window
Copyright (c) 2021-present. Weiwei
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.