我是初学者,尝试使用 canvas 将 java 代码重写为 Kotlin,尝试写一个圆圈。但结果我的设备上只有空白屏幕

I'm beginner and try to re-write java code to Kotlin, using canvas, try write a circle. But as a result I get only blank screen on my Device

这是我的 SplashScreen.kt 文件:

class SplashScreen(context: Context): View(context) {
    private var paint: Paint? = null
    private var cx : Float = 0.0f
    private var cy : Float = 0.0f
    private var radius : Float = 0.0f

    fun SplashScreen(context: Context) {
        val paint = Paint()
        paint.color = Color.GREEN
        paint.strokeWidth = 2.0f
        paint.isAntiAlias = true
        cx = 200f; cy = 200f; radius = 50.0f;
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas);
        if (paint != null) {
            canvas.drawCircle(cx,cy,radius,paint)
        }
    }
}

这是我的 Main.kt 文件:

class MainActivity : AppCompatActivity() {
    private lateinit var splash : SplashScreen

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val splash = SplashScreen(this)
        splash.keepScreenOn = true
        setContentView(splash)
    }
}

当我 运行 我的代码在我的设备上时,我只看到白色的空白屏幕。没有任何圆圈。
我尝试调用 ActivityMain,而不是 SplashScreen,所有的工作都是“Hello world”。

我不明白我在这里弄错了什么。

谁能告诉我为什么这只会产生一个空白屏幕?

我发现你的代码有问题。 所以你做了错误的构造函数。 您应该替换构造函数代码。 您可以使用构造函数关键字。 就这样吧。

class SplashScreen: View {
    private var paint: Paint? = null
    private var cx : Float = 0.0f
    private var cy : Float = 0.0f
    private var radius : Float = 0.0f


    //fun SplashScreen(context: Context) { // this is wrong
    constructor(context: Context) : super(context) { // this is correct.
        val paint = Paint()
        paint.color = Color.GREEN
        paint.strokeWidth = 2.0f
        paint.isAntiAlias = true
        cx = 200f; cy = 200f; radius = 50.0f;
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas);
        if (paint != null) {
            canvas.drawCircle(cx,cy,radius,paint)
        }
    }
}

问题是您将自己与构造函数和另一个名为 SplashScreen

的方法混淆了

您可以使用以下方法显着简化您的代码:

class SplashScreen(context: Context): View(context) {
    private val paint: Paint = Paint().apply {
        color = Color.GREEN
        strokeWidth = 2.0f
        isAntiAlias = true
    }
    private val cx : Float = 200.0f
    private val cy : Float = 200.0f
    private val radius : Float = 50.0f
    
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(cx,cy,radius,paint)
    }
}

或者,您可以创建一个额外的构造函数以允许设置值

class SplashScreen(context: Context): View(context) {
    private val paint: Paint = Paint().apply {
        color = Color.GREEN
        strokeWidth = 2.0f
        isAntiAlias = true
    }
    private var cx : Float = 0.0f
    private var cy : Float = 0.0f
    private var radius : Float = 0.0f

    constructor(context: Context, radius: Float, cx: Float, cy: Float) : this(context) {
        this.radius = radius
        this.cx = cx
        this.cy = cy
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(cx,cy,radius,paint)
    }
}