Partage
  • Partager sur Facebook
  • Partager sur Twitter

custom classe view en kotlin

dessiner avec Path

Sujet résolu
    19 décembre 2023 à 13:20:45

    bonjour, je débute en kotlin et je m'intéresse à un tutoriel au cours duquel on nous apprend à créer des lignes grâce à Paint et à Path dans une view custom...

    le problème est que le code compile, mais que je n'ai rien à l'écran..

    Je précise que je suis un absolu débutant dans ce langage.

    voici ma classe path qui étend view:

    package com.gunsailor.drawing
    
    import android.content.Context
    import android.graphics.Canvas
    import android.graphics.Color
    import android.graphics.Paint
    import android.graphics.Path
    import android.util.AttributeSet
    import android.view.View
    
    class path @JvmOverloads
    constructor(
        private val ctx: Context,
        private val attributeSet: AttributeSet? = null,
        private val defStyleAttr: Int = 0
    ) : View(ctx, attributeSet, defStyleAttr) {
    
    
        var p = Path()
        var pathPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
            color = Color.parseColor("#a20a0a")
        }
    
        private fun drawLinedPath(canvas: Canvas?) {
    
            p.moveTo(0.1f * width, 0.1f * height)
            p.lineTo(0.1f * width, 0.5f * height)
            p.lineTo(0.9f * width, 0.1f * height)
    
            canvas?.drawPath(p, pathPaint)
    
        }
    }


    et voici mon activité principale:

    package com.gunsailor.drawing
    
    import android.os.Bundle
    import androidx.activity.ComponentActivity
    import androidx.activity.compose.setContent
    import androidx.compose.foundation.layout.fillMaxSize
    import androidx.compose.material3.MaterialTheme
    import androidx.compose.material3.Surface
    import androidx.compose.material3.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.Modifier
    import androidx.compose.ui.tooling.preview.Preview
    import com.gunsailor.drawing.ui.theme.DrawingTheme
    
    class MainActivity : ComponentActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val p: path = path(this)
    
            setContentView(p)
        }
    }
    

    j'ai même essayé d instancier mon path dans le fichier layout xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    <com.gunsailor.drawing.path
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>


    malheureusement je n'ai toujours rien sur mon émulateur android..

    auriez vous une idée de ce qui se passe?

    merci de votre attention

    • Partager sur Facebook
    • Partager sur Twitter

    custom classe view en kotlin

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    • Editeur
    • Markdown