【Kotlin】Kotlin 自定义组件 ( 自定义 View | 自定义 SurfaceView )
生活随笔
收集整理的這篇文章主要介紹了
【Kotlin】Kotlin 自定义组件 ( 自定义 View | 自定义 SurfaceView )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、自定義 View 組件 ( Kotlin )
- 二、自定義 SurfaceView 組件 ( Kotlin )
自定義組件構造函數統一在 constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) 構造函數中實現 , 在 constructor(context: Context?) , constructor(context: Context?, attrs: AttributeSet?) 構造函數中 , 都基于三個參數的構造函數 ;
一、自定義 View 組件 ( Kotlin )
package kim.hsl.aa.viewimport android.content.Context import android.graphics.Canvas import android.os.Build import android.util.AttributeSet import android.view.View import androidx.annotation.RequiresApiclass 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)// 繪制核心方法} }
二、自定義 SurfaceView 組件 ( Kotlin )
package kim.hsl.aa.viewimport 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.Rclass MySurfaceView : SurfaceView, SurfaceHolder.Callback, Runnable {val TAG = "MySurfaceView"/*** 渲染繪制標志*/private var mDrawingFlag = false/*** 渲染線程*/private var mRender: Thread? = null/*** 畫布*/private var mCanvas: Canvas? = nullconstructor(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) continuemCanvas?.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 = truemRender = Thread(this)mRender?.start()// 加載圖片var bitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)}override fun surfaceDestroyed(holder: SurfaceHolder?) {}override fun surfaceCreated(holder: SurfaceHolder?) {}} 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的【Kotlin】Kotlin 自定义组件 ( 自定义 View | 自定义 SurfaceView )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Kotlin】循环控制流 ( for
- 下一篇: 【运筹学】线性规划 单纯形法 案例二