|
|
@ -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) |
|
|
|
} |
|
|
|
} |
|
|
|