From d8e2e36c23c1aa70d8da6be7fcdf6d338a54ca83 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 13 Feb 2020 10:01:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/read/config/ReadStyleDialog.kt | 30 +++--- .../app/ui/widget/image/CircleImageView.kt | 101 +++++++++++++++--- .../app/ui/widget/image/CoverImageView.kt | 34 +++--- .../res/layout/dialog_read_book_style.xml | 50 +-------- app/src/main/res/values/attrs.xml | 2 + 5 files changed, 128 insertions(+), 89 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt index a5cd694e7..b7279da4a 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt @@ -140,16 +140,16 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack { } } } - tv_bg0.onClick { changeBg(0) } - tv_bg0.onLongClick { showBgTextConfig(0) } - tv_bg1.onClick { changeBg(1) } - tv_bg1.onLongClick { showBgTextConfig(1) } - tv_bg2.onClick { changeBg(2) } - tv_bg2.onLongClick { showBgTextConfig(2) } - tv_bg3.onClick { changeBg(3) } - tv_bg3.onLongClick { showBgTextConfig(3) } - tv_bg4.onClick { changeBg(4) } - tv_bg4.onLongClick { showBgTextConfig(4) } + bg0.onClick { changeBg(0) } + bg0.onLongClick { showBgTextConfig(0) } + bg1.onClick { changeBg(1) } + bg1.onLongClick { showBgTextConfig(1) } + bg2.onClick { changeBg(2) } + bg2.onLongClick { showBgTextConfig(2) } + bg3.onClick { changeBg(3) } + bg3.onLongClick { showBgTextConfig(3) } + bg4.onClick { changeBg(4) } + bg4.onLongClick { showBgTextConfig(4) } } private fun changeBg(index: Int) { @@ -182,11 +182,11 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack { } private fun setBg() { - tv_bg0.setTextColor(ReadBookConfig.getConfig(0).textColor()) - tv_bg1.setTextColor(ReadBookConfig.getConfig(1).textColor()) - tv_bg2.setTextColor(ReadBookConfig.getConfig(2).textColor()) - tv_bg3.setTextColor(ReadBookConfig.getConfig(3).textColor()) - tv_bg4.setTextColor(ReadBookConfig.getConfig(4).textColor()) + bg0.setTextColor(ReadBookConfig.getConfig(0).textColor()) + bg1.setTextColor(ReadBookConfig.getConfig(1).textColor()) + bg2.setTextColor(ReadBookConfig.getConfig(2).textColor()) + bg3.setTextColor(ReadBookConfig.getConfig(3).textColor()) + bg4.setTextColor(ReadBookConfig.getConfig(4).textColor()) for (i in 0..4) { val iv = when (i) { 1 -> bg1 diff --git a/app/src/main/java/io/legado/app/ui/widget/image/CircleImageView.kt b/app/src/main/java/io/legado/app/ui/widget/image/CircleImageView.kt index 4a7eb307c..253c1bef3 100644 --- a/app/src/main/java/io/legado/app/ui/widget/image/CircleImageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/image/CircleImageView.kt @@ -8,6 +8,7 @@ import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.net.Uri import android.os.Build +import android.text.TextPaint import android.util.AttributeSet import android.view.MotionEvent import android.view.View @@ -19,6 +20,7 @@ import androidx.annotation.RequiresApi import androidx.appcompat.widget.AppCompatImageView import io.legado.app.R import io.legado.app.utils.getCompatColor +import io.legado.app.utils.sp import kotlin.math.min import kotlin.math.pow @@ -31,6 +33,12 @@ class CircleImageView : AppCompatImageView { private val mBitmapPaint = Paint() private val mBorderPaint = Paint() private val mCircleBackgroundPaint = Paint() + private val textPaint by lazy { + val textPaint = TextPaint() + textPaint.isAntiAlias = true + textPaint.textAlign = Paint.Align.CENTER + textPaint + } private var mBorderColor = DEFAULT_BORDER_COLOR private var mBorderWidth = DEFAULT_BORDER_WIDTH @@ -105,22 +113,43 @@ class CircleImageView : AppCompatImageView { setup() } - constructor(context: Context) : super(context) { + private var text: String? = null + private var textColor = context.getCompatColor(R.color.tv_text_default) + + constructor(context: Context) : super(context) { init() } @JvmOverloads - constructor(context: Context, attrs: AttributeSet, defStyle: Int = 0) : super(context, attrs, defStyle) { + constructor(context: Context, attrs: AttributeSet, defStyle: Int = 0) : super( + context, + attrs, + defStyle + ) { val a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0) - mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_civ_border_width, DEFAULT_BORDER_WIDTH) - mBorderColor = a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR) - mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY) + mBorderWidth = a.getDimensionPixelSize( + R.styleable.CircleImageView_civ_border_width, + DEFAULT_BORDER_WIDTH + ) + mBorderColor = + a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR) + mBorderOverlay = + a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY) mCircleBackgroundColor = - a.getColor(R.styleable.CircleImageView_civ_circle_background_color, DEFAULT_CIRCLE_BACKGROUND_COLOR) - + a.getColor( + R.styleable.CircleImageView_civ_circle_background_color, + DEFAULT_CIRCLE_BACKGROUND_COLOR + ) + text = a.getString(R.styleable.CircleImageView_text) + if (a.hasValue(R.styleable.CircleImageView_textColor)) { + textColor = a.getColor( + R.styleable.CircleImageView_textColor, + context.getCompatColor(R.color.tv_text_default) + ) + } a.recycle() init() @@ -161,20 +190,54 @@ class CircleImageView : AppCompatImageView { super.onDraw(canvas) return } - if (mBitmap == null) { return } if (mCircleBackgroundColor != Color.TRANSPARENT) { - canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mCircleBackgroundPaint) + canvas.drawCircle( + mDrawableRect.centerX(), + mDrawableRect.centerY(), + mDrawableRadius, + mCircleBackgroundPaint + ) } - canvas.drawCircle(mDrawableRect.centerX(), mDrawableRect.centerY(), mDrawableRadius, mBitmapPaint) + canvas.drawCircle( + mDrawableRect.centerX(), + mDrawableRect.centerY(), + mDrawableRadius, + mBitmapPaint + ) if (mBorderWidth > 0) { - canvas.drawCircle(mBorderRect.centerX(), mBorderRect.centerY(), mBorderRadius, mBorderPaint) + canvas.drawCircle( + mBorderRect.centerX(), + mBorderRect.centerY(), + mBorderRadius, + mBorderPaint + ) + } + drawText(canvas) + } + + private fun drawText(canvas: Canvas) { + text?.let { + textPaint.color = textColor + textPaint.textSize = 15.sp.toFloat() + val fm = textPaint.fontMetrics + canvas.drawText( + it, + width * 0.5f, + (height * 0.5f + (fm.bottom - fm.top) * 0.5f - fm.bottom), + textPaint + ) } } + fun setTextColor(@ColorInt textColor: Int) { + this.textColor = textColor + invalidate() + } + override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) setup() @@ -249,7 +312,11 @@ class CircleImageView : AppCompatImageView { BITMAP_CONFIG ) } else { - Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, BITMAP_CONFIG) + Bitmap.createBitmap( + drawable.intrinsicWidth, + drawable.intrinsicHeight, + BITMAP_CONFIG + ) } val canvas = Canvas(bitmap) @@ -306,7 +373,10 @@ class CircleImageView : AppCompatImageView { mBorderRect.set(calculateBounds()) mBorderRadius = - min((mBorderRect.height() - mBorderWidth) / 2.0f, (mBorderRect.width() - mBorderWidth) / 2.0f) + min( + (mBorderRect.height() - mBorderWidth) / 2.0f, + (mBorderRect.width() - mBorderWidth) / 2.0f + ) mDrawableRect.set(mBorderRect) if (!mBorderOverlay && mBorderWidth > 0) { @@ -347,7 +417,10 @@ class CircleImageView : AppCompatImageView { } mShaderMatrix.setScale(scale, scale) - mShaderMatrix.postTranslate((dx + 0.5f).toInt() + mDrawableRect.left, (dy + 0.5f).toInt() + mDrawableRect.top) + mShaderMatrix.postTranslate( + (dx + 0.5f).toInt() + mDrawableRect.left, + (dy + 0.5f).toInt() + mDrawableRect.top + ) mBitmapShader!!.setLocalMatrix(mShaderMatrix) } 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 f20ace6ce..426391be4 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 @@ -19,8 +19,22 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView { internal var height: Float = 0.toFloat() private var nameHeight = 0f private var authorHeight = 0f - private val namePaint = TextPaint() - private val authorPaint = TextPaint() + 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 @@ -35,17 +49,6 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView { defStyleAttr ) - init { - namePaint.typeface = Typeface.DEFAULT_BOLD - namePaint.isAntiAlias = true - namePaint.textAlign = Paint.Align.CENTER - namePaint.textSkewX = -0.2f - authorPaint.typeface = Typeface.DEFAULT - authorPaint.isAntiAlias = true - authorPaint.textAlign = Paint.Align.CENTER - authorPaint.textSkewX = -0.1f - } - override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { val measuredWidth = MeasureSpec.getSize(widthMeasureSpec) val measuredHeight = measuredWidth * 7 / 5 @@ -63,8 +66,9 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView { namePaint.strokeWidth = namePaint.textSize / 10 authorPaint.textSize = width / 9 authorPaint.strokeWidth = authorPaint.textSize / 10 - nameHeight = height / 2 - authorHeight = nameHeight + authorPaint.fontSpacing + 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) { diff --git a/app/src/main/res/layout/dialog_read_book_style.xml b/app/src/main/res/layout/dialog_read_book_style.xml index 62e1bcdfb..75e6cf2e2 100644 --- a/app/src/main/res/layout/dialog_read_book_style.xml +++ b/app/src/main/res/layout/dialog_read_book_style.xml @@ -222,98 +222,58 @@ android:layout_height="48dp" app:civ_border_color="@color/tv_text_default" app:civ_border_width="1dp" + app:text="@string/text" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toLeftOf="@+id/bg1" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@+id/tv_bg_ts" /> - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 485e8ef20..89308f5d7 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -131,6 +131,8 @@ + +