From da891e122732f767eca8761a6fff9a45da7656ab Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 3 Feb 2020 21:55:41 +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/info/BookInfoActivity.kt | 28 ++++----- .../main/bookshelf/books/BooksAdapterGrid.kt | 9 +-- .../main/bookshelf/books/BooksAdapterList.kt | 9 +-- .../app/ui/widget/image/CoverImageView.kt | 59 +++++++++++++++---- 4 files changed, 62 insertions(+), 43 deletions(-) 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 6f5c23d67..37d8c6d3f 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 @@ -106,11 +106,6 @@ class BookInfoActivity : return super.onMenuOpened(featureId, menu) } - private fun defaultCover(): RequestBuilder { - return ImageLoader.load(this, R.drawable.image_cover_default) - .apply(bitmapTransform(BlurTransformation(this, 25))) - } - private fun showBook(book: Book) { showCover(book) tv_name.text = book.name @@ -152,17 +147,18 @@ class BookInfoActivity : } private fun showCover(book: Book) { - book.getDisplayCover()?.let { - ImageLoader.load(this, it) - .centerCrop() - .into(iv_cover) - ImageLoader.load(this, it) - .transition(DrawableTransitionOptions.withCrossFade(1500)) - .thumbnail(defaultCover()) - .centerCrop() - .apply(bitmapTransform(BlurTransformation(this, 25))) - .into(bg_book) //模糊、渐变、缩小效果 - } + iv_cover.load(book.getDisplayCover(), book.name) + ImageLoader.load(this, book.getDisplayCover()) + .transition(DrawableTransitionOptions.withCrossFade(1500)) + .thumbnail(defaultCover()) + .centerCrop() + .apply(bitmapTransform(BlurTransformation(this, 25))) + .into(bg_book) //模糊、渐变、缩小效果 + } + + private fun defaultCover(): RequestBuilder { + return ImageLoader.load(this, R.drawable.image_cover_default) + .apply(bitmapTransform(BlurTransformation(this, 25))) } private fun showChapter(chapterList: List) { diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt index f6761ce78..5cc19d9fc 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterGrid.kt @@ -5,7 +5,6 @@ import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.constant.BookType import io.legado.app.data.entities.Book -import io.legado.app.help.ImageLoader import io.legado.app.lib.theme.ATH import io.legado.app.utils.invisible import kotlinx.android.synthetic.main.item_bookshelf_grid.view.* @@ -21,13 +20,7 @@ class BooksAdapterGrid(context: Context, private val callBack: CallBack) : ATH.applyBackgroundTint(this) tv_name.text = item.name bv_author.text = item.author - item.getDisplayCover()?.let { - ImageLoader.load(context, it)//Glide自动识别http://和file:// - .placeholder(R.drawable.image_cover_default) - .error(R.drawable.image_cover_default) - .centerCrop() - .into(iv_cover) - } + iv_cover.load(item.getDisplayCover(), item.name) onClick { callBack.open(item) } onLongClick { callBack.openBookInfo(item) diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt index 133f865d9..948b9128e 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksAdapterList.kt @@ -5,7 +5,6 @@ import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.constant.BookType import io.legado.app.data.entities.Book -import io.legado.app.help.ImageLoader import io.legado.app.lib.theme.ATH import io.legado.app.utils.invisible import kotlinx.android.synthetic.main.item_bookshelf_list.view.* @@ -23,13 +22,7 @@ class BooksAdapterList(context: Context, private val callBack: CallBack) : tv_author.text = item.author tv_read.text = item.durChapterTitle tv_last.text = item.latestChapterTitle - item.getDisplayCover()?.let { - ImageLoader.load(context, it)//Glide自动识别http://和file:// - .placeholder(R.drawable.image_cover_default) - .error(R.drawable.image_cover_default) - .centerCrop() - .into(iv_cover) - } + iv_cover.load(item.getDisplayCover(), item.name) onClick { callBack.open(item) } onLongClick { callBack.openBookInfo(item) 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 2db34ef74..8e520d222 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 @@ -4,9 +4,15 @@ import android.annotation.SuppressLint import android.content.Context import android.graphics.Canvas import android.graphics.Path +import android.graphics.drawable.Drawable import android.text.TextPaint import android.util.AttributeSet -import io.legado.app.utils.dp +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.help.ImageLoader class CoverImageView : androidx.appcompat.widget.AppCompatImageView { @@ -26,16 +32,6 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView { defStyleAttr ) - init { - textPaint.textSize = 13.dp.toFloat() - } - - override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { - super.onLayout(changed, left, top, right, bottom) - width = getWidth().toFloat() - height = getHeight().toFloat() - } - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) val measuredHeight = measuredWidth * 7 / 5 @@ -45,6 +41,13 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView { ) } + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { + super.onLayout(changed, left, top, right, bottom) + width = getWidth().toFloat() + height = getHeight().toFloat() + textPaint.textSize = width / 9 + } + override fun onDraw(canvas: Canvas) { if (width >= 10 && height > 10) { @SuppressLint("DrawAllocation") @@ -77,4 +80,38 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView { val width = height * 5 / 7 minimumWidth = width } + + fun load(path: String?, name: String) { + if (path.isNullOrEmpty()) { + setName(name) + } else { + ImageLoader.load(context, path)//Glide自动识别http://和file:// + .placeholder(R.drawable.image_cover_default) + .error(R.drawable.image_cover_default) + .listener(object : RequestListener { + override fun onLoadFailed( + e: GlideException?, + model: Any?, + target: Target?, + isFirstResource: Boolean + ): Boolean { + setName(name) + return false + } + + override fun onResourceReady( + resource: Drawable?, + model: Any?, + target: Target?, + dataSource: DataSource?, + isFirstResource: Boolean + ): Boolean { + return false + } + + }) + .centerCrop() + .into(this) + } + } }