Canvas von android.view.View?

  • 2 Antworten
  • Letztes Antwortdatum
N

noenglish

Neues Mitglied
0
[FONT=Consolas, monospace]Ich habe eine Activity mit mehreren Objekten (TextView, Button, SeekBar). Dazwischen befindet sich eine View auf die ich zeichnen möchte.
Das ganze sieht jetzt so aus --> Anhang.
[/FONT]

[FONT=Consolas, monospace]Die .xml dazu:
[/FONT]
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginBottom="200dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="30dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/view1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="64dp"
        android:text="Button" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="38dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>
Und die Activity:
Code:
package com.example.zeichneninview;

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

public class MainActivity extends Activity {
    private Paint myPaint;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        /** Attribute für myPaint */
        myPaint = new Paint();
        myPaint.setAntiAlias(true); 
        myPaint.setColor(Color.RED);
        myPaint.setStrokeWidth(4);
        
        View myView = (View) findViewById(R.id.view1);
        myView.setBackgroundColor(Color.CYAN);
    }
    
 // Wie bekomme ich Zugriff auf Canvas von myView             <-- ???
//    @Override
    protected void onDraw(Canvas canvas){ 
        canvas.drawCircle(100.0f, 150.0f, 50.0f, myPaint);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
[FONT=Consolas, monospace]Ich habe aber keine Ahnung wie ich der View eine Canvas zuordne bzw. auf die Canvas der View zugreifen kann.[/FONT]
[FONT=Consolas, monospace]Da ich ganz am Anfang von Java/Android stehe, bin ich auf ausführliche Hilfe angewiesen.[/FONT]
[FONT=Consolas, monospace]Im voraus besten Dank.[/FONT]
[FONT=Consolas, monospace]noenglish[/FONT]
 

Anhänge

  • Activity_1.jpg
    Activity_1.jpg
    6 KB · Aufrufe: 297
Zwei Möglichkeiten:

Du schreibst eine Klasse, die von View erbt.
Diese kann man im Layout ganz normal benutzen:
Code:
<de.foo.bar.mein.MalView ......./>
Du bekommst den Canvas dann im onDraw übergeben.

Oder du malst in eine Bitmap rein, und sagst einem ImageView, dass der diese Bitmap darstellen soll.
 
  • Danke
Reaktionen: Sentenza
Danke für die schnelle Antwort.
noenglish
 

Ähnliche Themen

M
Antworten
21
Aufrufe
1.402
swa00
swa00
Mr-Fisch
Antworten
5
Aufrufe
998
migi01
migi01
Mr-Fisch
Antworten
8
Aufrufe
1.027
Mr-Fisch
Mr-Fisch
M
Antworten
9
Aufrufe
806
mkuz24
M
A
Antworten
5
Aufrufe
707
swa00
swa00
Zurück
Oben Unten