1page.title=Inline Complex XML Resources 2parent.title=Application Resources 3parent.link=index.html 4@jd:body 5 6<p>Certain resource types are a composition of multiple complex resources represented by XML files. 7One example is an animated vector drawable, which is a drawable resource encapsulating a vector 8drawable and an animation. This requires the use of at least three XML files.</p> 9 10<dl> 11 12<dt><code>res/drawable/avd.xml</code></dt> 13<dd> 14<pre class="stx"> 15<?xml version="1.0" encoding="utf-8"?> 16<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 17 android:drawable="@drawable/vectordrawable" > 18 <target 19 android:name="rotationGroup" 20 android:animation="@anim/rotation" /> 21</animated-vector> 22</pre> 23</dd> 24 25<dt><code>res/drawable/vectordrawable.xml</code></dt> 26<dd> 27<pre class="stx"> 28<?xml version="1.0" encoding="utf-8"?> 29<vector xmlns:android="http://schemas.android.com/apk/res/android" 30 android:height="64dp" 31 android:width="64dp" 32 android:viewportHeight="600" 33 android:viewportWidth="600" > 34 <group 35 android:name="rotationGroup" 36 android:pivotX="300.0" 37 android:pivotY="300.0" 38 android:rotation="45.0" > 39 <path 40 android:fillColor="#000000" 41 android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> 42 </group> 43</vector> 44</pre> 45</dd> 46 47<dt><code>res/anim/rotation.xml</code></dt> 48<dd> 49<pre class="stx"> 50<?xml version="1.0" encoding="utf-8"?> 51<objectAnimator xmlns:android="http://schemas.android.com/apk/android" 52 android:duration="6000" 53 android:propertyName="rotation" 54 android:valueFrom="0" 55 android:valueTo="360" /> 56</pre> 57</dd> 58 59</dl> 60 61<p>There are a lot of files here just to make a single animated vector drawable! 62If the vector drawable and animations are re-used elsewhere, this is the best way to implement an 63animated vector drawable. If they’re only ever used for this animated vector drawable, then there is 64a more compact way to implement them.</p> 65 66<p>Using AAPT’s inline resource format, you can define all three resources in the same XML file. 67Since we’re making an animated vector drawable, we put the file under <code>res/drawable/</code>.</p> 68 69<dl> 70 71<dt><code>res/drawable/avd.xml</code></dt> 72<dd> 73<pre class="stx"> 74<?xml version="1.0" encoding="utf-8"?> 75<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 76 <strong>xmlns:aapt="http://schemas.android.com/aapt"</strong> > 77 78 <strong><aapt:attr name="android:drawable" ></strong> 79 <vector 80 android:height="64dp" 81 android:width="64dp" 82 android:viewportHeight="600" 83 android:viewportWidth="600" > 84 <group 85 android:name="rotationGroup" 86 android:pivotX="300.0" 87 android:pivotY="300.0" 88 android:rotation="45.0" > 89 <path 90 android:fillColor="#000000" 91 android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> 92 </group> 93 </vector> 94 <strong><aapt:attr /></strong> 95 96 <target android:name="rotationGroup"> 97 <strong><aapt:attr name="android:animation" ></strong> 98 <objectAnimator 99 android:duration="6000" 100 android:propertyName="rotation" 101 android:valueFrom="0" 102 android:valueTo="360" /> 103 <strong><aapt:attr></strong> 104 </target> 105</animated-vector> 106</pre> 107</dd> 108 109</dl> 110 111<p>The XML tag <code><aapt:attr ></code> tells AAPT that the tag’s child shall be treated as a 112resource and extracted into its own resource file. The value in the attribute name specifies where 113to use the inline resource within the parent tag.</p> 114 115<p>AAPT will generate resource files and names for all of the inline resources. 116Applications built using this inline format are compatible with all versions of Android.</p> 117 118