Hallo,
ich bin gerade dabei meine erste APP zu erstellen und sie funktioniert auch schon ganz gut (Zeichnen mit Canvas). Nun möchte ich ein Menue erstellen, ich habe einen Button in "activity_main.xml" erstellt, er erscheint aber nicht.
Ich füge den Code von "MainActivity.java" und "activity_main.xml" mit ein.
Gruß
Berni
Code
package 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;
import android.widget.Button;
public class MainActivity extends Activity {
//--------------------------------------------------------
public void buttonOnClicked (View v){
Button button = (Button) v;
((Button) v). setText("Hallo");
}
//--------------------------------------------------------
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);
}
}
Alles anzeigen
Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="examplecanvas.com.imagemove.MainActivity">
<Button
android:text="D R Ü C K E N"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginBottom="84dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="buttonOnClick"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Alles anzeigen