• Android: 使用AAPT将多个具有关联性的xml资源全部配置在一个xml文件中


    Inline Complex XML Resources

    Certain resource types are a composition of multiple complex resources represented by XML files. One example is an animated vector drawable, which is a drawable resource encapsulating a vector drawable and an animation. This requires the use of 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 a lot of files here just to make a single animated vector drawable! If the vector drawable and animations are re-used elsewhere, this is the best way to implement an animated vector drawable. If they’re only ever used for this animated vector drawable, then 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’re making an animated vector drawable, 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 the tag’s child shall be treated as a resource and extracted into its own resource file. The value in the attribute name specifies where to use the inline resource within the parent tag.

    AAPT will generate resource files and names for all of the inline resources. Applications built using this inline format are compatible with all versions of Android.

  • 相关阅读:
    BZOJ 2594: [Wc2006]水管局长数据加强版
    BZOJ 2049: [Sdoi2008]Cave 洞穴勘测
    html5 canvas ( 贝塞尔曲线, 一片星空加绿地 ) quadraticCurveTo, bezierCurveTo
    c#.net 接收 base64 格式的数据并解析为图片
    html5 canvas ( 绘制一轮弯月, 星空中的弯月 )
    html5 canvas ( 圆和切点曲线的绘制 ) arc, arcTo
    html5 canvas ( 图片填充样式 ) fillStyle, createPattern
    html5 canvas ( 径向渐变, 升级版的星空 ) fillStyle, createRadialGradient
    html5 canvas ( 线性渐变, 升级版的星空 ) fillStyle, createLinearGradient, addColorStop
    html5 canvas ( 图形变换矩阵 ) transform, setTransform
  • 原文地址:https://www.cnblogs.com/emmet7life/p/7048655.html
Copyright © 2020-2023  润新知