Graphic and Button at the Display

  • Replies:0
Berni33
  • Forum posts: 5

Feb 15, 2017, 10:00:47 AM via Website

Hello,
I am a beginner in Android Studio. In the moment I create an app where I can draw some lines with canvas. It works really good, but I also want to have a menue or a button on the display. But this is very difficult.

With the order at the end of MainActivity.java
setContentView(view)
I get the graphic (canvas) to my display.

With the order at the end of MainActivity.java
setContentView(R.layout.activity_main)
I get the button to my display.

But I want to have both at my dislay, the graphic and the button. Someone gave my the tip, that I had to insert a "view" - order in the layout "activity_main.xml". But in the moment I do not have the knowledge for this.
If someone has an idea how it works, that both, the graphic and the button is at the display or how the view-order for the layout "activity_main.xml" had to look, it would be very good.

Regards, Berni

Here is the code of the MainActivity.java:

a quote goes herepackage examplecanvas.com.imagemove;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.graphics.Color;

public class MainActivity extends Activity {

public class DisplayView extends View
{
    int positionX = 5;
    int positionY = 15;

    int positionX_a;
    int positionY_a;

    int positionX_aa;
    int positionY_aa;

    int kkk = 1;
    int[] x_pos = new int[500];
    int[] y_pos = new int[500];

    int x_Sol, y_Sol;
    int x_Text, y_Text, Text_Size;

    String C_TeSize;

    Paint mPaint;
    DisplayView(Context context)
    {
        super(context);
        mPaint = new Paint();

        mPaint.setColor(0xFF0000FF);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        switch(event.getAction())
        {

// Ermittlung der Touch-Koordinate

            case MotionEvent.ACTION_DOWN: {
                positionX = (int)event.getX();
                positionY = (int)event.getY();

                invalidate();
            }
        }
        return true;
    }
    @Override
    public void onDraw(Canvas canvas)
    {
 //  Berechnung der Textgröße und Textposition

// Auflösung ermitteln

        x_Sol = getWidth();
        y_Sol = getHeight();

 // Textgröße und Textposition
        C_TeSize = String.valueOf(x_Sol);
        Text_Size = x_Sol / 23;
        x_Text = x_Sol / 2 - 37 * Text_Size/4;
        y_Text = Text_Size * 1;

        mPaint.setTextSize(Text_Size);   // Textgröße

//--------------------------------------------------------------------------------------------

        System.out.println("X & Y"+positionX+":"+positionY);

//-------------------------------------------------------------------------------------------

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        canvas.drawPaint(paint);
        paint.setColor(Color.RED);
        canvas.drawLine( positionX_a, positionY_a, positionX, positionY, paint);
        positionX_a = positionX;
        positionY_a = positionY;

        mPaint.setColor(Color.RED);
        canvas.drawText("Android App", x_Text, y_Text, mPaint);

        //  Dreieck
        canvas.drawLine( 130, 620, 150, 640, paint);
        canvas.drawLine( 150, 640, 110, 640, paint);
        canvas.drawLine( 110, 640, 130, 620, paint);

        //  Rollen
        canvas.drawCircle(120,646,5,paint);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(120,646,4,paint);

        paint.setColor(Color.RED);
        canvas.drawCircle(140,646,5,paint);
        paint.setColor(Color.WHITE);
        canvas.drawCircle(140,646,4,paint);

        //  Dreieck
        paint.setColor(Color.RED);
        canvas.drawLine( 430, 620, 450, 640, paint);
        canvas.drawLine( 450, 640, 410, 640, paint);
        canvas.drawLine( 410, 640, 430, 620, paint);

        canvas.drawLine( 438, 649, 444, 640, paint);
        canvas.drawLine( 426, 649, 432, 640, paint);
        canvas.drawLine( 414, 649, 420, 640, paint);

        //  Balken
        paint.setColor(Color.BLACK);
        canvas.drawLine( 100, 619, 460, 619, paint);

        paint.setColor(Color.RED);
        canvas.drawLine( 100, 618, 460, 618, paint);

        paint.setColor(Color.BLACK);
        canvas.drawLine( 100, 617, 460, 617, paint);


        x_pos[1] = 1;
        x_pos[kkk] = +positionX;
        y_pos[kkk] = +positionY;

        kkk = kkk + 1;

        for(int i=1; i<=10; i++)
        {
  //          canvas.drawLine( positionX_aa, positionY_aa, x_pos[i], y_pos[i], paint);
  //          positionX_aa = x_pos[i];
  //          positionY_aa = y_pos[i];

            System.out.println("Control " + i);
        }
    }
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DisplayView view = new DisplayView(this);

    setContentView(view);   // Here you see the graphic at the display 
    setContentView(R.layout.activity_main);   // Here you see the button at the display 

}

}

Reply