View neu zeichnen

C

canogretic

Neues Mitglied
0
Hi,

ich bin gerade dabei mich etwas mit der Android App Programmierung zu beschäftigen. Allerdings habe ich jetzt ein paar Schwierigkeiten bei meinem Beispielcode.

Ich habe eine Klasse erstellt welche von der Oberklasse View erbt. Diese Klasse wird in einer Activity Klasse aufgerufen. In der XML Datei für die Activity gibt es einen Button und das vorhandene View. Durch die onClick Methode die in der XML Datei definiert ist wird eine Funktion test in der Activity Klasse aufgerufen. In dieser habe ich jetzt ein neues Objekt meiner View Klasse erzeugt und wollte anschließend über eine set Funktion die Farbe meines Paint Objektes ändern und anschließend das ganze über Objektnamen.invalidate() neu zeichnen. Allerding wird bei der programmierten App das View in der Activity nicht neu gezeichnet.

Die Activity
Code:
package de.alex.helloworld;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;



public class CanvasTextActivity extends Activity {
    /** Called when the activity is first created. */
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.canvas);
     } 
    
    public void test(View view) {
        CanvasTextView cview = new CanvasTextView(this);
        cview.setColor(Color.BLUE);
        cview.invalidate();

    }
}
Das erstellte View (Canvas)
Code:
package de.alex.helloworld;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class CanvasTextView extends View {

    protected int color = Color.WHITE;
    protected Canvas canvas = new Canvas();
    protected Paint paint = new Paint();
      
    public CanvasTextView(Context context) {
        super(context);
        color = Color.RED;
    }
    
     public CanvasTextView(Context context, AttributeSet attrs) {
           super(context, attrs);
           color = Color.RED;
        }
    
    
       public CanvasTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }

    @Override
    protected void onDraw(Canvas canvas) {
        int height = canvas.getHeight() / 2;
        int width = canvas.getWidth();
        float y1 = (float) 40;
        float y2 = (float) 80;
        float z1 = ((float)Math.round(width/2));
        float z2 = ((float)Math.round(height/2));
        //canvas.drawRect(0, 0, width, height, this.paint);
        paint.setColor(this.getColor());
        this.paint.setStyle(Paint.Style.FILL);
        canvas.drawText("Das ist ein test", y1, y2, paint);
        invalidate();
    }
    
    public Canvas getCanvas() {
        return this.canvas;
    }
    
    public Paint getPaint() {
        return this.paint;
    }
    
    public void setPaintColor(int color) {
        this.paint.setColor(color);
        invalidate();
    }
    
    public int getColor() {
        return this.color;
    }
    
    public void setColor(int color) {
        this.color = color;
    }

}
Die dazugehörige XML Datei
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"></LinearLayout

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="vertical">

        <de.alex.helloworld.CanvasTextView
            android:id="@+id/canvasTextView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2" />

  <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="test"
            android:text="@string/menge" 
            android:layout_weight="1" />

    </LinearLayout>
    
</LinearLayout>
    
</LinearLayout>
Vielleicht kann mir ja jemand einen Hinweis geben was an meiner Überlegung falsch ist. Wenn ich in der Funktion die das Anklicken des Buttons aufruft

Code:
setContentView(cview)
aufrufe, wird das View mit der entsprechenden Farbe erstellt und angezeigt. Allerdings fehlen dann die Buttons für weitere Methodenaufrufe.
 
Zuletzt bearbeitet:

Ähnliche Themen

D
  • Data2006
Antworten
14
Aufrufe
448
jogimuc
J
H
Antworten
1
Aufrufe
1.225
jogimuc
J
5
Antworten
22
Aufrufe
1.366
590239
5
Zurück
Oben Unten