Reputation: 3642
I have my custom Drawable
like this:
public class SeekBarBackgroundDrawable extends Drawable {
Paint mBasePaint = null;
public SeekBarBackgroundDrawable() {
super();
mBasePaint = new Paint();
mBasePaint.setAntiAlias(true);
mBasePaint.setStyle(Paint.Style.STROKE);
mBasePaint.setStrokeCap(Paint.Cap.ROUND);
mBasePaint.setStrokeWidth(10);
mBasePaint.setColor(0xFF00FF00);
}
@Override
public void draw(Canvas canvas) {
Rect r = getBounds();
canvas.drawLine(r.left, canvas.getHeight()/2,r.right,canvas.getHeight()/2, mBasePaint);
}
Now, this drawable is used in layer-list
with parameters color
and width
like here:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<cdev.mypreferences.SeekBarBackgroundDrawable
android:width="1dp" android:color="@color/bg_color">
</cdev.mypreferences.SeekBarBackgroundDrawable>
</item>
<item android:id="@android:id/progress">
<clip>
<shape android:shape="rectangle">
<corners android:radius="20dp"></corners>
<solid android:color="@color/seekbar_progress"></solid>
</shape>
</clip>
</item>
</layer-list>
How can I get parameters from this xml
into Drawable
class? I need to set mBasePaint
stroke width and color?
Upvotes: 0
Views: 827
Reputation: 62189
Declaring custom drawables in xml
is possible to do from API 24 onward, though I couldn't succeed to do that using the first approach mentioned in the docs.
Nevertheless, as the question relates to other aspect, I'll try to answer that part.
Adding this in your custom Drawable
class would return the values you are interested in:
private final int[] attrsArray = new int[] {
android.R.attr.width,
android.R.attr.color,
};
@Override public void inflate(@NonNull Resources r, @NonNull XmlPullParser parser,
@NonNull AttributeSet attrs) throws XmlPullParserException, IOException {
super.inflate(r, parser, attrs);
final TypedArray a = r.obtainAttributes(attrs, attrsArray);
float width = a.getDimensionPixelSize(0, 0);
@SuppressLint("ResourceType")
int color = a.getColor(1, 0);
a.recycle();
}
Upvotes: 1