繼承View重寫onDraw方法
通常用于實作復雜的、不規則的效果,需要自己支持wrap_content和padding,自定義圓形進度條組件的kotlin代碼
class MyCircleProgress : View {
private var mWidth = 0
private var mHeight = 0
private var progress = 0.6
constructor(context: Context) : super(context) {
}
constructor(context: Context, attr: AttributeSet) : super(context, attr) {
}
private fun dp2px(dp: Int): Int {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp.toFloat(), Resources.getSystem().displayMetrics).toInt()
}
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
when(widthMode) {
// 處理match_parent和固定寬度的情況
MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY -> mWidth = widthSize
// 處理wrap_content
MeasureSpec.AT_MOST -> mWidth = dp2px(200)
}
when(heightMode) {
MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY -> mHeight = heightSize
MeasureSpec.AT_MOST -> mHeight = dp2px(200)
}
setMeasuredDimension(mWidth, mHeight)
}
override fun onDraw(canvas: Canvas?) {
canvas?.apply {
val paint = Paint().apply {
// Paint設定的顏色默認是有透明度的,且透明度為0
// 并且在kotlin中0xff00ffff超過了int的范圍,所以這里要呼叫toInt()
color = 0xff00ffff.toInt()
isAntiAlias = true
this.strokeWidth = 10F
style = Paint.Style.STROKE
}
// 設定圓弧的繪制區域,處理padding
val rectF = RectF(10F + paddingLeft, 10F + paddingTop,
(mWidth - 10 - paddingRight).toFloat(), (mHeight - 10 - paddingBottom).toFloat())
// 繪制圓弧,表示進度
drawArc(rectF, -90F, (360 * progress).toFloat(), false, paint)
paint.color = 0xff000000.toInt()
paint.style = Paint.Style.FILL
paint.textSize = 50F
// 繪制文字,顯示進度百分比
drawText("${(100 * progress).toInt()} %", (mWidth / 2).toFloat(), (mHeight / 2).toFloat(), paint)
}
}
}
繼承特定的View重寫onDraw方法
用于擴展某種已有View的功能,比如TextView、AppCompatImageView,不需要自己支持wrap_content和padding,自定義圓形ImageView的kotlin代碼
class MyImageView : androidx.appcompat.widget.AppCompatImageView {
constructor(context: Context) : super(context) {
}
constructor(context: Context, attr: AttributeSet) : super(context, attr) {
}
override fun onDraw(canvas: Canvas?) {
val paint = Paint().apply {
color = 0xffffffff.toInt()
isAntiAlias = true
}
val xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
canvas?.apply {
saveLayer(0F, 0F, width.toFloat(), height.toFloat(), null, Canvas.ALL_SAVE_FLAG)
// 直接使用clipPath會有鋸齒問題
// canvas.clipPath(path)
canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), (width / 2 - 1).toFloat(), paint)
val bitmap = drawable.toBitmap()
val bitmapRect = Rect(0, 0, bitmap.width, bitmap.height)
val canvasRect = Rect(0, 0, width, height)
paint.xfermode = xfermode
drawBitmap(bitmap, bitmapRect, canvasRect, paint)
restore()
}
}
}
最終效果
除了以上兩種方法,我們還可以繼承ViewGroup和特定的ViewGroup(比如FrameLayout)來實作自定義View,在實際開發中,常常需要實作幾種View組合在一起的效果,可以繼承特定的ViewGroup來實作(一般不直接繼承ViewGroup,除非必要)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/124656.html
標籤:其他
