Hallo,
versuche gerade eine APP zu entwickeln, mit der man grafische Darstellungen mit Canvas machen kann.
Leider erscheint die Grafik nicht auf dem Display. Das Menue (PopUp-Fenster) funktioniert.
Kompilieren läuft auch ohne Fehler durch - bloß eben keine Grafik!
MainActivity.java und activity_main.xml sind eingefügt.
Gruß
Berni
Code
package com.example.pastorek.myapplication1_1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
// public class MainActivity extends Activity {
Paint paint;
Canvas c;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.activity_main);
//--------------------------------------------------------------------
// Grafikbefehle
// editText = (EditText) findViewById (R.id.editText1);
// display = (TextView)findViewById(R.id.display);
c = new Canvas();
c.drawColor(Color.CYAN);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4.5f);
c.drawLine( 10, 10, 100, 100, paint);
c.drawCircle(50,50,30,paint);
//-------------------------------------------------------------------
DisplayView view = new DisplayView(this);
setContentView(view);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,Pop.class));
}
});
}
//-----------------------------------------------------------
// Grafikbefehle
public class DisplayView extends View
{
int positionX = 5;
int positionY = 15;
int positionX_a;
int positionY_a;
String C_TeSize;
Paint mPaint;
public DisplayView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(0xFF0000FF);
}
@Override
public boolean onTouchEvent (MotionEvent event){
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:{
positionX = (int) event.getX();
positionY = (int) event.getY();
invalidate();
}
}
return true;
}
public void onDraw (Canvas canvas){
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 = positionX;
}
// protected void onCreate(Bundle savedInstanceState) {
// public void onCreate (Bundle savedInstanceState) {
// super.onCreate ( savedInstanceState );
// DisplayView view = new DisplayView(this);
// setContentView(view);
// }
}
}
Alles anzeigen
Code: activity_main.xml
<?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="com.example.pastorek.myapplication1_1.MainActivity">
<Button
android:text="POP UP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Alles anzeigen