【Kotlin】Kotlin 自定义组件 ( 自定义 View | 自定义 SurfaceView )
【摘要】
文章目录
一、自定义 View 组件 ( Kotlin )二、自定义 SurfaceView 组件 ( Kotlin )
自定义组件构造函数统一在 constructor(context...
自定义组件构造函数统一在 constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int)
构造函数中实现 , 在 constructor(context: Context?)
, constructor(context: Context?, attrs: AttributeSet?)
构造函数中 , 都基于三个参数的构造函数 ;
一、自定义 View 组件 ( Kotlin )
package kim.hsl.aa.view
import android.content.Context
import android.graphics.Canvas
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
class MyView : View {
val TAG: String = "MyView"
constructor(context: Context?) : this(context, null, 0)
constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
super(context, attrs, defStyleAttr)
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes)
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
// 绘制核心方法
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
二、自定义 SurfaceView 组件 ( Kotlin )
package kim.hsl.aa.view
import android.content.Context
import android.graphics.*
import android.os.Build
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.annotation.RequiresApi
import kim.hsl.aa.R
class MySurfaceView : SurfaceView, SurfaceHolder.Callback, Runnable {
val TAG = "MySurfaceView"
/**
* 渲染绘制标志
*/
private var mDrawingFlag = false
/**
* 渲染线程
*/
private var mRender: Thread? = null
/**
* 画布
*/
private var mCanvas: Canvas? = null
constructor(context: Context) : this(context, null, 0)
constructor(context: Context, attrs : AttributeSet?) : this(context, attrs, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr : Int) :
super(context, attrs, defStyleAttr) {
holder.addCallback(this)
setZOrderOnTop(true)
holder.setFormat(PixelFormat.TRANSLUCENT)
}
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
constructor(context: Context, attrs: AttributeSet, defStyleAttr : Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes)
override fun run() {
val paint = Paint()
while (mDrawingFlag) {
paint.setColor(Color.WHITE)
mCanvas = holder.lockCanvas()
if (mCanvas == null) continue
mCanvas?.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)
//TODO 绘制逻辑
holder.unlockCanvasAndPost(mCanvas)
Thread.sleep(20)
}
}
/*
下面的三个函数是 实现 SurfaceHolder.Callback 接口方法
*/
override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
mDrawingFlag = true
mRender = Thread(this)
mRender?.start()
// 加载图片
var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
}
override fun surfaceDestroyed(holder: SurfaceHolder?) {
}
override fun surfaceCreated(holder: SurfaceHolder?) {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
文章来源: hanshuliang.blog.csdn.net,作者:韩曙亮,版权归原作者所有,如需转载,请联系作者。
原文链接:hanshuliang.blog.csdn.net/article/details/107560832
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)