Android View ???????

  • 6 Antworten
  • Letztes Antwortdatum
M

midi1007

Neues Mitglied
0
Hallo,

ich habe in der activity_main.xml ein view im graphical Layout hinzugefügt, dieses füllt nicht den ganzen Bildschirm aus da noch Platz für andere Dinge benötigt wird.

Das Problem ist, dass das Bild immer über die gesamte Fläche gezeichnet wird.
Zeichnen tut er was ich möchte aber nicht in das von mir vorgegebene view Fenster...warum?

Hier mal der Code der Klasse MainActivity, der Klasse DrawView und activity_main.xml
Code:
package de.example.view;

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

public class MainActivity extends Activity {
    
    public View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        view = (View) this.findViewById(R.id.view1);
        
        view = new DrawView(this);
        view.setBackgroundColor(Color.BLUE);
        setContentView(view);    
    }
}
Draw View:
Code:
import java.text.DecimalFormat;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import android.view.SurfaceView;
import android.view.View;

@SuppressLint("DrawAllocation")
public class DrawView extends SurfaceView {
    Paint paintBlack = new Paint();
    Paint paintText = new Paint();
    Paint paintGray = new Paint();
    Paint paintBlue = new Paint();
    Paint paintRed = new Paint();
    Paint paintW = new Paint();
    Paint paintGreen = new Paint();
    double xstart, ystart;
    double xsize, ysize;

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

    }

    @SuppressLint("DrawAllocation")
    @Override
    public void onDraw(Canvas canvas) {
        paintText.setColor(Color.GRAY);
        paintText.setStrokeWidth(1);
        paintText.setTextSize(20);
        paintBlack.setColor(Color.BLACK);
        paintBlack.setStrokeWidth(2);
        paintGray.setColor(Color.GRAY);
        paintGray.setStrokeWidth(2);
        paintBlue.setColor(Color.BLUE);
        paintBlue.setStrokeWidth(2);
        paintRed.setColor(Color.RED);
        paintRed.setStrokeWidth(2);
        paintGreen.setColor(Color.CYAN);
        paintGreen.setStrokeWidth(2);

        int y = getHeight();
        int x = getWidth();
        int centerX = x / 2;
        int centerY = y / 2;

        xstart = centerX;
        ystart = centerY;

        canvas.drawText("Im {Z}", centerX + 8, 25, paintText);
        canvas.drawText("Re {Z}", x - 55, centerY - 13, paintText);
        canvas.drawLine(5, centerY, x - 5, centerY, paintGray);// horiz
        canvas.drawLine(centerX, 5, centerX, y - 5, paintGray);// vertik

        //pList.erstes_Element();

        // canvas.drawCircle((float) xstart, (float) ystart, 1, paintRed);
        canvas.drawPoint((float) xstart, (float) ystart, paintRed);

        DecimalFormat f = new DecimalFormat("#0.00");

        canvas.drawText("joo", centerX - 48, (int) ystart + 5, paintBlue);

        canvas.drawText("jo", (int) xstart - 15, centerY + 20, paintBlue);

        canvas.drawLine(centerX - 5, (int) ystart, centerX + 5, (int) ystart,
                paintBlack);// horiz

        canvas.drawLine((int) xstart, centerY - 5, (int) xstart, centerY + 5,
                paintBlack);// vertik
        //pList.naechstes_Element();
    }

}

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"
    tools:context=".MainActivity" >

    <View
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Vielen Dank schonmal
 
A

Anzeige

  • Gerade eben
  • Neu
Lass mal das letzte setContentView in der Activity weg.

Gesendet von meinem Galaxy Nexus mit der Android-Hilfe.de App
 
Leider geht das auch nicht.
 
Diese Zeile:
Code:
view = new DrawView(this);
ist das Problem. Wenn du dein DrawView nutzen willst musst du es entweder im xml-File ansprechen, also statt
Code:
<View         
android:id="@+id/view1"        
android:layout_width="wrap_content"         
android:layout_height="300dp"        
android:layout_centerHorizontal="true" />
sowas wie
Code:
<de.example.view.DrawView         
android:id="@+id/view1"         
android:layout_width="wrap_content"         
android:layout_height="300dp"         
android:layout_centerHorizontal="true" />
oder du erzeugst erst in Code ein DrawView und setzt auch da die Layoutparamter entsprechend.
 
Also wenn ich das DrawView in der XML so anspreche:

Code:
<de.example.view.DrawView          android:id="@+id/view1"          android:layout_width="wrap_content"          android:layout_height="300dp"          android:layout_centerHorizontal="true" />

bekomme ich im LogCat:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.example.view/de.example.view.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class de.example.view1.DrawView
 
Hey kannst du im Layout editor nicht deine DrawView aus den Vustom Views heraus in das layout ziehen?
 
Ja, das geht auch. Bewirkt aber nicht anderes als die manuelle Eingabe in der XML.

Habe es jetzt hin bekommen :)

man muss im DrawView die folgenden Konstruktoren ergänzen, dann geht es.

Code:
    public DrawView(Context context) {
        super(context);
    }

    public DrawView(Context context, AttributeSet attrs) {

        super(context, attrs);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {

        super(context, attrs, defStyle);
    }

Vielen Dank an alle :)
 

Ähnliche Themen

asconsulting
Antworten
5
Aufrufe
142
LonelyGremlin
LonelyGremlin
Katharina1985
Antworten
9
Aufrufe
610
Anz
Anz
M
Antworten
21
Aufrufe
1.787
swa00
swa00
Mr-Fisch
Antworten
5
Aufrufe
1.128
migi01
migi01
Mr-Fisch
Antworten
8
Aufrufe
1.132
Mr-Fisch
Mr-Fisch
Zurück
Oben Unten