App Resource -- inline complex XML resource

Keywords: Android xml encoding

Some resource types are combinations of multiple complex resources represented by XML files. An example is animation vector drawable, which is a paintable resource, encapsulating vector drawable and animation. This requires at least three XML files.

res/drawable/avd.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/vectordrawable" >
    <target
        android:name="rotationGroup"
        android:animation="@anim/rotation" />
</animated-vector>

res/drawable/vectordrawable.xml

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="64dp"
    android:width="64dp"
    android:viewportHeight="600"
    android:viewportWidth="600" >

   <group
        android:name="rotationGroup"
        android:pivotX="300.0"
        android:pivotY="300.0"
        android:rotation="45.0" >
        <path
            android:fillColor="#000000"
            android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />

   </group>
</vector>

res/anim/rotation.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/android"
    android:duration="6000"
    android:propertyName="rotation"
    android:valueFrom="0"

   android:valueTo="360" />

There are many files here just to make an animation vector that can be drawn! If the vector drawable and animation are used repeatedly in other places, this is the best way to realize the vector drawable of animation. If they are only available for this animation vector, there is a more compact way to implement them.

Using AAPT's inline resource format, you can define all three resources in the same XML file. Since we are making an animation vector that can be drawn, we put the file under res/drawable /.

res/drawable/avd.xml

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt" >

    <aapt:attr name="android:drawable" >
        <vector
            android:height="64dp"
            android:width="64dp"
            android:viewportHeight="600"
            android:viewportWidth="600" >

           <group
                android:name="rotationGroup"
                android:pivotX="300.0"
                android:pivotY="300.0"
                android:rotation="45.0" >
                <path
                    android:fillColor="#000000"
                    android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />

           </group>
        </vector>
    </aapt:attr>

    <target android:name="rotationGroup">
        <aapt:attr name="android:animation" >
            <objectAnimator
                android:duration="6000"
                android:propertyName="rotation"
                android:valueFrom="0"

               android:valueTo="360" />
        </aapt:attr>
    </target>
</animated-vector>


The XML tag < AAPT: attr > tells AAPT that children of the tag should be treated as resources and extracted into its own resource file. The value in the property name specifies where the inline resource is used in the parent tag.

AAPT generates resource files and names for all inline resources. Applications built in this inline format are compatible with all Android versions.

Posted by tegwin15 on Wed, 01 Jan 2020 02:41:48 -0800