Fix CoverImageView image display size issue #4902
This commit is contained in:
@@ -9,6 +9,8 @@ import android.graphics.Typeface
|
|||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
import android.text.TextPaint
|
import android.text.TextPaint
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
|
import android.view.ViewGroup
|
||||||
|
import androidx.appcompat.widget.AppCompatImageView
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.Lifecycle
|
import androidx.lifecycle.Lifecycle
|
||||||
import com.bumptech.glide.load.DataSource
|
import com.bumptech.glide.load.DataSource
|
||||||
@@ -32,13 +34,10 @@ import io.legado.app.utils.toStringArray
|
|||||||
class CoverImageView @JvmOverloads constructor(
|
class CoverImageView @JvmOverloads constructor(
|
||||||
context: Context,
|
context: Context,
|
||||||
attrs: AttributeSet? = null
|
attrs: AttributeSet? = null
|
||||||
) : androidx.appcompat.widget.AppCompatImageView(
|
) : AppCompatImageView(context, attrs) {
|
||||||
context,
|
|
||||||
attrs
|
|
||||||
) {
|
|
||||||
private var filletPath = Path()
|
private var filletPath = Path()
|
||||||
private var width: Float = 0.toFloat()
|
private var viewWidth: Float = 0f
|
||||||
private var height: Float = 0.toFloat()
|
private var viewHeight: Float = 0f
|
||||||
private var defaultCover = true
|
private var defaultCover = true
|
||||||
var bitmapPath: String? = null
|
var bitmapPath: String? = null
|
||||||
private set
|
private set
|
||||||
@@ -61,6 +60,18 @@ class CoverImageView @JvmOverloads constructor(
|
|||||||
textPaint
|
textPaint
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun setLayoutParams(params: ViewGroup.LayoutParams?) {
|
||||||
|
if (params != null) {
|
||||||
|
val width = params.width
|
||||||
|
if (width >= 0) {
|
||||||
|
params.height = width * 7 / 5
|
||||||
|
} else {
|
||||||
|
params.height = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||||
|
}
|
||||||
|
}
|
||||||
|
super.setLayoutParams(params)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
||||||
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec)
|
val measuredWidth = MeasureSpec.getSize(widthMeasureSpec)
|
||||||
val measuredHeight = measuredWidth * 7 / 5
|
val measuredHeight = measuredWidth * 7 / 5
|
||||||
@@ -72,18 +83,18 @@ class CoverImageView @JvmOverloads constructor(
|
|||||||
|
|
||||||
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
|
||||||
super.onLayout(changed, left, top, right, bottom)
|
super.onLayout(changed, left, top, right, bottom)
|
||||||
width = getWidth().toFloat()
|
viewWidth = width.toFloat()
|
||||||
height = getHeight().toFloat()
|
viewHeight = height.toFloat()
|
||||||
filletPath.reset()
|
filletPath.reset()
|
||||||
if (width > 10 && height > 10) {
|
if (width > 10 && viewHeight > 10) {
|
||||||
filletPath.apply {
|
filletPath.apply {
|
||||||
moveTo(10f, 0f)
|
moveTo(10f, 0f)
|
||||||
lineTo(width - 10, 0f)
|
lineTo(viewWidth - 10, 0f)
|
||||||
quadTo(width, 0f, width, 10f)
|
quadTo(viewWidth, 0f, viewWidth, 10f)
|
||||||
lineTo(width, height - 10)
|
lineTo(viewWidth, viewHeight - 10)
|
||||||
quadTo(width, height, width - 10, height)
|
quadTo(viewWidth, viewHeight, viewWidth - 10, viewHeight)
|
||||||
lineTo(10f, height)
|
lineTo(10f, viewHeight)
|
||||||
quadTo(0f, height, 0f, height - 10)
|
quadTo(0f, viewHeight, 0f, viewHeight - 10)
|
||||||
lineTo(0f, 10f)
|
lineTo(0f, 10f)
|
||||||
quadTo(0f, 0f, 10f, 0f)
|
quadTo(0f, 0f, 10f, 0f)
|
||||||
close()
|
close()
|
||||||
@@ -104,9 +115,9 @@ class CoverImageView @JvmOverloads constructor(
|
|||||||
private fun drawNameAuthor(canvas: Canvas) {
|
private fun drawNameAuthor(canvas: Canvas) {
|
||||||
if (!BookCover.drawBookName) return
|
if (!BookCover.drawBookName) return
|
||||||
var startX = width * 0.2f
|
var startX = width * 0.2f
|
||||||
var startY = height * 0.2f
|
var startY = viewHeight * 0.2f
|
||||||
name?.toStringArray()?.let { name ->
|
name?.toStringArray()?.let { name ->
|
||||||
namePaint.textSize = width / 6
|
namePaint.textSize = viewWidth / 6
|
||||||
namePaint.strokeWidth = namePaint.textSize / 5
|
namePaint.strokeWidth = namePaint.textSize / 5
|
||||||
name.forEachIndexed { index, char ->
|
name.forEachIndexed { index, char ->
|
||||||
namePaint.color = Color.WHITE
|
namePaint.color = Color.WHITE
|
||||||
@@ -116,20 +127,20 @@ class CoverImageView @JvmOverloads constructor(
|
|||||||
namePaint.style = Paint.Style.FILL
|
namePaint.style = Paint.Style.FILL
|
||||||
canvas.drawText(char, startX, startY, namePaint)
|
canvas.drawText(char, startX, startY, namePaint)
|
||||||
startY += namePaint.textHeight
|
startY += namePaint.textHeight
|
||||||
if (startY > height * 0.8) {
|
if (startY > viewHeight * 0.8) {
|
||||||
startX += namePaint.textSize
|
startX += namePaint.textSize
|
||||||
namePaint.textSize = width / 10
|
namePaint.textSize = viewWidth / 10
|
||||||
startY = (height - (name.size - index - 1) * namePaint.textHeight) / 2
|
startY = (viewHeight - (name.size - index - 1) * namePaint.textHeight) / 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!BookCover.drawBookAuthor) return
|
if (!BookCover.drawBookAuthor) return
|
||||||
author?.toStringArray()?.let { author ->
|
author?.toStringArray()?.let { author ->
|
||||||
authorPaint.textSize = width / 10
|
authorPaint.textSize = viewWidth / 10
|
||||||
authorPaint.strokeWidth = authorPaint.textSize / 5
|
authorPaint.strokeWidth = authorPaint.textSize / 5
|
||||||
startX = width * 0.8f
|
startX = width * 0.8f
|
||||||
startY = height * 0.95f - author.size * authorPaint.textHeight
|
startY = viewHeight * 0.95f - author.size * authorPaint.textHeight
|
||||||
startY = maxOf(startY, height * 0.3f)
|
startY = maxOf(startY, viewHeight * 0.3f)
|
||||||
author.forEach {
|
author.forEach {
|
||||||
authorPaint.color = Color.WHITE
|
authorPaint.color = Color.WHITE
|
||||||
authorPaint.style = Paint.Style.STROKE
|
authorPaint.style = Paint.Style.STROKE
|
||||||
@@ -138,7 +149,7 @@ class CoverImageView @JvmOverloads constructor(
|
|||||||
authorPaint.style = Paint.Style.FILL
|
authorPaint.style = Paint.Style.FILL
|
||||||
canvas.drawText(it, startX, startY, authorPaint)
|
canvas.drawText(it, startX, startY, authorPaint)
|
||||||
startY += authorPaint.textHeight
|
startY += authorPaint.textHeight
|
||||||
if (startY > height * 0.95) {
|
if (startY > viewHeight * 0.95) {
|
||||||
return@let
|
return@let
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user