From c89f2fd7e031ac5d33464fcf1b73f4a601a66247 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 19 Sep 2021 11:41:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/changecover/CoverAdapter.kt | 2 +- .../app/ui/book/explore/ExploreShowAdapter.kt | 2 +- .../app/ui/book/group/GroupEditDialog.kt | 4 +- .../app/ui/book/info/BookInfoActivity.kt | 2 +- .../ui/book/info/edit/BookInfoEditActivity.kt | 2 +- .../app/ui/book/search/SearchAdapter.kt | 4 +- .../style1/books/BooksAdapterGrid.kt | 4 +- .../style1/books/BooksAdapterList.kt | 2 +- .../main/bookshelf/style2/BooksAdapterGrid.kt | 4 +- .../main/bookshelf/style2/BooksAdapterList.kt | 4 +- .../app/ui/widget/image/CoverImageView.kt | 106 +++++++++++++----- 11 files changed, 95 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt b/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt index c8c546e82..323dbfc05 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/CoverAdapter.kt @@ -35,7 +35,7 @@ class CoverAdapter(context: Context, val callBack: CallBack) : item: SearchBook, payloads: MutableList ) = binding.run { - ivCover.load(item.coverUrl) + ivCover.load(item.coverUrl, item.name, item.author) tvSource.text = item.originName } diff --git a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowAdapter.kt b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowAdapter.kt index d4f7cc10f..99d6b9285 100644 --- a/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/explore/ExploreShowAdapter.kt @@ -42,7 +42,7 @@ class ExploreShowAdapter(context: Context, val callBack: CallBack) : llKind.visible() llKind.setLabels(kinds) } - ivCover.load(item.coverUrl) + ivCover.load(item.coverUrl, item.name, item.author) } } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt index 96f6cd026..c9ca840d7 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupEditDialog.kt @@ -77,12 +77,12 @@ class GroupEditDialog() : BaseDialogFragment() { } else { bookGroup?.let { it.groupName = groupName - it.cover = binding.ivCover.path + it.cover = binding.ivCover.bitmapPath viewModel.upGroup(it) { dismiss() } } ?: let { - viewModel.addGroup(groupName, binding.ivCover.path) { + viewModel.addGroup(groupName, binding.ivCover.bitmapPath) { dismiss() } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index 66cf3eaa3..bab99579a 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -207,7 +207,7 @@ class BookInfoActivity : } private fun showCover(book: Book) { - binding.ivCover.load(book.getDisplayCover()) + binding.ivCover.load(book.getDisplayCover(), book.name, book.author) ImageLoader.load(this, book.getDisplayCover()) .transition(DrawableTransitionOptions.withCrossFade(1500)) .thumbnail(defaultCover()) diff --git a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt index fee68dc2f..ea36d9c4f 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt @@ -76,7 +76,7 @@ class BookInfoEditActivity : private fun upCover() { viewModel.book.let { - binding.ivCover.load(it?.getDisplayCover()) + binding.ivCover.load(it?.getDisplayCover(), it?.name, it?.author) } } diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt index c54d293ec..469b71ada 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchAdapter.kt @@ -81,7 +81,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : upLasted(binding, searchBook.latestChapterTitle) tvIntroduce.text = searchBook.trimIntro(context) upKind(binding, searchBook.getKindList()) - ivCover.load(searchBook.coverUrl) + ivCover.load(searchBook.coverUrl, searchBook.name, searchBook.author) } } @@ -94,7 +94,7 @@ class SearchAdapter(context: Context, val callBack: CallBack) : "last" -> upLasted(binding, searchBook.latestChapterTitle) "intro" -> tvIntroduce.text = searchBook.trimIntro(context) "kind" -> upKind(binding, searchBook.getKindList()) - "cover" -> ivCover.load(searchBook.coverUrl) + "cover" -> ivCover.load(searchBook.coverUrl, searchBook.name, searchBook.author) } } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterGrid.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterGrid.kt index eedcbbfac..b029e5eed 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterGrid.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterGrid.kt @@ -27,13 +27,13 @@ class BooksAdapterGrid(context: Context, private val callBack: CallBack) : val bundle = payloads.getOrNull(0) as? Bundle if (bundle == null) { tvName.text = item.name - ivCover.load(item.getDisplayCover()) + ivCover.load(item.getDisplayCover(), item.name, item.author) upRefresh(binding, item) } else { bundle.keySet().forEach { when (it) { "name" -> tvName.text = item.name - "cover" -> ivCover.load(item.getDisplayCover()) + "cover" -> ivCover.load(item.getDisplayCover(), item.name, item.author) "refresh" -> upRefresh(binding, item) } } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterList.kt index 39604b9dd..453cefbd0 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksAdapterList.kt @@ -30,7 +30,7 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) : tvAuthor.text = item.author tvRead.text = item.durChapterTitle tvLast.text = item.latestChapterTitle - ivCover.load(item.getDisplayCover()) + ivCover.load(item.getDisplayCover(), item.name, item.author) upRefresh(binding, item) } else { tvRead.text = item.durChapterTitle diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt index 664bc4d21..9166759b0 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterGrid.kt @@ -71,7 +71,7 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) : bundle.keySet().forEach { when (it) { "name" -> tvName.text = item.name - "cover" -> ivCover.load(item.getDisplayCover()) + "cover" -> ivCover.load(item.getDisplayCover(), item.name, item.author) "refresh" -> upRefresh(this, item) } } @@ -106,7 +106,7 @@ class BooksAdapterGrid(context: Context, callBack: CallBack) : val item = callBack.getItem(position) if (item is Book) { tvName.text = item.name - ivCover.load(item.getDisplayCover()) + ivCover.load(item.getDisplayCover(), item.name, item.author) upRefresh(this, item) } root.setOnClickListener { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt index 95d673c50..c2a700ab1 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BooksAdapterList.kt @@ -72,7 +72,7 @@ class BooksAdapterList(context: Context, callBack: CallBack) : when (it) { "name" -> tvName.text = item.name "author" -> tvAuthor.text = item.author - "cover" -> ivCover.load(item.getDisplayCover()) + "cover" -> ivCover.load(item.getDisplayCover(), item.name, item.author) "refresh" -> upRefresh(this, item) } } @@ -117,7 +117,7 @@ class BooksAdapterList(context: Context, callBack: CallBack) : tvAuthor.text = item.author tvRead.text = item.durChapterTitle tvLast.text = item.latestChapterTitle - ivCover.load(item.getDisplayCover()) + ivCover.load(item.getDisplayCover(), item.name, item.author) flHasNew.visible() ivAuthor.visible() ivLast.visible() diff --git a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt index f26848d0e..1716e2dc9 100644 --- a/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt @@ -2,9 +2,9 @@ package io.legado.app.ui.widget.image import android.annotation.SuppressLint import android.content.Context -import android.graphics.Canvas -import android.graphics.Path +import android.graphics.* import android.graphics.drawable.Drawable +import android.text.TextPaint import android.util.AttributeSet import com.bumptech.glide.load.DataSource import com.bumptech.glide.load.engine.GlideException @@ -29,11 +29,32 @@ class CoverImageView @JvmOverloads constructor( context, attrs ) { - internal var width: Float = 0.toFloat() - internal var height: Float = 0.toFloat() - var path: String? = null - private set + private var filletPath = Path() + private var width: Float = 0.toFloat() + private var height: Float = 0.toFloat() private var defaultCover = true + var bitmapPath: String? = null + private set + private var name: String? = null + private var author: String? = null + 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 + } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) @@ -48,26 +69,57 @@ class CoverImageView @JvmOverloads constructor( super.onLayout(changed, left, top, right, bottom) width = getWidth().toFloat() height = getHeight().toFloat() + filletPath.reset() + if (width > 10 && height > 10) { + filletPath.apply { + moveTo(10f, 0f) + lineTo(width - 10, 0f) + quadTo(width, 0f, width, 10f) + lineTo(width, height - 10) + quadTo(width, height, width - 10, height) + lineTo(10f, height) + quadTo(0f, height, 0f, height - 10) + lineTo(0f, 10f) + quadTo(0f, 0f, 10f, 0f) + close() + } + } + 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) { - if (width >= 10 && height > 10) { - @SuppressLint("DrawAllocation") - val path = Path() - //四个圆角 - path.moveTo(10f, 0f) - path.lineTo(width - 10, 0f) - path.quadTo(width, 0f, width, 10f) - path.lineTo(width, height - 10) - path.quadTo(width, height, width - 10, height) - path.lineTo(10f, height) - path.quadTo(0f, height, 0f, height - 10) - path.lineTo(0f, 10f) - path.quadTo(0f, 0f, 10f, 0f) - - canvas.clipPath(path) + if (!filletPath.isEmpty) { + canvas.clipPath(filletPath) } super.onDraw(canvas) + if (defaultCover && drawBookName) { + drawName(canvas) + } + } + + private fun drawName(canvas: Canvas) { + 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) { @@ -102,8 +154,10 @@ class CoverImageView @JvmOverloads constructor( } } - fun load(path: String? = null) { - this.path = path + fun load(path: String? = null, name: String? = null, author: String? = null) { + this.bitmapPath = path + this.name = name + this.author = author if (AppConfig.useDefaultCover) { ImageLoader.load(context, defaultDrawable) .centerCrop() @@ -119,7 +173,7 @@ class CoverImageView @JvmOverloads constructor( } companion object { - private var showBookName = true + private var drawBookName = true lateinit var defaultDrawable: Drawable init { @@ -134,10 +188,10 @@ class CoverImageView @JvmOverloads constructor( defaultDrawable = Drawable.createFromPath(path)?.let { val showNameKey = if (isNightTheme) PreferKey.defaultCoverDarkShowName else PreferKey.defaultCoverShowName - showBookName = appCtx.getPrefBoolean(showNameKey) + drawBookName = appCtx.getPrefBoolean(showNameKey) return@let it } ?: let { - showBookName = true + drawBookName = true return@let appCtx.resources.getDrawable(R.drawable.image_cover_default, null) } }