0

Android カスタム ビュー クラスのサブクラスで四角形を描画する問題に直面しています。スーパークラスのonDrawメソッドが機能するたびに、サブクラスのonDrawメソッドは実行されませんでした。スーパークラスは四角形を描画し、サブクラスはスーパークラスが描画した四角形内に 4 つの四角形を描画します。この問題を解決できません。助けてください。

これが私のサンプルコードです。

スーパークラス:

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);     
    }
}

サブクラス:

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>

この問題に関して私を助けてください。うまくいけば、すぐに返事が来るでしょう。

4

1 に答える 1

1

私の推測では、レイアウト(質問を編集してレイアウトを含めてください)は、高さまたは幅が0になるようにColorFanViewインスタンスを定義しています。したがって、親ビューはそれらを描画しません。

2011年7月27日編集:HabiburRahmanがレイアウトXMLを質問に追加しました。これが新しい答えです:

2つのクラスは機能しますが、間違ったクラスをレイアウトに追加しました(ColorFanStack代わりにを使用する必要がありますColorFanView)。のインスタンスはColorFanStack、の描画を継承します(メソッドが呼び出すColorFanViewという事実により)。それがあなたが達成しようとしていた行動だったと思います。ColorFanStack.onDraw()super.onDraw()

クラスを定義したときにクラスで使用したXMLは次のとおりです。

<?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>

ここに画像の説明を入力してください

于 2011-07-25T20:30:18.480 に答える