feat: 优化代码

pull/103/head
kunfei 5 years ago
parent 0728318303
commit d8e2e36c23
  1. 30
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  2. 101
      app/src/main/java/io/legado/app/ui/widget/image/CircleImageView.kt
  3. 34
      app/src/main/java/io/legado/app/ui/widget/image/CoverImageView.kt
  4. 50
      app/src/main/res/layout/dialog_read_book_style.xml
  5. 2
      app/src/main/res/values/attrs.xml

@ -140,16 +140,16 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
} }
} }
} }
tv_bg0.onClick { changeBg(0) } bg0.onClick { changeBg(0) }
tv_bg0.onLongClick { showBgTextConfig(0) } bg0.onLongClick { showBgTextConfig(0) }
tv_bg1.onClick { changeBg(1) } bg1.onClick { changeBg(1) }
tv_bg1.onLongClick { showBgTextConfig(1) } bg1.onLongClick { showBgTextConfig(1) }
tv_bg2.onClick { changeBg(2) } bg2.onClick { changeBg(2) }
tv_bg2.onLongClick { showBgTextConfig(2) } bg2.onLongClick { showBgTextConfig(2) }
tv_bg3.onClick { changeBg(3) } bg3.onClick { changeBg(3) }
tv_bg3.onLongClick { showBgTextConfig(3) } bg3.onLongClick { showBgTextConfig(3) }
tv_bg4.onClick { changeBg(4) } bg4.onClick { changeBg(4) }
tv_bg4.onLongClick { showBgTextConfig(4) } bg4.onLongClick { showBgTextConfig(4) }
} }
private fun changeBg(index: Int) { private fun changeBg(index: Int) {
@ -182,11 +182,11 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
} }
private fun setBg() { private fun setBg() {
tv_bg0.setTextColor(ReadBookConfig.getConfig(0).textColor()) bg0.setTextColor(ReadBookConfig.getConfig(0).textColor())
tv_bg1.setTextColor(ReadBookConfig.getConfig(1).textColor()) bg1.setTextColor(ReadBookConfig.getConfig(1).textColor())
tv_bg2.setTextColor(ReadBookConfig.getConfig(2).textColor()) bg2.setTextColor(ReadBookConfig.getConfig(2).textColor())
tv_bg3.setTextColor(ReadBookConfig.getConfig(3).textColor()) bg3.setTextColor(ReadBookConfig.getConfig(3).textColor())
tv_bg4.setTextColor(ReadBookConfig.getConfig(4).textColor()) bg4.setTextColor(ReadBookConfig.getConfig(4).textColor())
for (i in 0..4) { for (i in 0..4) {
val iv = when (i) { val iv = when (i) {
1 -> bg1 1 -> bg1

@ -8,6 +8,7 @@ import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.text.TextPaint
import android.util.AttributeSet import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.view.View import android.view.View
@ -19,6 +20,7 @@ import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatImageView import androidx.appcompat.widget.AppCompatImageView
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
import io.legado.app.utils.sp
import kotlin.math.min import kotlin.math.min
import kotlin.math.pow import kotlin.math.pow
@ -31,6 +33,12 @@ class CircleImageView : AppCompatImageView {
private val mBitmapPaint = Paint() private val mBitmapPaint = Paint()
private val mBorderPaint = Paint() private val mBorderPaint = Paint()
private val mCircleBackgroundPaint = 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 mBorderColor = DEFAULT_BORDER_COLOR
private var mBorderWidth = DEFAULT_BORDER_WIDTH private var mBorderWidth = DEFAULT_BORDER_WIDTH
@ -105,22 +113,43 @@ class CircleImageView : AppCompatImageView {
setup() 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() init()
} }
@JvmOverloads @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) val a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0)
mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_civ_border_width, DEFAULT_BORDER_WIDTH) mBorderWidth = a.getDimensionPixelSize(
mBorderColor = a.getColor(R.styleable.CircleImageView_civ_border_color, DEFAULT_BORDER_COLOR) R.styleable.CircleImageView_civ_border_width,
mBorderOverlay = a.getBoolean(R.styleable.CircleImageView_civ_border_overlay, DEFAULT_BORDER_OVERLAY) 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 = 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() a.recycle()
init() init()
@ -161,18 +190,52 @@ class CircleImageView : AppCompatImageView {
super.onDraw(canvas) super.onDraw(canvas)
return return
} }
if (mBitmap == null) { if (mBitmap == null) {
return return
} }
if (mCircleBackgroundColor != Color.TRANSPARENT) { 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) { 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) { override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
@ -249,7 +312,11 @@ class CircleImageView : AppCompatImageView {
BITMAP_CONFIG BITMAP_CONFIG
) )
} else { } else {
Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, BITMAP_CONFIG) Bitmap.createBitmap(
drawable.intrinsicWidth,
drawable.intrinsicHeight,
BITMAP_CONFIG
)
} }
val canvas = Canvas(bitmap) val canvas = Canvas(bitmap)
@ -306,7 +373,10 @@ class CircleImageView : AppCompatImageView {
mBorderRect.set(calculateBounds()) mBorderRect.set(calculateBounds())
mBorderRadius = 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) mDrawableRect.set(mBorderRect)
if (!mBorderOverlay && mBorderWidth > 0) { if (!mBorderOverlay && mBorderWidth > 0) {
@ -347,7 +417,10 @@ class CircleImageView : AppCompatImageView {
} }
mShaderMatrix.setScale(scale, scale) 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) mBitmapShader!!.setLocalMatrix(mShaderMatrix)
} }

@ -19,8 +19,22 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
internal var height: Float = 0.toFloat() internal var height: Float = 0.toFloat()
private var nameHeight = 0f private var nameHeight = 0f
private var authorHeight = 0f private var authorHeight = 0f
private val namePaint = TextPaint() private val namePaint by lazy {
private val authorPaint = TextPaint() 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 name: String? = null
private var author: String? = null private var author: String? = null
private var loadFailed = false private var loadFailed = false
@ -35,17 +49,6 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
defStyleAttr 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) { 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
@ -63,8 +66,9 @@ class CoverImageView : androidx.appcompat.widget.AppCompatImageView {
namePaint.strokeWidth = namePaint.textSize / 10 namePaint.strokeWidth = namePaint.textSize / 10
authorPaint.textSize = width / 9 authorPaint.textSize = width / 9
authorPaint.strokeWidth = authorPaint.textSize / 10 authorPaint.strokeWidth = authorPaint.textSize / 10
nameHeight = height / 2 val fm = namePaint.fontMetrics
authorHeight = nameHeight + authorPaint.fontSpacing 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) {

@ -222,98 +222,58 @@
android:layout_height="48dp" android:layout_height="48dp"
app:civ_border_color="@color/tv_text_default" app:civ_border_color="@color/tv_text_default"
app:civ_border_width="1dp" app:civ_border_width="1dp"
app:text="@string/text"
app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/bg1" app:layout_constraintRight_toLeftOf="@+id/bg1"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_bg_ts" /> app:layout_constraintTop_toBottomOf="@+id/tv_bg_ts" />
<TextView
android:id="@+id/tv_bg0"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/text"
app:layout_constraintTop_toTopOf="@+id/bg0"
app:layout_constraintLeft_toLeftOf="@+id/bg0" />
<io.legado.app.ui.widget.image.CircleImageView <io.legado.app.ui.widget.image.CircleImageView
android:id="@+id/bg1" android:id="@+id/bg1"
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
app:civ_border_color="@color/tv_text_default" app:civ_border_color="@color/tv_text_default"
app:civ_border_width="1dp" app:civ_border_width="1dp"
app:text="@string/text"
app:layout_constraintLeft_toRightOf="@+id/bg0" app:layout_constraintLeft_toRightOf="@+id/bg0"
app:layout_constraintRight_toLeftOf="@+id/bg2" app:layout_constraintRight_toLeftOf="@+id/bg2"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@+id/bg0" /> app:layout_constraintTop_toTopOf="@+id/bg0" />
<TextView
android:id="@+id/tv_bg1"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/text"
app:layout_constraintTop_toTopOf="@+id/bg1"
app:layout_constraintLeft_toLeftOf="@+id/bg1" />
<io.legado.app.ui.widget.image.CircleImageView <io.legado.app.ui.widget.image.CircleImageView
android:id="@+id/bg2" android:id="@+id/bg2"
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
app:civ_border_color="@color/tv_text_default" app:civ_border_color="@color/tv_text_default"
app:civ_border_width="1dp" app:civ_border_width="1dp"
app:text="@string/text"
app:layout_constraintLeft_toRightOf="@+id/bg1" app:layout_constraintLeft_toRightOf="@+id/bg1"
app:layout_constraintRight_toLeftOf="@+id/bg3" app:layout_constraintRight_toLeftOf="@+id/bg3"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@+id/bg0" /> app:layout_constraintTop_toTopOf="@+id/bg0" />
<TextView
android:id="@+id/tv_bg2"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/text"
app:layout_constraintTop_toTopOf="@+id/bg2"
app:layout_constraintLeft_toLeftOf="@+id/bg2" />
<io.legado.app.ui.widget.image.CircleImageView <io.legado.app.ui.widget.image.CircleImageView
android:id="@+id/bg3" android:id="@+id/bg3"
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
app:civ_border_color="@color/tv_text_default" app:civ_border_color="@color/tv_text_default"
app:civ_border_width="1dp" app:civ_border_width="1dp"
app:text="@string/text"
app:layout_constraintLeft_toRightOf="@+id/bg2" app:layout_constraintLeft_toRightOf="@+id/bg2"
app:layout_constraintRight_toLeftOf="@+id/bg4" app:layout_constraintRight_toLeftOf="@+id/bg4"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@+id/bg0" /> app:layout_constraintTop_toTopOf="@+id/bg0" />
<TextView
android:id="@+id/tv_bg3"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/text"
app:layout_constraintTop_toTopOf="@+id/bg3"
app:layout_constraintLeft_toLeftOf="@+id/bg3" />
<io.legado.app.ui.widget.image.CircleImageView <io.legado.app.ui.widget.image.CircleImageView
android:id="@+id/bg4" android:id="@+id/bg4"
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
app:civ_border_color="@color/tv_text_default" app:civ_border_color="@color/tv_text_default"
app:civ_border_width="1dp" app:civ_border_width="1dp"
app:text="@string/text"
app:layout_constraintLeft_toRightOf="@+id/bg3" app:layout_constraintLeft_toRightOf="@+id/bg3"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="@+id/bg0" /> app:layout_constraintTop_toTopOf="@+id/bg0" />
<TextView
android:id="@+id/tv_bg4"
android:layout_width="48dp"
android:layout_height="48dp"
android:gravity="center"
android:text="@string/text"
app:layout_constraintTop_toTopOf="@+id/bg4"
app:layout_constraintLeft_toLeftOf="@+id/bg4" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -131,6 +131,8 @@
<attr name="civ_border_color" format="color" /> <attr name="civ_border_color" format="color" />
<attr name="civ_border_overlay" format="boolean" /> <attr name="civ_border_overlay" format="boolean" />
<attr name="civ_circle_background_color" format="color" /> <attr name="civ_circle_background_color" format="color" />
<attr name="text" format="string" />
<attr name="textColor" format="color" />
</declare-styleable> </declare-styleable>
<declare-styleable name="VerticalSeekBar"> <declare-styleable name="VerticalSeekBar">

Loading…
Cancel
Save