开发者

How onDraw method works in subclass of android-custom-view class?

I facing problem to draw rectangle at subclass of my android custom view class. Each time super class onDraw method works.But subclass onDraw method never executed. Super class will draw a rectangle and subclass will draw 4 rectangle within the super-class drawn rectangle.I can't fixed this problem.please help me.

Here is my sample code.

SuperClass:

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

Subclass:

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 };
        m开发者_开发问答Drawables[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>

Please help me regarding this issue. Hopefully, I will get a reply soon.


My guess is that your layout (please edit the question to include your layout), is defining your ColorFanView instances in such a way that they have 0 height or width; therefore, the parent View does not draw them.

EDIT 7/27/2011: Habibur Rahman added his layout XML to the question. This is the new answer:

Your two classes work, but you added the wrong one to your layout (you should have used ColorFanStack instead of ColorFanView). An instance of ColorFanStack will inherit the drawing of ColorFanView (by virtue of the fact that your ColorFanStack.onDraw() method calls super.onDraw()). I think that that was the behavior that you were trying to achieve.

Here is the XML that I used with the classes as you defined them:

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

How onDraw method works in subclass of android-custom-view class?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜