Home | History | Annotate | Download | only in resources
      1 page.title=Inline Complex XML Resources
      2 parent.title=Application Resources
      3 parent.link=index.html
      4 @jd:body
      5 
      6 <p>Certain resource types are a composition of multiple complex resources represented by XML files.
      7 One example is an animated vector drawable, which is a drawable resource encapsulating a vector
      8 drawable 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 &lt;?xml version="1.0" encoding="utf-8"?&gt;
     16 &lt;animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
     17     android:drawable="@drawable/vectordrawable" &gt;
     18     &lt;target
     19         android:name="rotationGroup"
     20         android:animation="@anim/rotation" /&gt;
     21 &lt;/animated-vector&gt;
     22 </pre>
     23 </dd>
     24 
     25 <dt><code>res/drawable/vectordrawable.xml</code></dt>
     26 <dd>
     27 <pre class="stx">
     28 &lt;?xml version="1.0" encoding="utf-8"?&gt;
     29 &lt;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" &gt;
     34  &lt;group
     35         android:name="rotationGroup"
     36         android:pivotX="300.0"
     37         android:pivotY="300.0"
     38         android:rotation="45.0" &gt;
     39  &lt;path
     40             android:fillColor="#000000"
     41             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /&gt;
     42  &lt;/group&gt;
     43 &lt;/vector&gt;
     44 </pre>
     45 </dd>
     46 
     47 <dt><code>res/anim/rotation.xml</code></dt>
     48 <dd>
     49 <pre class="stx">
     50 &lt;?xml version="1.0" encoding="utf-8"?&gt;
     51 &lt;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" /&gt;
     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!
     62 If the vector drawable and animations are re-used elsewhere, this is the best way to implement an
     63 animated vector drawable. If theyre only ever used for this animated vector drawable, then there is
     64 a more compact way to implement them.</p>
     65 
     66 <p>Using AAPTs inline resource format, you can define all three resources in the same XML file.
     67 Since were 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 &lt;?xml version="1.0" encoding="utf-8"?&gt;
     75 &lt;animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
     76     <strong>xmlns:aapt="http://schemas.android.com/aapt"</strong> &gt;
     77 
     78     <strong>&lt;aapt:attr name="android:drawable" &gt;</strong>
     79         &lt;vector
     80             android:height="64dp"
     81             android:width="64dp"
     82             android:viewportHeight="600"
     83             android:viewportWidth="600" &gt;
     84          &lt;group
     85                 android:name="rotationGroup"
     86                 android:pivotX="300.0"
     87                 android:pivotY="300.0"
     88                 android:rotation="45.0" &gt;
     89          &lt;path
     90                     android:fillColor="#000000"
     91                     android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /&gt;
     92          &lt;/group&gt;
     93         &lt;/vector&gt;
     94     <strong>&lt;aapt:attr /&gt;</strong>
     95 
     96     &lt;target android:name="rotationGroup"&gt;
     97  <strong>&lt;aapt:attr name="android:animation" &gt;</strong>
     98             &lt;objectAnimator
     99                 android:duration="6000"
    100                 android:propertyName="rotation"
    101                 android:valueFrom="0"
    102              android:valueTo="360" /&gt;
    103         <strong>&lt;aapt:attr&gt;</strong>
    104     &lt;/target&gt;
    105 &lt;/animated-vector&gt;
    106 </pre>
    107 </dd>
    108 
    109 </dl>
    110 
    111 <p>The XML tag <code>&lt;aapt:attr &gt;</code> tells AAPT that the tags child shall be treated as a
    112 resource and extracted into its own resource file. The value in the attribute name specifies where
    113 to 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.
    116 Applications built using this inline format are compatible with all versions of Android.</p>
    117 
    118