android - Canvas 不在 SurfaceView 中绘制

标签 android kotlin

我在 SurfaceView 中创建了一个名为 draw 的重写方法。我想看到我在 SurfaceView 中设置的绘画,但当我触摸屏幕并尝试画一条线时,什么也没有显示。我应该做什么才能使这项工作成功?

   private var mPaint: Paint
   private val mPaths: ArrayList<Path> = ArrayList<Path>()
   private val mEraserPath: Path = Path()

   init {
        mPaint = Paint()
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeJoin = Paint.Join.ROUND
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.strokeWidth = 3f
        mPaint.alpha = 255
        mPaint.color = android.graphics.Color.BLACK
        mPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
       }

   override fun draw(canvas: Canvas) {
        canvas.drawPaint(mPaint)
        val action: EditAction? = this.getEditAction()
        for (path: Path in mPaths) {
            when (action) {
                EditAction.COLOR -> {
                    setPaintColor(this.getStrokeColor()) // android.graphics.Color.BLACK
                    setPaintSize(this.getStrokeSize()) // 5f
                    canvas.drawPath(path, mPaint)
                }
                EditAction.SIZE -> {
                    setPaintColor(this.getStrokeColor()) // android.graphics.Color.BLACK
                    setPaintSize(this.getStrokeSize()) // 5f
                    canvas.drawPath(path, mPaint)
                }
                EditAction.ERASER -> {

                }
            }
        }
        canvas.drawPath(mEraserPath, mPaint)
        super.draw(canvas)
    }

最佳答案

不要使用draw,而是使用SurfaceHolder.Callback函数,如下所示。我有莫夫

class SlowSurfaceView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0)
    : SurfaceView(context, attrs, defStyleAttr), SurfaceHolder.Callback {

    private var mPaint: Paint = Paint()

    init {
        holder.addCallback(this)
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeJoin = Paint.Join.ROUND
        mPaint.strokeCap = Paint.Cap.ROUND
        mPaint.strokeWidth = 3f
        mPaint.alpha = 255
        mPaint.color = android.graphics.Color.RED
        mPaint.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
        // Do nothing for now
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        if (isAttachedToWindow) {
            val canvas = holder.lockCanvas()
            canvas?.let {
                it.drawRect(Rect(100, 100, 200, 200), mPaint)
                holder.unlockCanvasAndPost(it)
            }
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val desiredWidth = suggestedMinimumWidth + paddingLeft + paddingRight
        val desiredHeight = suggestedMinimumHeight + paddingTop + paddingBottom

        setMeasuredDimension(View.resolveSize(desiredWidth, widthMeasureSpec),
                View.resolveSize(desiredHeight, heightMeasureSpec))
    }
}

引用上面修改代码,希望你能得到你想要的。

关于android - Canvas 不在 SurfaceView 中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55214089/

相关文章:

java - 如何在 graphql-java-tools Resolver 中 @Autowire JPA 存储库

java - 将 java Class<> 转换为 kotlin 的 KClass<>

android - 具有上下文的 Xamarin 依赖服务

php - 数组推送,期望参数 1 为数组但给定为 null

android:使用Intent.ACTION_BOOT_COMPLETED还是...?

android - 在 Retrofit Interceptor 中访问 sharedPreferences

ssl - 无需证书验证即可进行 httpS 连接的 kotlin 库(如 curl --insecure)

android - 需要解释 android 布局语法

android - 如何获取Android sdcard上的可用空间?

android - 是否可以确保 Android Room 数据库中只有一行包含 bool 值 'true' ?