pull/32/head
GKF 6 years ago
parent ab951e3d2b
commit f917e2f3d5
  1. 79
      app/src/main/java/io/legado/app/lib/theme/ColorUtils.kt
  2. 4
      app/src/main/java/io/legado/app/ui/main/findbook/FindBookAdapter.kt
  3. 8
      app/src/main/res/drawable/shape_radius_10dp.xml
  4. 4
      app/src/main/res/layout/item_find_book.xml

@ -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
}
}
}

@ -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<BookSource, FindBookAdapter.MyViewHolder>(DIFF_CALLBACK) {
@ -36,6 +38,8 @@ class FindBookAdapter:PagedListAdapter<BookSource, FindBookAdapter.MyViewHolder>
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
}
}

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="16dp" />
<solid android:color="#00ffffff" />
<stroke
android:width="3dp"
android:color="@color/tv_text_secondary" />
</shape>

@ -1,8 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_name"
android:background="@drawable/shape_radius_10dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_margin="5dp"
android:gravity="center"
android:padding="5dp" />
Loading…
Cancel
Save