feat: 优化代码

pull/379/head
kunfei 5 years ago
parent 455db2ccfa
commit 1afb68b233
  1. 2
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt
  2. 7
      app/src/main/java/io/legado/app/utils/ColorUtils.kt
  3. 5
      app/src/main/java/io/legado/app/utils/FloatExtensions.kt
  4. 2
      app/src/main/java/io/legado/app/utils/StringExtensions.kt

@ -147,7 +147,7 @@ object BitmapUtils {
//图片不被压缩 //图片不被压缩
fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap { fun convertViewToBitmap(view: View, bitmapWidth: Int, bitmapHeight: Int): Bitmap {
val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888) val bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Config.ARGB_8888)
view.draw(Canvas(bitmap)) view.draw(Canvas(bitmap))
return bitmap return bitmap
} }

@ -4,6 +4,9 @@ import android.graphics.Color
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.annotation.FloatRange import androidx.annotation.FloatRange
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
@Suppress("unused") @Suppress("unused")
object ColorUtils { object ColorUtils {
@ -53,7 +56,7 @@ object ColorUtils {
@ColorInt @ColorInt
fun adjustAlpha(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) factor: Float): Int { fun adjustAlpha(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) factor: Float): Int {
val alpha = Math.round(Color.alpha(color) * factor) val alpha = (Color.alpha(color) * factor).roundToInt()
val red = Color.red(color) val red = Color.red(color)
val green = Color.green(color) val green = Color.green(color)
val blue = Color.blue(color) val blue = Color.blue(color)
@ -62,7 +65,7 @@ object ColorUtils {
@ColorInt @ColorInt
fun withAlpha(@ColorInt baseColor: Int, @FloatRange(from = 0.0, to = 1.0) alpha: Float): Int { fun withAlpha(@ColorInt baseColor: Int, @FloatRange(from = 0.0, to = 1.0) alpha: Float): Int {
val a = Math.min(255, Math.max(0, (alpha * 255).toInt())) shl 24 val a = min(255, max(0, (alpha * 255).toInt())) shl 24
val rgb = 0x00ffffff and baseColor val rgb = 0x00ffffff and baseColor
return a + rgb return a + rgb
} }

@ -2,14 +2,13 @@ package io.legado.app.utils
import android.content.res.Resources import android.content.res.Resources
val Float.dp: Float
val Float.dp: Float // [xxhdpi](360 -> 1080)
get() = android.util.TypedValue.applyDimension( get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics android.util.TypedValue.COMPLEX_UNIT_DIP, this, Resources.getSystem().displayMetrics
) )
val Float.sp: Float // [xxhdpi](360 -> 1080) val Float.sp: Float
get() = android.util.TypedValue.applyDimension( get() = android.util.TypedValue.applyDimension(
android.util.TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics android.util.TypedValue.COMPLEX_UNIT_SP, this, Resources.getSystem().displayMetrics
) )

@ -1,7 +1,5 @@
package io.legado.app.utils package io.legado.app.utils
// import org.apache.commons.text.StringEscapeUtils
fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim() fun String?.safeTrim() = if (this.isNullOrBlank()) null else this.trim()
fun String?.isContentPath(): Boolean = this?.startsWith("content://") == true fun String?.isContentPath(): Boolean = this?.startsWith("content://") == true

Loading…
Cancel
Save