1

I've got a problem which I don't know how to solve. Please, help me if you can. In my app I have to create a custom view extended View. In this view I should draw a lot of rectangles and I create them by canvas.drawRect or canvas.drawRoundRect. It's clear. But I want to create a compound design of these rectangles (with gradients, corners, paddings and etc.) and I want to carry out these settings (gradients, corners, paddings and etc.) in XML. How can I do it? The problem is that I determine shape in XML I can use this drawable only as background but when I draw a rectangle I can't set background for rectangle. Maybe there are another way to solve the problem. Could I use the XML shape object for setting not only as background but a view object with x,y-coordinates and width, height?

Edit: I can draw rectangle:

canvas.drawRect(x1, y1, x2, y2, paint);

but I have rectangle settings in XML like this:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

<!-- Specify a gradient for the background -->
    <gradient
    android:angle="90"
    android:startColor="#55000066"
    android:centerColor="#FFFFFF"
    android:endColor="#55000066" />

<!-- Specify a dark blue border -->
    <stroke 
    android:width="2dp"
    android:color="#000066" />

<!-- Specify the margins that all content inside the drawable must adhere to -->
    <padding
    android:left="5dp"
    android:right="5dp"
    android:top="5dp"
    android:bottom="5dp" />

<corners
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="10dp" />
</shape>

and I want to apply this settings to my rectangle. How?

Alex Zezekalo
  • 1,031
  • 2
  • 17
  • 34
  • Your question is a bit unclear. Could you try to edit it to be more concise on what you are asking for? – O'Mutt Oct 10 '12 at 14:13

1 Answers1

6

You can load and use the XML defined drawable from code like so:

public class CustomView extends View {

    Drawable shape;

    public CustomView(Context context) {
        super(context);
        shape = context.getResources().getDrawable(R.drawable.shape);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        shape.setBounds(left, top, right, bottom);
        shape.draw(canvas)
    }

    // ... Additional methods omitted for brevity

}
nEx.Software
  • 6,782
  • 1
  • 26
  • 34
  • it should be casted to GradientDrawable, not to ShapeDrawable because it would be thrown error java.lang.ClassCastException: android.graphics.drawable.GradientDrawable – Alex Zezekalo Oct 10 '12 at 15:40
  • Sounds plausible. When I originally wrote my answer the XML was not included in the question. In reality, you don't need to cast it at all if you are not using anything specific to the implementation. – nEx.Software Oct 10 '12 at 16:05
  • If my answer was helpful, it'd be awesome if you'd indicate so via an upvote and/or marking it as correct. – nEx.Software Oct 10 '12 at 23:36