Files
assistant-android/app/src/main/java/com/gh/common/view/DrawableView.kt
2022-03-23 17:31:59 +08:00

125 lines
5.1 KiB
Kotlin

package com.gh.common.view
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.ShapeDrawable
import android.graphics.drawable.StateListDrawable
import android.graphics.drawable.shapes.RectShape
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import com.gh.common.util.DisplayUtils
import com.gh.common.util.dip2px
import com.halo.assistant.HaloApp
object DrawableView {
@JvmStatic
fun getServerDrawableBySource(@ColorRes colorId: Int): Drawable {
return getServerDrawable(ContextCompat.getColor(HaloApp.getInstance().application, colorId))
}
@JvmStatic
fun getServerDrawable(colorCode: String): Drawable {
return getServerDrawable(Color.parseColor(colorCode))
}
@JvmStatic
fun getServerDrawable(colorCode: Int): Drawable {
val drawable = GradientDrawable()
drawable.setColor(colorCode)
drawable.cornerRadius = DisplayUtils.dip2px(2F).toFloat()
return drawable
}
@JvmStatic
fun getOvalDrawable(@ColorRes colorId: Int, radius: Float = 999F): Drawable {
val drawable = GradientDrawable()
drawable.setColor(ContextCompat.getColor(HaloApp.getInstance().application, colorId))
drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
return drawable
}
@JvmStatic
fun getOvalSelectorStyle(@ColorRes defaultColorId: Int, @ColorRes checkColorId: Int): StateListDrawable {
val res = StateListDrawable()
res.addState(intArrayOf(android.R.attr.state_checked), getOvalDrawable(checkColorId))
res.addState(intArrayOf(), getOvalDrawable(defaultColorId))
return res
}
@JvmStatic
fun getSelectorColorStyle(@ColorRes defaultColorId: Int, @ColorRes checkColorId: Int): ColorStateList {
val states = arrayOf(
intArrayOf(-android.R.attr.state_checked),
intArrayOf(android.R.attr.state_checked))
val colors = intArrayOf(
ContextCompat.getColor(HaloApp.getInstance().application, defaultColorId),
ContextCompat.getColor(HaloApp.getInstance().application, checkColorId))
return ColorStateList(states, colors)
}
@JvmStatic
fun getStrokeDrawable(@ColorRes colorCode: Int, strokeWidth: Float = 1F, radius: Float = 999F): Drawable {
val drawable = GradientDrawable()
drawable.setStroke(strokeWidth.dip2px(), ContextCompat.getColor(HaloApp.getInstance().application, colorCode))
drawable.cornerRadius = radius
return drawable
}
@JvmStatic
fun setTextDrawable(textView: TextView, @DrawableRes drawableId: Int?, text: String? = null) {
val drawable = if (drawableId != null) {
ContextCompat.getDrawable(HaloApp.getInstance().application, drawableId)
} else {
null
}
drawable?.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
textView.setCompoundDrawables(drawable, null, null, null)
if (text != null) textView.text = text
}
// 需要跳转角度请自行设置 x0,y0,x1,y1
@JvmStatic
fun getGradientDrawable(width: Int, startColor: Int, endColor: Int): Drawable {
val drawable = ShapeDrawable(RectShape())
drawable.paint.shader = LinearGradient(0f, 0F, width.toFloat(), 0F, startColor, endColor, Shader.TileMode.REPEAT)
return drawable
}
@JvmStatic
fun getCornerDrawable(color: Int, radiusLT: Float, radiusRT: Float, radiusRB: Float, radiusLB: Float):Drawable{
val drawable = GradientDrawable()
drawable.shape = GradientDrawable.RECTANGLE
drawable.setColor(color)
val radiusEach = floatArrayOf(radiusLT.dip2px().toFloat(), radiusLT.dip2px().toFloat(), radiusRT.dip2px().toFloat(), radiusRT.dip2px().toFloat(),
radiusRB.dip2px().toFloat(), radiusRB.dip2px().toFloat(), radiusLB.dip2px().toFloat(), radiusLB.dip2px().toFloat())
drawable.cornerRadii = radiusEach
return drawable
}
@JvmStatic
fun getCornerGradientDrawable(startColor: Int, endColor: Int, radius: Float = 999F): Drawable {
val drawable = GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, intArrayOf(startColor, endColor))
drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
return drawable
}
@JvmStatic
fun getGradientDrawable(startColor: Int, endColor: Int, orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.LEFT_RIGHT, radius: Float = 999F): Drawable {
val drawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
drawable.cornerRadius = DisplayUtils.dip2px(radius).toFloat()
return drawable
}
fun convertAlphaKey(percent: Int): String {
val hexString = Integer.toHexString(Math.round((255 * percent / 100).toFloat()))
return (if (hexString.length < 2) "0" else "") + hexString
}
}