Seraphim's
Seraphim's

Reputation: 12768

set a custom button selector bitmap item from SVG

I have a customization for a standard Button:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true">
   <layer-list>
     <item android:drawable="@drawable/standard_button_normal_background_layer1"/>
     <item android:drawable="@drawable/standard_button_normal_background_layer0"/>
     <item><bitmap android:gravity="center" android:src="@drawable/the_bitmap" /></item>
   </layer-list>
 </item>

...

</selector>

I need to change the bitmap with the one I load from an SVG file. I use

http://code.google.com/p/svg-android/

that can produce a PictureDrawable:

SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.icon);
PictureDrawable pd = svg.createPictureDrawable();
imageView.setImageDrawable(pd);

How can I access the specific <bitmap> item, and how can I set the bitmap? Any solution?

EDIT:

I want to know if I can programmatically add to the Button customization a picture on the bottom of the <layer-list> items. Picture come from svg, so it's not on the compiled resource.

The result I need is a button with some custom graphic (defined by <layer-list>) and a icon centered inside it coming from svg.

Upvotes: 1

Views: 2779

Answers (1)

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

If you are asking how to write an XML file that references the Picture that you loaded from an SVG, that's sort of a chicken and an egg. When your app is deployed, all the stuff in res/ is read-only. So by dynamically loading a bitmap from a set of drawing instructions you can simply store the outputed bitmap.

imageView.onDraw(mBitmapCanvas);

That's all you can do. You could create a raster as a prebuild step but you will lose the size benefits of the vector being bundled with your app.

If you are asking how to edit the layout-list, try this:

layer-list.xml

<layer-list>
     <item android:drawable="@drawable/standard_button_normal_background_layer1"/>
     <item android:drawable="@drawable/standard_button_normal_background_layer0"/>
     <!-- Remove bitmap -->
</layer-list>

Then when you want to use this drawable:

LayerDrawable drawable = (LayerDrawable)getResources().getDrawable(R.drawable.layout_list);
ArrayList<Drawable> layers = new ArrayList<Drawable>();
for(int i=0; i < drawable.getNumberOfLayers(); i++) {
   layers.add(drawable.getDrawable(i);
}

layers.add(new BitmapDrawable(mBitmap));
drawable = new LayerDrawable(layers.toArray(new Drawable[layers.size()]);

Upvotes: 1

Related Questions