From f917e2f3d542f4140193c8e4ce477c1a33e7dbcc Mon Sep 17 00:00:00 2001 From: GKF Date: Thu, 20 Jun 2019 15:55:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/lib/theme/ColorUtils.kt | 79 +++++++++++++++++++ .../app/ui/main/findbook/FindBookAdapter.kt | 4 + .../main/res/drawable/shape_radius_10dp.xml | 8 ++ app/src/main/res/layout/item_find_book.xml | 4 +- 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/shape_radius_10dp.xml diff --git a/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt b/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt index ba0289637..5d76e6e4e 100644 --- a/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt +++ b/app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt @@ -3,6 +3,7 @@ package io.legado.app.lib.theme import android.graphics.Color import androidx.annotation.ColorInt import androidx.annotation.FloatRange +import java.util.* object ColorUtils { @@ -76,4 +77,82 @@ object ColorUtils { return Color.argb(a.toInt(), r.toInt(), g.toInt(), b.toInt()) } + /** + * 按条件的到随机颜色 + * + * @param alpha 透明 + * @param lower 下边界 + * @param upper 上边界 + * @return 颜色值 + */ + fun getRandomColor(alpha: Int, lower: Int, upper: Int): Int { + return RandomColor(alpha, lower, upper).color + } + + /** + * @return 获取随机色 + */ + fun getRandomColor(): Int { + return RandomColor(255, 80, 200).color + } + + + /** + * 随机颜色 + */ + class RandomColor(alpha: Int, lower: Int, upper: Int) { + internal var alpha: Int = 0 + internal var lower: Int = 0 + internal var upper: Int = 0 + + //随机数是前闭 后开 + val color: Int + get() { + val red = getLower() + Random().nextInt(getUpper() - getLower() + 1) + val green = getLower() + Random().nextInt(getUpper() - getLower() + 1) + val blue = getLower() + Random().nextInt(getUpper() - getLower() + 1) + + return Color.argb(getAlpha(), red, green, blue) + } + + init { + if (upper <= lower) { + throw IllegalArgumentException("must be lower < upper") + } + setAlpha(alpha) + setLower(lower) + setUpper(upper) + } + + fun getAlpha(): Int { + return alpha + } + + fun setAlpha(alpha: Int) { + var alpha = alpha + if (alpha > 255) alpha = 255 + if (alpha < 0) alpha = 0 + this.alpha = alpha + } + + fun getLower(): Int { + return lower + } + + fun setLower(lower: Int) { + var lower = lower + if (lower < 0) lower = 0 + this.lower = lower + } + + fun getUpper(): Int { + return upper + } + + fun setUpper(upper: Int) { + var upper = upper + if (upper > 255) upper = 255 + this.upper = upper + } + } } diff --git a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt index 59bca7a84..eab962729 100644 --- a/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt @@ -1,5 +1,6 @@ package io.legado.app.ui.main.findbook +import android.graphics.drawable.GradientDrawable import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -8,6 +9,7 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.RecyclerView import io.legado.app.R import io.legado.app.data.entities.BookSource +import io.legado.app.lib.theme.ColorUtils import kotlinx.android.synthetic.main.item_find_book.view.* class FindBookAdapter:PagedListAdapter(DIFF_CALLBACK) { @@ -36,6 +38,8 @@ class FindBookAdapter:PagedListAdapter class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { fun bind(bookSource: BookSource) = with(itemView) { + val bgShape: GradientDrawable? = tv_name.background as? GradientDrawable + bgShape?.setStroke(1, ColorUtils.getRandomColor()) tv_name.text = bookSource.name } } diff --git a/app/src/main/res/drawable/shape_radius_10dp.xml b/app/src/main/res/drawable/shape_radius_10dp.xml new file mode 100644 index 000000000..0d0161412 --- /dev/null +++ b/app/src/main/res/drawable/shape_radius_10dp.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_find_book.xml b/app/src/main/res/layout/item_find_book.xml index c71cc089d..9ed16650c 100644 --- a/app/src/main/res/layout/item_find_book.xml +++ b/app/src/main/res/layout/item_find_book.xml @@ -1,8 +1,10 @@ \ No newline at end of file