Drawing in SurfaceView, via button

21 views Asked by At

Good afternoon, tell me how to do the following: I need to add a drawing of an object when a button is pressed (so that later I can change the parameters of this drawing object using other buttons).I'm using surfaceview and don't understand how to trigger drawing via SetOnClickListener My code:

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.test2.databinding.FragmentItem2Binding



class item2Fragment : Fragment() {

    private var _binding: FragmentItem2Binding? = null
    private var surface: SurfaceView? = null

    private var mSurfaceHolder: SurfaceHolder? = null
    private val MP1 = Paint(Paint.ANTI_ALIAS_FLAG)
    private val MP2 = Paint(Paint.ANTI_ALIAS_FLAG)
    private val binding get() = _binding!!
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        _binding = FragmentItem2Binding.inflate(inflater, container, false)
        binding.testbut.setOnClickListener {

        }

        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        surface = view.findViewById<SurfaceView>(R.id.surf)
        this.surface!!.holder!!.addCallback(/* callback = */ object : SurfaceHolder.Callback {
            override fun surfaceCreated(holder: SurfaceHolder) {
                Log.e("prog","CV ")
                var canvas: Canvas? = null
                mSurfaceHolder = holder
                canvas = mSurfaceHolder!!.lockCanvas()

                MP2.setColor(Color.RED)
                MP1.setColor(Color.WHITE)
                canvas.drawRect(1000F, 100F, 50F, 1000F, MP1)
                canvas.drawRect(100F, 200F, 50F, 300F, MP2)

                Log.e("MyCameraView","CANV ")

                mSurfaceHolder!!.unlockCanvasAndPost(canvas)}

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

            }

            override fun surfaceDestroyed(holder: SurfaceHolder) {

            }

        })

    }

html:

    <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=".item2Fragment">
    <SurfaceView
        android:layout_width="400sp"
        android:layout_height="400sp"
        android:id="@+id/surf"

        >


    </SurfaceView>



    <Button
        android:id="@+id/testbut"
        android:layout_width="40sp"
        android:layout_height="40sp"
        android:layout_marginTop="400sp"
        android:layout_marginLeft="50sp">


    </Button>


</FrameLayout>

I need to render objects with the ability to change them. I've been stuck with this problem for quite some time now and I can't find a way out.

0

There are 0 answers