문제

so i made a Surface view with a canvas, so i could do some graphics. is there any way of having a xml layout with a canvas, so i can have buttons and what not on the screen, or is there any other option to making buttons on a canvas?

도움이 되었습니까?

해결책

You will have to made one class that having that canvas in it. And then Import that class in the xml layout.

See this example: FingerPaint Example.

In above example there is myView class that contain the canvas to draw on it.

What you need to do is:

First Create any Layout in your xml as like below:

<RelativeLayout
        android:id="@+id/drawingLayout"     
        android:layout_width="wrap_content"     
        android:layout_height="wrap_content">

    </RelativeLayout>

Now let say main.xml is your main xml layout file then add that class in to that layout as like below:

   setContentView(R.layout.main);

    drawingLayout = (RelativeLayout)findViewById(R.id.drawingLayout);
    System.out.println("The Layout is: "+drawingLayout);
    myView = new MyView(this);
    drawingLayout.addView(myView);

Hope you got the point. If not then let me know.

Enjoy. :))

다른 팁

Yes and Yes, You can have Button Views on the layout but they will exist outside the Canvas but they can overlay the canvas too. Or you can can render your own button directly on the surface and go through the overhead of writing your own touch detection. If you're creating a little framework for a game (or something game-like with "sprites") then the overhead is something you're already doing. Making these buttons look exactly like a Button view may be a bit difficult but it is possible.

This ultimately comes down to a design decision about what is easier or better to implement.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top