3

I am trying to work through an android example given on the developers' page. It gives 2 ways of drawing on a canvas. The first way is to use a class called CustomDrawableView, which looks like this:

public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;

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

int x = 10;
int y = 10;
int width = 300;
int height = 50;

mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}

protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}

This is then called by the main class:

CustomDrawableView mCustomDrawableView;
protected void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);    
mCustomDrawableView = new CustomDrawableView(this);        
setContentView(mCustomDrawableView);
}

But then the tutorial says:

If you'd like to draw this custom drawable from the XML layout instead of from the Activity, then the CustomDrawable class must override the View(Context, AttributeSet) constructor, which is called when instantiating a View via inflation from XML. Then add a CustomDrawable element to the XML, like so:

<com.example.shapedrawable.CustomDrawableView
   android:layout_width="fill_parent"     
android:layout_height="wrap_content"     
/>

I have successfully got the first example to run but I do not know what my code should look like if I want to adopt the second option. Presumably, I start with @Override View but then I am stuck.

The reason I want to do this is because I want to create a canvas that covers only part of the screen so I can fit buttons and text in the other part.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
Steblo
  • 625
  • 2
  • 12
  • 22

2 Answers2

4

If the tutorial is correct then the only change you make is to add that constructor. You probably want to move the drawable init code to a separate method.

public CustomDrawableView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initDrawable();
}

The xml

<com.example.shapedrawable.CustomDrawableView
   android:id="+id/custom_drawable_view"
   android:layout_width="fill_parent"     
    android:layout_height="wrap_content"     
/>

And you can obtain the view like this.

CustomDrawableView mCustomDrawableView;

protected void onCreate(Bundle savedInstanceState) {    
    super.onCreate(savedInstanceState);          
    setContentView(R.layout.main); // assuming your layout is named main.xml
    mCustomDrawableView = (CustomDrawableView) findViewById(R.id.custom_drawable_view);
}
Robby Pond
  • 73,164
  • 16
  • 126
  • 119
  • Thanks Robby. I don't want to try your patience but how do I now call the activity? previously it was: mCustomDrawableView = new CustomDrawableView(this); setContentView(mCustomDrawableView); – Steblo Feb 22 '11 at 15:15
0

public CustomDrawableView(Context v,AttributeSet as){ super(v,as); drawShape(as); } public void drawShape(AttributeSet ast) {
int x =0; int y=0 ; int width=0; int height=0 ;

            x=Integer.parseInt(ast.getAttributeValue(null, "x"));       
    y=Integer.parseInt(ast.getAttributeValue(null, "y"));
    width=Integer.parseInt(ast.getAttributeValue(null, "width"));
    height= Integer.parseInt(ast.getAttributeValue(null, "height"));

    mDrawable = new ShapeDrawable(new OvalShape());
    mDrawable.getPaint().setColor(0xff74AC23);
    mDrawable.setBounds(x, y, x + width, y + height);

} }

MainActivity.java{ CustomDrawableView mCustomDrawableView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 }
public View.OnClickListener cc = (new View.OnClickListener() {
    public void onClick(View view) {
                    setContentView(R.id.myid);}
       });       

} This works fine..(Not so sure whether this is the best way to do it...)

Sora
  • 409
  • 5
  • 6