Frage

Ich kann ein Problem haben, um Rechteck in der Unterklasse meiner Android Custom View -Klasse zu zeichnen. Jedes Mal, wenn Super Class OnDraw -Methode funktioniert. Die Superklasse zeichnet ein Rechteck und Unterklasse zeichnet 4 Rechteck in das Superklassen-Rechteck. Ich kann dieses Problem nicht beheben. Bitte helfen Sie mir.

Hier ist mein Beispielcode.

Superklasse:

public class ColorFanView extends View{

    public ShapeDrawable[] mDrawables;

    public ColorFanView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);    
    }
    public ColorFanView(Context context, AttributeSet attrs) {
        super(context, attrs);  
    }
    public ColorFanView(Context context) {
        super(context);
    }
    @Override 
    protected void onDraw(Canvas canvasObject) {
        super.onDraw(canvasObject);
        int x = 100;
        int y = 100;
        int width = 80;
        int height = 200;
        Paint thePaint = new Paint();   
        thePaint.setColor(Color.WHITE);
        RectF rectnagle1 = new RectF(x,y,x+width,y+height);
        canvasObject.drawRoundRect(rectnagle1, 10.0f, 10.0f, thePaint);     
    }
}

Unterklasse:

public class ColorFanStack extends ColorFanView{

    public ColorFanStack(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public ColorFanStack(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ColorFanStack(Context context) {
        super(context);
        initView();
    }
    public void initView() {

        mDrawables = new ShapeDrawable[4];
        float[] outerR1 = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
        mDrawables[0] = new ShapeDrawable(new RoundRectShape(outerR1, null, null));
        mDrawables[0].getPaint().setColor(Color.RED);
        mDrawables[1] = new ShapeDrawable(new RectShape());
        mDrawables[1].getPaint().setColor(Color.WHITE); 
        mDrawables[2] = new ShapeDrawable(new RectShape());
        mDrawables[2].getPaint().setColor(Color.BLUE);
        mDrawables[3] = new ShapeDrawable(new RectShape());
        mDrawables[3].getPaint().setColor(Color.YELLOW);
    }
    @Override 
    protected void onDraw(Canvas canvasObj) {
        super.onDraw(canvasObj);
        int x = 100;
        int y = 100;
        int width = 80;
        int height = 40;
        int canvasSpace =5;
        for (Drawable dr : mDrawables) {
            dr.setBounds(x, y, x + width, y + height);
            dr.draw(canvasObj);
            y += height + canvasSpace;
        }
    }
}

Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myViewGroup" android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <com.test.colorfan.ColorFanView
        android:layout_width="200dip" android:layout_height="400dip"
        android:id="@+id/firstView" />

</RelativeLayout>

Bitte helfen Sie mir in Bezug auf dieses Problem. Hoffentlich bekomme ich bald eine Antwort.

War es hilfreich?

Lösung

Meine Vermutung ist, dass Ihr Layout (bitte bearbeiten Sie die Frage, um Ihr Layout einzuschließen) Ihre Colorfanview -Instanzen so definieren, dass sie 0 Höhe oder Breite haben. Daher zeichnet die Elternansicht sie nicht.

Bearbeiten 27.07.2011: Habibur Rahman hat die Frage sein Layout XML hinzugefügt. Dies ist die neue Antwort:

Ihre beiden Klassen funktionieren, aber Sie haben Ihr Layout falsch hinzugefügt (Sie hätten verwenden sollen ColorFanStack Anstatt von ColorFanView). Eine Instanz von ColorFanStack wird die Zeichnung von erben ColorFanView (aufgrund der Tatsache, dass Ihre ColorFanStack.onDraw() Methodenaufrufe super.onDraw()). Ich denke, das war das Verhalten, das Sie erreichen wollten.

Hier ist der XML, den ich mit den Klassen verwendet habe, wie Sie sie definiert haben:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
     <com.habecker.demo.ColorFanStack
        android:layout_width="200dip" android:layout_height="400dip"
        android:id="@+id/secondView" />

</RelativeLayout>

enter image description here

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top