Android View zur Laufzeit hinzufügen

E

einmal

Gast
Moin!
Möchte per Button zur Laufzeit Kreise malen lassen. Das Problem ist, dass immer nur der zuletzt gezeichnete Kreis zu sehen ist, die zuvor gezeichneten verschwinden beim Benutzen des Button.

Java:
 public class MainActivity extends Activity {
    	    TextView tv;
    	    EditText et;
    	    LinearLayout ll;
    
    	   public void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);

                     ll = new LinearLayout(this);
    	        ll.setOrientation(android.widget.LinearLayout.VERTICAL);
    	        ll.setLayoutParams(new ViewGroup.LayoutParams(-1,-1));
    	        ll.setBackgroundColor(0x88ff0000);
  
    	        Button btn = new Button(this);
    	        btn.setText("Go!");
    	        btn.setOnClickListener(new Button.OnClickListener() {
    	            public void onClick(View v) {
    				Kreis kreis = new Kreis(v.getContext());
    	            	ll.addView(kreis);
    	            }
    	        });

    	        ll.addView(btn);
    	        setContentView(ll);

    	    }
 }

und

Java:
 public class Kreis extends View {

	private Paint myPaint;
	private static int aufrufe = 0;

	public Kreis(Context context) {
		super(context);
		setPaintAttributes();
		aufrufe += 25;		
	}
	
	private void setPaintAttributes() {
		myPaint = new Paint();
		myPaint.setAntiAlias(true);
		myPaint.setStyle(Paint.Style.FILL);
		myPaint.setColor(Color.GREEN);
		}
		
		protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.WHITE);
		canvas.drawCircle(10.0f, 20.0f + aufrufe, 10.0f, myPaint);
		}
}
 

schlingel

Gesperrter Benutzer
Du zeichnest ja auch nur einen. Wenn du mehrere zeichnen willst, dann musst du dir merken wieviel von was zu zeichnen ist.

onDraw findet das Canvas oder zu Deutsch auch die Leinwand immer leer vor und füllt sie bei jedem Aufruf erneut.
 
E

einmal

Gast
Danke für die Rückmeldung!

hm, gibt es eine Möglichkeit, einfach die "aktuelle Leinwand" (also die schon teilweise bemalte) zu übergeben und auf ihr dann weiterzumalen? (ich weiß nicht, wie ich mit dem canvas umgehe)

Ansonsten meinst du, müsste ich mir irgendwie außerhalb der onDraw-Methode merken, welche Kreise bereits gezeichnet wurden!?! Ich denke das ist als solches nicht allzu problematisch, aber sicherlich nicht sehr elegant, da ich immer das gesamte canvas neuzeichnen müsste. ich will ja aber nur einen kleinen Teil hinzufügen.
Jemand noch eine elegantere Lösung?
 

schlingel

Gesperrter Benutzer
So läuft das nun einmal. Ist ja bei anderen GUI-Frameworks nicht anders. (Swing, Winforms, etc.)

Aber als Alternative kannst du in ein Bitmap zeichnen und diese wieder verwenden.
 
E

einmal

Gast
Ok, dann werd ichs einfach erstmal so speichern!

Das mit dem bitmap find ich auch ne gute Idee, werd ich dann auch noch ausprobieren, da bin ich aber nicht fit drin, da muss ich erst mal schaun, wie das geht! Aber das dann bei Zeiten!

Vielen Dank!
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
W Zur Laufzeit erstelltes MenuItem an eine View binden Android & Cross-Platform Mobile Apps 1
R Android Zugriff auf view von MainActivity Android & Cross-Platform Mobile Apps 7
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 canvas in view anzeigen 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
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
S Android Absoluter Neuling: EditText zur Laufzeit verändern bzw. über Button Android & Cross-Platform Mobile Apps 2
G Android zur Laufzeit den Text im Menü ändern Android & Cross-Platform Mobile Apps 3
F Android Spinner zur Laufzeit mit verschiedenem Inhalt füllen Android & Cross-Platform Mobile Apps 2
D Samsung Tastatur Shortcut hinzufügen Android & Cross-Platform Mobile Apps 4
W Android Wieso kann ich keine ListView mehr zum Layout hinzufügen? Android & Cross-Platform Mobile Apps 1
ruutaiokwu Android Wo das 'android.useAndroidX' property hinzufügen? Android & Cross-Platform Mobile Apps 8
B Android SQLite Tabelle neue Spalten hinzufügen Android & Cross-Platform Mobile Apps 8
J Android Java Packet in Android Java ide hinzufügen. Android & Cross-Platform Mobile Apps 3
V Buttons bei Klick hinzufügen Android & Cross-Platform Mobile Apps 2
V Buttons zur Actionbar hinzufügen Android & Cross-Platform Mobile Apps 4
J Android Verhalten beim Hinzufügen neuer Views? Android & Cross-Platform Mobile Apps 6

Ähnliche Java Themen

Neue Themen


Oben