SurfaceView不能用ScrollView包装吗?

时间:2018-07-24 03:46:18

标签: android scrollview surfaceview android-scrollview

我完成了SurfaceView

class CanvasDrawView : SurfaceView, SurfaceHolder.Callback {

    @JvmOverloads
    constructor(
            context: Context,
            attrs: AttributeSet? = null,
            defStyleAttr: Int = 0)
            : super(context, attrs, defStyleAttr)

    init {
        holder.addCallback(this)
    }

    private val strokePaint = Paint()
            .apply { color = Color.RED }
            .apply { strokeWidth = 16f }

    private var job: Job? = null
    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
        // Do nothing for now
    }

    override fun surfaceDestroyed(holder: SurfaceHolder?) {
        job?.cancel()
    }

    override fun surfaceCreated(holder: SurfaceHolder?) {

        var i = 0f
        job = launch {
            while (true) {
                val canvas = holder?.lockCanvas(null)
                synchronized(holder!!) {
                    i += 1f
                    if (i > width) {
                        i = 0f
                    }
                    canvas?.drawPoint(i, i, strokePaint)
                }

                holder?.unlockCanvasAndPost(canvas)
            }
        }
    }
}

当我将其包裹在FrameLayout上时,效果很好。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.elyeproj.canvasdrawing.CanvasDrawView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="1000dp" />

</FrameLayout>

但是,如果我将其包装在ScrollView中,如下所示,它将不再显示。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.elyeproj.canvasdrawing.CanvasDrawView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="1000dp" />

</ScrollView>

但是,如果我切换到普通视图,则所有工作(都在ScrollViewFrameView上进行。

SurfaceView上,我还尝试如下显式设置宽度和高度,也无法正常工作

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
    setMeasuredDimension(1000, 1000)
}

SurfaceView不能被ScrollView包装吗?

0 个答案:

没有答案