|
|
@ -2,14 +2,10 @@ package io.legado.app.ui.widget.image |
|
|
|
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint |
|
|
|
import android.annotation.SuppressLint |
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
import android.graphics.* |
|
|
|
import android.graphics.Canvas |
|
|
|
|
|
|
|
import android.graphics.Path |
|
|
|
import android.graphics.drawable.Drawable |
|
|
|
import android.graphics.drawable.Drawable |
|
|
|
import android.text.TextPaint |
|
|
|
|
|
|
|
import android.util.AttributeSet |
|
|
|
import android.util.AttributeSet |
|
|
|
import com.bumptech.glide.load.DataSource |
|
|
|
|
|
|
|
import com.bumptech.glide.load.engine.GlideException |
|
|
|
|
|
|
|
import com.bumptech.glide.request.RequestListener |
|
|
|
|
|
|
|
import com.bumptech.glide.request.target.Target |
|
|
|
|
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.R |
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
import io.legado.app.constant.PreferKey |
|
|
|
import io.legado.app.help.AppConfig |
|
|
|
import io.legado.app.help.AppConfig |
|
|
@ -32,27 +28,7 @@ class CoverImageView @JvmOverloads constructor( |
|
|
|
internal var height: Float = 0.toFloat() |
|
|
|
internal var height: Float = 0.toFloat() |
|
|
|
var path: String? = null |
|
|
|
var path: String? = null |
|
|
|
private set |
|
|
|
private set |
|
|
|
private var nameHeight = 0f |
|
|
|
|
|
|
|
private var authorHeight = 0f |
|
|
|
|
|
|
|
private val namePaint by lazy { |
|
|
|
|
|
|
|
val textPaint = TextPaint() |
|
|
|
|
|
|
|
textPaint.typeface = Typeface.DEFAULT_BOLD |
|
|
|
|
|
|
|
textPaint.isAntiAlias = true |
|
|
|
|
|
|
|
textPaint.textAlign = Paint.Align.CENTER |
|
|
|
|
|
|
|
textPaint.textSkewX = -0.2f |
|
|
|
|
|
|
|
textPaint |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private val authorPaint by lazy { |
|
|
|
|
|
|
|
val textPaint = TextPaint() |
|
|
|
|
|
|
|
textPaint.typeface = Typeface.DEFAULT |
|
|
|
|
|
|
|
textPaint.isAntiAlias = true |
|
|
|
|
|
|
|
textPaint.textAlign = Paint.Align.CENTER |
|
|
|
|
|
|
|
textPaint.textSkewX = -0.1f |
|
|
|
|
|
|
|
textPaint |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
private var name: String? = null |
|
|
|
|
|
|
|
private var author: String? = null |
|
|
|
|
|
|
|
private var loadFailed = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { |
|
|
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { |
|
|
|
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) |
|
|
|
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) |
|
|
@ -67,13 +43,6 @@ class CoverImageView @JvmOverloads constructor( |
|
|
|
super.onLayout(changed, left, top, right, bottom) |
|
|
|
super.onLayout(changed, left, top, right, bottom) |
|
|
|
width = getWidth().toFloat() |
|
|
|
width = getWidth().toFloat() |
|
|
|
height = getHeight().toFloat() |
|
|
|
height = getHeight().toFloat() |
|
|
|
namePaint.textSize = width / 6 |
|
|
|
|
|
|
|
namePaint.strokeWidth = namePaint.textSize / 10 |
|
|
|
|
|
|
|
authorPaint.textSize = width / 9 |
|
|
|
|
|
|
|
authorPaint.strokeWidth = authorPaint.textSize / 10 |
|
|
|
|
|
|
|
val fm = namePaint.fontMetrics |
|
|
|
|
|
|
|
nameHeight = height * 0.5f + (fm.bottom - fm.top) * 0.5f |
|
|
|
|
|
|
|
authorHeight = nameHeight + (fm.bottom - fm.top) * 0.6f |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onDraw(canvas: Canvas) { |
|
|
|
override fun onDraw(canvas: Canvas) { |
|
|
@ -94,23 +63,6 @@ class CoverImageView @JvmOverloads constructor( |
|
|
|
canvas.clipPath(path) |
|
|
|
canvas.clipPath(path) |
|
|
|
} |
|
|
|
} |
|
|
|
super.onDraw(canvas) |
|
|
|
super.onDraw(canvas) |
|
|
|
if (!loadFailed || !showBookName) return |
|
|
|
|
|
|
|
name?.let { |
|
|
|
|
|
|
|
namePaint.color = Color.WHITE |
|
|
|
|
|
|
|
namePaint.style = Paint.Style.STROKE |
|
|
|
|
|
|
|
canvas.drawText(it, width / 2, nameHeight, namePaint) |
|
|
|
|
|
|
|
namePaint.color = Color.RED |
|
|
|
|
|
|
|
namePaint.style = Paint.Style.FILL |
|
|
|
|
|
|
|
canvas.drawText(it, width / 2, nameHeight, namePaint) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
author?.let { |
|
|
|
|
|
|
|
authorPaint.color = Color.WHITE |
|
|
|
|
|
|
|
authorPaint.style = Paint.Style.STROKE |
|
|
|
|
|
|
|
canvas.drawText(it, width / 2, authorHeight, authorPaint) |
|
|
|
|
|
|
|
authorPaint.color = Color.RED |
|
|
|
|
|
|
|
authorPaint.style = Paint.Style.FILL |
|
|
|
|
|
|
|
canvas.drawText(it, width / 2, authorHeight, authorPaint) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fun setHeight(height: Int) { |
|
|
|
fun setHeight(height: Int) { |
|
|
@ -118,24 +70,8 @@ class CoverImageView @JvmOverloads constructor( |
|
|
|
minimumWidth = width |
|
|
|
minimumWidth = width |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun setText(name: String?, author: String?) { |
|
|
|
fun load(path: String? = null) { |
|
|
|
this.name = |
|
|
|
|
|
|
|
when { |
|
|
|
|
|
|
|
name == null -> null |
|
|
|
|
|
|
|
name.length > 5 -> name.substring(0, 4) + "…" |
|
|
|
|
|
|
|
else -> name |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this.author = |
|
|
|
|
|
|
|
when { |
|
|
|
|
|
|
|
author == null -> null |
|
|
|
|
|
|
|
author.length > 8 -> author.substring(0, 7) + "…" |
|
|
|
|
|
|
|
else -> author |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun load(path: String? = null, name: String? = null, author: String? = null) { |
|
|
|
|
|
|
|
this.path = path |
|
|
|
this.path = path |
|
|
|
setText(name, author) |
|
|
|
|
|
|
|
if (AppConfig.useDefaultCover) { |
|
|
|
if (AppConfig.useDefaultCover) { |
|
|
|
ImageLoader.load(context, defaultDrawable) |
|
|
|
ImageLoader.load(context, defaultDrawable) |
|
|
|
.centerCrop() |
|
|
|
.centerCrop() |
|
|
@ -144,29 +80,6 @@ class CoverImageView @JvmOverloads constructor( |
|
|
|
ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// |
|
|
|
ImageLoader.load(context, path)//Glide自动识别http://,content://和file:// |
|
|
|
.placeholder(defaultDrawable) |
|
|
|
.placeholder(defaultDrawable) |
|
|
|
.error(defaultDrawable) |
|
|
|
.error(defaultDrawable) |
|
|
|
.listener(object : RequestListener<Drawable> { |
|
|
|
|
|
|
|
override fun onLoadFailed( |
|
|
|
|
|
|
|
e: GlideException?, |
|
|
|
|
|
|
|
model: Any?, |
|
|
|
|
|
|
|
target: Target<Drawable>?, |
|
|
|
|
|
|
|
isFirstResource: Boolean |
|
|
|
|
|
|
|
): Boolean { |
|
|
|
|
|
|
|
loadFailed = true |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun onResourceReady( |
|
|
|
|
|
|
|
resource: Drawable?, |
|
|
|
|
|
|
|
model: Any?, |
|
|
|
|
|
|
|
target: Target<Drawable>?, |
|
|
|
|
|
|
|
dataSource: DataSource?, |
|
|
|
|
|
|
|
isFirstResource: Boolean |
|
|
|
|
|
|
|
): Boolean { |
|
|
|
|
|
|
|
loadFailed = false |
|
|
|
|
|
|
|
return false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.centerCrop() |
|
|
|
.centerCrop() |
|
|
|
.into(this) |
|
|
|
.into(this) |
|
|
|
} |
|
|
|
} |
|
|
|