surfaceview模板代码

package com.mou.blog.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.Surface
import android.view.SurfaceHolder
import android.view.SurfaceView

/**
 * FileName: com.mou.blog.widget.SurfaceDemo.java
 * Author: mouxuefei
 * date: 2018/2/7
 * version: V1.0
 * desc:
 */
class SurfaceDemo : SurfaceView, SurfaceHolder, SurfaceHolder.Callback, Runnable {
    private var mSurfaceHolder: SurfaceHolder? = null
    private var mCanvas: Canvas? = null
    private var mIsDrawing: Boolean = false
    private var mPath: Path? = null
    private var mPaint: Paint? = null

    private var x: Int = 0
    private var y: Int = 0

    constructor(context: Context) : super(context) {
        init()
    }


    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()

    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    /**
     * 初始化内容
     */
    private fun init() {
        mSurfaceHolder = holder
        mSurfaceHolder?.addCallback(this)

        isFocusable = true
        isFocusableInTouchMode = true
        keepScreenOn = true


        mPaint = Paint()
        mPaint?.color = Color.BLUE
        mPaint?.strokeWidth = 10f
        mPaint?.style = Paint.Style.STROKE
        mPath = Path()
    }

    override fun addCallback(callback: SurfaceHolder.Callback) {

    }

    override fun removeCallback(callback: SurfaceHolder.Callback) {

    }

    override fun isCreating(): Boolean {
        return false
    }

    override fun setType(type: Int) {

    }

    override fun setFixedSize(width: Int, height: Int) {

    }

    override fun setSizeFromLayout() {

    }

    override fun setFormat(format: Int) {

    }

    override fun lockCanvas(): Canvas? {
        return null
    }

    override fun lockCanvas(dirty: Rect): Canvas? {
        return null
    }

    override fun unlockCanvasAndPost(canvas: Canvas) {

    }

    override fun getSurfaceFrame(): Rect? {
        return null
    }

    override fun getSurface(): Surface? {
        return null
    }

    /*---------------callback-------------*/
    override fun surfaceCreated(holder: SurfaceHolder) {
        mIsDrawing = true
        Thread(this).start()
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
        mIsDrawing = false
    }

    /*---------------runnable-------------*/
    override fun run() {
        while (mIsDrawing) {
            draw()
            x += 1
            y = (100 * Math.sin(x.toDouble() * 2.0 * Math.PI / 180) + 400).toInt()
            mPath?.lineTo(x.toFloat(), y.toFloat())
        }
    }

    private fun draw() {

        try {
            mCanvas = mSurfaceHolder?.lockCanvas()
            //绘制的内容
            mCanvas?.drawPath(mPath, mPaint)
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            if (mCanvas != null) {
                //保存绘制的内容
                mSurfaceHolder?.unlockCanvasAndPost(mCanvas)
            }
        }

    }
}

猜你喜欢

转载自blog.csdn.net/villa_mou/article/details/79281150