canvas in view anzeigen

G

Gast2

Gast
Hi,

ich habe eine canvas erstellt und möchte diese nun auf meinem view anzeigen lassen.
Leider bekomme ich das nicht hin,
meine canvas wird nicht angezeigt.
Hier meine Quellen...
vielleicht kann mir jemand sagen (schreiben) was fehlt?!

activity_main.xml
[XML]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativ_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/txv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="26dp"
android:text="@string/hello_world"
android:textSize="30dp"
tools:context=".MainActivity" />

<ivi.uebungondraw2.DrawView
android:layout_below="@+id/txv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

</RelativeLayout>[/XML]

MainActivity.java
Java:
package ivi.uebungondraw2;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

	DrawView drawView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		drawView = new DrawView(this);
	}
}

DrawView.java
Java:
package ivi.uebungondraw2;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.View;

public class DrawView extends View {
	Bitmap bitmap;
	Canvas bitmapCanvas;

	boolean isInitialized;
	Paint paint = new Paint();

	public DrawView(Context context) {
		super(context);

		paint.setColor(Color.BLACK);
		paint.setStyle(Style.FILL_AND_STROKE);

		isInitialized = false;
	}

	private void init() {
		bitmap = Bitmap.createBitmap(470, 320, Bitmap.Config.RGB_565);

		bitmapCanvas = new Canvas();
		bitmapCanvas.setBitmap(bitmap);
		bitmapCanvas.drawColor(Color.rgb(250, 250, 250));

		isInitialized = true;

		bitmapCanvas.drawLine(10, 10, 10, 300, paint);
		bitmapCanvas.drawLine(10, 160, 310, 160, paint);
		invalidate();
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (!isInitialized)
			init();
		canvas.drawBitmap(bitmap, 0, 0, paint);
	}
}

? ? ?
 
T

Tomate_Salat

Gast
Was soll den passieren? Du definierst einmal deinen View im XML und einmal erstellt du diesen View im Source, weist ihn aber nirgends zu.

Ich bin mir nicht sicher, aber ich glaube: Eigene Views fürs XML müssen den View-Konstruktor mit 2 Parametern bereitstellen [wobei ich generell immer alle 3 Konstruktoren vom View implementiere).
Zudem: wenn du mit dem View arbeiten willst, würde ich mit IDs arbeiten.
 
G

Gast2

Gast
Ok, ID füge ich hinzu und auch die drei Parameter.

ABER so wie der Quelltext oben zu sehen ist gibt es eine InflateException in der xml, Zeile 19
[XML] <ivi.uebungondraw2.DrawView[/XML]

dabei habe ich den namespace mit copy + paste übernommen, Fehler in der Schreibung halte ich für ausgeschlossen.

Warum kann die canvas denn nicht inflated werden?
 
T

Tomate_Salat

Gast
Zeig mal bitte deinen angepassten DrawView und poste sicherheitshalber mal noch die genaue Fehlermeldung.
 
G

Gast2

Gast
DrawView:
Java:
package ivi.uebungondraw2;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class DrawView extends View {
	
	boolean isInitialized;
	int hoehe;
	Paint paint = new Paint();
	Bitmap bitmap;
	Canvas bitmapCanvas;

	public DrawView(Context context, AttributeSet attrs, int height) {
		super(context, attrs);
		hoehe = height;
		paint.setColor(Color.BLACK);
		paint.setStyle(Style.FILL_AND_STROKE);
		isInitialized = false;
	}

	private void init() {
		bitmap = Bitmap.createBitmap(470, hoehe, Bitmap.Config.RGB_565);
		bitmapCanvas = new Canvas();
		bitmapCanvas.setBitmap(bitmap);
		bitmapCanvas.drawColor(Color.rgb(250, 250, 250));
		isInitialized = true;
		bitmapCanvas.drawLine(10, 10, 10, 300, paint);
		bitmapCanvas.drawLine(10, 160, 310, 160, paint);
		invalidate();
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (!isInitialized) init();
		canvas.drawBitmap(bitmap, 0, 0, paint);
	}
}

die Fehlermeldung (LogCat):
fehler28.bmp
 
Zuletzt bearbeitet von einem Moderator:
T

Tomate_Salat

Gast
View | Android Developers

Schau nochmal in die Dokumentation. Du brauchst du diesen Konstruktor:
Java:
public DrawView(Context c,AttributSet a) {
}

Doku hat gesagt.:
public View (Context context, AttributeSet attrs)

Since: API Level 1
Constructor that is called when inflating a view from XML. This is called when a view is being constructed from an XML file, supplying attributes that were specified in the XML file. This version uses a default style of 0, so the only attribute values applied are those in the Context's Theme and the given AttributeSet.
 
G

Gast2

Gast
Oh, super!
Vielen Dank!

Jetzt wird die canvas dargestellt.

Aber leider nicht unter dem TextView, so wie im RelativeLayout angegeben...
[XML]android:layout_below="@+id/txv"[/XML]

Es sollte unter dem Text erscheinen.
Statt dessen erscheint es als einziges und am oben auf dem Bildschirm.
 
T

Tomate_Salat

Gast
Versuch es doch mal über den GUI-Editor zu positionieren. Sonst arbeite ich immer mit der XML, aber beim RelativeLayout greife ich gerne auf den GUI-Editor zurück, der ist eigentl. ziemlich gut.
 
G

Gast2

Gast
Es wird im GUI-Editor richtig dargestellt,
nur leider nicht auf dem Endgerät und auch nicht auf dem virtual device,
nicht mal das margin_top 50dp.

gui_edit01.bmp
 
Zuletzt bearbeitet von einem Moderator:
G

Gast2

Gast
Jetzt hab ich's!!!

Aus der MainActivity.java
musste ich
Java:
setContentView(drawView);
löschen.

Auf ins Wochenende! :)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
Arif Android Canvas wird nicht gezeichnet? Android & Cross-Platform Mobile Apps 0
Dechasa Android Android - Canvas NullpointException Android & Cross-Platform Mobile Apps 2
Z Canvas onDraw() Variablen muss in der Methode initialisiert werden Android & Cross-Platform Mobile Apps 1
F Mit Canvas zeichen und durch einen Timer aktualiesieren Android & Cross-Platform Mobile Apps 1
J Canvas mit dem Accelorometersensor bewegen Android & Cross-Platform Mobile Apps 0
E Android Zeichnen auf Canvas - Teilbereich als Bild speichern Android & Cross-Platform Mobile Apps 3
B onDraw Canvas jede Millisekunde aufrufen? Android & Cross-Platform Mobile Apps 12
S Android Canvas - drawText - Update Text in ActionListener Android & Cross-Platform Mobile Apps 8
B Java ME Canvas Android & Cross-Platform Mobile Apps 2
J Java ME Canvas Klasse Gameloop und Funktion Android & Cross-Platform Mobile Apps 2
B Java ME Leere Canvas Seite Android & Cross-Platform Mobile Apps 21
P Java ME Texteingabe Canvas Android & Cross-Platform Mobile Apps 2
D wie geht Klassenvererbung von Canvas Klassen Android & Cross-Platform Mobile Apps 2
U Probleme mit der drawString Methode bei Canvas Android & Cross-Platform Mobile Apps 8
H Canvas kann nichts auf Graphics drawen Android & Cross-Platform Mobile Apps 3
M Textfeld in Canvas, implementation der keys aendern Android & Cross-Platform Mobile Apps 8
M Text in Canvas scrollen lassen Android & Cross-Platform Mobile Apps 13
M Wie in einer anderen "nicht canvas" klasse zeichne Android & Cross-Platform Mobile Apps 5
U ein texteingabefeld auf einem Canvas . Android & Cross-Platform Mobile Apps 2
A Problem: Canvas-Grösse Motorola RAZR v3r Android & Cross-Platform Mobile Apps 8
F Canvas Stringlänge Android & Cross-Platform Mobile Apps 2
R Android Zugriff auf view von MainActivity Android & Cross-Platform Mobile Apps 7
W Zur Laufzeit erstelltes MenuItem an eine View binden Android & Cross-Platform Mobile Apps 1
W Bild aus dem Internet in View bzw. ImageView laden (Fragment) Android & Cross-Platform Mobile Apps 2
J View Breite/Höhe bestimmen Android & Cross-Platform Mobile Apps 4
B Android In einem View der ersten Activity zweite anzeigen Android & Cross-Platform Mobile Apps 2
S Dynamische EditText View eingaben in Datenbank speichern Android & Cross-Platform Mobile Apps 0
V PopUp in gleicher View anzeigen Android & Cross-Platform Mobile Apps 1
M Android Suche Activity/View Namen Android & Cross-Platform Mobile Apps 1
R Android Warum (View view)? Android & Cross-Platform Mobile Apps 4
J Android neue View mit OnTouchListener Android & Cross-Platform Mobile Apps 0
B Eigene View xml-Layout einbinden Android & Cross-Platform Mobile Apps 1
R Problem mit View in ScrollView Android & Cross-Platform Mobile Apps 6
R Android Android.view Serializable ? Android & Cross-Platform Mobile Apps 3
M Android View zu View hinzufügen Android & Cross-Platform Mobile Apps 4
M Activity wechseln aus List View mit Android & Cross-Platform Mobile Apps 2
M Suche Name von View Komponente Android & Cross-Platform Mobile Apps 10
G Fehlermeldung: "No XML content. Please add a root view or layout to your documet." Android & Cross-Platform Mobile Apps 7
E Android View zur Laufzeit hinzufügen Android & Cross-Platform Mobile Apps 4
N neuen view öffnen Android & Cross-Platform Mobile Apps 13
J id's von view komponenten werden nicht gefunden Android & Cross-Platform Mobile Apps 2
C Panel/View für Android Android & Cross-Platform Mobile Apps 3
tfa Android Layout-Probleme: View programmatisch erweitern (addContentView) Android & Cross-Platform Mobile Apps 7
G Dynamische View Inhalt -> Lagesensor Android & Cross-Platform Mobile Apps 3
W Bilder und Videos anzeigen lassen mit Zoom- und Loop-Funktion Android & Cross-Platform Mobile Apps 5
R Android Daten speichern und anzeigen Android & Cross-Platform Mobile Apps 7
W Android Kann keine ListView mehr in der MainActivtiy anzeigen, obwohl noch sehr viel Platz frei ist Android & Cross-Platform Mobile Apps 1
ruutaiokwu Android In einem Android-“Spinner”-Element GLEICHZEITIG Bild (links) UND Text (rechts) anzeigen Android & Cross-Platform Mobile Apps 0
J ImageView: Bilder in niedriger Grafik anzeigen lassen Android & Cross-Platform Mobile Apps 2
M ImageButton: Bild ausblenden und Text anzeigen Android & Cross-Platform Mobile Apps 2
E Android Naturkatastrophen auf App anzeigen Android & Cross-Platform Mobile Apps 1
L Dialog anzeigen wenn auf Button gedrückt wird. Android & Cross-Platform Mobile Apps 4
C Android Bei Text "Popup" anzeigen Android & Cross-Platform Mobile Apps 2
B Android App debugen auf Handy, Handy-Desktop auf PC anzeigen Android & Cross-Platform Mobile Apps 1
J (Android) Bild(er) anzeigen lassen Android & Cross-Platform Mobile Apps 0
J Verschiedene xml layouts in relativlayout anzeigen lassen Android & Cross-Platform Mobile Apps 1
P Android Kontakt anzeigen lassen Android & Cross-Platform Mobile Apps 1
M Anzeigen der Daten dauert zu lange - Progressdialog? Android & Cross-Platform Mobile Apps 5
E Android Zwei Canvase übereinander legen und anzeigen Android & Cross-Platform Mobile Apps 7
G Bilder als "Video" anzeigen Android & Cross-Platform Mobile Apps 3
S Android Fehler beim Anzeigen meines Apps auf Galaxy Tab Android & Cross-Platform Mobile Apps 4
J Schlicht und einfach Bild anzeigen ;) Android & Cross-Platform Mobile Apps 5
L HTML Seite anzeigen in Handy-Applikation Android & Cross-Platform Mobile Apps 5
P Bild anzeigen Android & Cross-Platform Mobile Apps 3
M Bilder nacheinander anzeigen Android & Cross-Platform Mobile Apps 6

Ähnliche Java Themen

Neue Themen


Oben