From 4f4c193103117fcf470f520a89629bfb0fbc92bb Mon Sep 17 00:00:00 2001 From: skydoves Date: Sun, 25 Aug 2019 00:23:37 +0900 Subject: [PATCH] added rainbow() view extension & released 1.0.1 --- README.md | 43 +++++++++++-------- .../com/skydoves/rainbowdemo/MainActivity.kt | 6 +++ dependencies.gradle | 4 +- .../main/java/com/skydoves/rainbow/Rainbow.kt | 30 +++++++------ .../com/skydoves/rainbow/ViewExtension.kt | 9 ++-- 5 files changed, 54 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index b117023..aea0c7a 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Add a dependency code to your **module**'s `build.gradle` file. ```gradle dependencies { - implementation "com.github.skydoves:rainbow:1.0.0" + implementation "com.github.skydoves:rainbow:1.0.1" } ``` @@ -42,40 +42,48 @@ Rainbow(myCardView).palette { // constructs a palette for collecting colors. +contextColor(R.color.green_200) +contextColor(R.color.blue_200) +color(Color.WHITE) // getting a color -}.withAlpha(225) // sets alpha +}.withAlpha(225) // sets alpha (0~255) .foreground() // applies gradations to myCardView ``` +Here is a kotlin-extesion way to apply gradations using `View.rainbow()` method to views. + +```kotlin +myLinearLayout.rainbow().palette { + +contextColor(R.color.skyBlue) + +contextColor(R.color.colorPrimary) +}.background(orientation = RainbowOrientation.TOP_BOTTOM, radius = 8) +``` + ### Background, Foreground -We can apply gradations composed with palette colors to the background or foreground.
-The `forground()` would be used for your `CardView` or something others. +We can apply gradations composed with palette colors to the view's background or foreground.
+The `forground()` method can be applied to your `CardView` or something others. ```kotlin -Rainbow(myCardView).palette { // constructs a palette for collecting colors. - +contextColor(R.color.red_200) // getting a color from the resource +Rainbow(myCardView).palette { + +contextColor(R.color.red_200) +contextColor(R.color.yellow_200) }.background() or .foreground() ``` -And we can control the gradation orientation and corner radius.
-We can change eight kinds of orientation using `RainbowOrientation`. +And we can control the gradient orientation and corner radius.
+We can use 8 kinds of orientation which `RainbowOrientation`. ```kotlin background(orientation = RainbowOrientation.RIGHT_LEFT, radius = 8) background(orientation = RainbowOrientation.TOP_BOTTOM, radius = 8) -foreground(orientation = RainbowOrientation.DIAGONAL_TOP_LEFT, radius = 8) -foreground(RainbowOrientation.DIAGONAL_BOTTOM_RIGHT, radius = 8) +foreground(RainbowOrientation.DIAGONAL_TOP_LEFT, 8) +foreground(RainbowOrientation.DIAGONAL_BOTTOM_RIGHT, 8) ``` ### Tinting -We can change tint colors to some kinds of views which can be tint.
-Here are views can be tint: TextView(drawable), ImageView, [CompoundButton](https://www.google.com/search?q=CompoundButton&oq=compo&aqs=chrome.0.69i59l2j69i57j69i60l2j69i61.4868j0j4&sourceid=chrome&ie=UTF-8), [TintableBackgroundView](https://developer.android.com/reference/androidx/core/view/TintableBackgroundView). +We can change some kinds of view's tint colors which can be applied tint.
+Here are views can be applied tint: TextView(drawable), ImageView, [CompoundButton](https://www.google.com/search?q=CompoundButton&oq=compo&aqs=chrome.0.69i59l2j69i57j69i60l2j69i61.4868j0j4&sourceid=chrome&ie=UTF-8), [TintableBackgroundView](https://developer.android.com/reference/androidx/core/view/TintableBackgroundView). ```kotlin Rainbow(myCheckBox).palette { +contextColor(R.color.red_200) - +contextColor(R.color.yellow_200) }.tint() ``` @@ -90,7 +98,7 @@ val drawable = Rainbow(myCheckBox).palette { ``` ### RainbowView -RainbowView is a gradient view for implementing a gradations.

+RainbowView is a gradient view for implementing gradations.

Add following XML namespace inside your XML layout file. ```gradle @@ -128,7 +136,7 @@ Tha `rainbowView_colors` attributes gets color list from the color-array from yo ``` ### BinaryRainbowView -BinaryRainbowView is a gradient view for implementing a simple gradation effect.
+BinaryRainbowView is a gradient view for implementing a simple view with gradations. ```gradle () private var alpha = 255 /** constructs a palette for collecting colors. */ + @RainbowDsl fun palette(block: Rainbow.() -> Unit): Rainbow { val rainbow = Rainbow(view) rainbow.block() @@ -127,7 +134,10 @@ class Rainbow(val view: View) { } /** gets the gradation drawable which composed with palette colors. */ - fun getDrawable(orientation: RainbowOrientation = RainbowOrientation.LEFT_RIGHT, radius: Int = 0): GradientDrawable { + fun getDrawable( + orientation: RainbowOrientation = RainbowOrientation.LEFT_RIGHT, + radius: Int = 0 + ): GradientDrawable { return getGradientDrawable(orientation, radius) } @@ -138,9 +148,7 @@ class Rainbow(val view: View) { return gradient } - private fun emptyColors(): Boolean { - return rainbowColorList.isEmpty() - } + private fun emptyColors(): Boolean = rainbowColorList.isEmpty() private fun getColorStateList(): ColorStateList { val states = mutableListOf() @@ -150,15 +158,9 @@ class Rainbow(val view: View) { return ColorStateList(arrayOf(states.toIntArray()), rainbowColorList.toIntArray()).withAlpha(alpha) } - operator fun ContextColor.unaryPlus() { - rainbowColorList.add(this) - } + operator fun ContextColor.unaryPlus() = rainbowColorList.add(this) - operator fun List.unaryPlus() { - rainbowColorList.addAll(this.toContextColorList()) - } + operator fun List.unaryPlus() = rainbowColorList.addAll(this.toContextColorList()) - operator fun IntArray.unaryPlus() { - rainbowColorList.addAll(this.toContextColorList()) - } + operator fun IntArray.unaryPlus() = rainbowColorList.addAll(this.toContextColorList()) } diff --git a/rainbow/src/main/java/com/skydoves/rainbow/ViewExtension.kt b/rainbow/src/main/java/com/skydoves/rainbow/ViewExtension.kt index 73a6e70..d7ebd95 100644 --- a/rainbow/src/main/java/com/skydoves/rainbow/ViewExtension.kt +++ b/rainbow/src/main/java/com/skydoves/rainbow/ViewExtension.kt @@ -25,11 +25,10 @@ internal const val outRangeColor = 65555 /** dp size to px size. */ internal fun View.dp2Px(dp: Int): Float { - val scale = resources.displayMetrics.density - return dp * scale + val scale = resources.displayMetrics.density + return dp * scale } /** px size to dp size. */ -internal fun View.px2Dp(px: Float): Float { - return px / (context.resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT) -} +internal fun View.px2Dp(px: Float): Float = + px / (context.resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT)