Встроенные сложные XML-ресурсы
Оптимизируйте свои подборки
Сохраняйте и классифицируйте контент в соответствии со своими настройками.
Некоторые типы ресурсов представляют собой композицию нескольких сложных ресурсов, представленных файлами XML. Одним из примеров является анимированный векторный объект рисования, который представляет собой ресурс рисования, инкапсулирующий векторный объект рисования и анимацию. Для этого необходимо использовать как минимум три XML-файла, как показано в следующих примерах.
-
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" />
Если векторный рисунок и анимация повторно используются в другом месте, это лучший способ реализовать анимированный векторный рисунок. Но если эти файлы используются только для рисования анимированного вектора, то существует более компактный способ их реализации.
Используя встроенный формат ресурсов AAPT, вы можете определить все три ресурса в одном XML-файле, как показано в следующем примере. Для рисования анимированного вектора поместите файл в 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>
Тег XML <aapt:attr >
сообщает AAPT рассматривать дочерний элемент тега как ресурс и извлекать его в собственный файл ресурсов. Значение имени атрибута указывает, где использовать встроенный ресурс в родительском теге.
AAPT генерирует файлы ресурсов и имена для всех встроенных ресурсов. Приложения, созданные с использованием этого встроенного формата, совместимы со всеми версиями Android.
Контент и образцы кода на этой странице предоставлены по лицензиям. Java и OpenJDK – это зарегистрированные товарные знаки корпорации Oracle и ее аффилированных лиц.
Последнее обновление: 2025-07-29 UTC.
[[["Прост для понимания","easyToUnderstand","thumb-up"],["Помог мне решить мою проблему","solvedMyProblem","thumb-up"],["Другое","otherUp","thumb-up"]],[["Отсутствует нужная мне информация","missingTheInformationINeed","thumb-down"],["Слишком сложен/слишком много шагов","tooComplicatedTooManySteps","thumb-down"],["Устарел","outOfDate","thumb-down"],["Проблема с переводом текста","translationIssue","thumb-down"],["Проблемы образцов/кода","samplesCodeIssue","thumb-down"],["Другое","otherDown","thumb-down"]],["Последнее обновление: 2025-07-29 UTC."],[],[],null,["# Inline complex XML resources\n\nCertain resource types are a composition of multiple complex resources represented by XML files.\nOne example is an animated vector drawable, which is a drawable resource encapsulating a vector\ndrawable and an animation. This requires the use of at least three XML files, as shown in the\nfollowing\nexamples.\n\n`res/drawable/avd.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003canimated-vector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:drawable=\"@drawable/vectordrawable\" \u003e\n \u003ctarget\n android:name=\"rotationGroup\"\n android:animation=\"@anim/rotation\" /\u003e\n \u003c/animated-vector\u003e\n ```\n\n`res/drawable/vectordrawable.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cvector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:height=\"64dp\"\n android:width=\"64dp\"\n android:viewportHeight=\"600\"\n android:viewportWidth=\"600\" \u003e\n \u003cgroup\n android:name=\"rotationGroup\"\n android:pivotX=\"300.0\"\n android:pivotY=\"300.0\"\n android:rotation=\"45.0\" \u003e\n \u003cpath\n android:fillColor=\"#000000\"\n android:pathData=\"M300,70 l 0,-70 70,70 0,0 -70,70z\" /\u003e\n \u003c/group\u003e\n \u003c/vector\u003e\n ```\n\n`res/anim/rotation.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003cobjectAnimator xmlns:android=\"http://schemas.android.com/apk/android\"\n android:duration=\"6000\"\n android:propertyName=\"rotation\"\n android:valueFrom=\"0\"\n android:valueTo=\"360\" /\u003e\n ```\n\n\nIf the vector drawable and animations are re-used elsewhere, this is the best way to implement an\nanimated vector drawable. But if these files are only used for this animated vector drawable,\nthen there is a more compact way to implement them.\n\nUsing AAPT's inline resource format, you can define all three resources in the same XML file, as\nshown in the following example.\nFor an animated vector drawable, put the file under `res/drawable/`.\n\n`res/drawable/avd.xml`\n:\n\n ```xml\n \u003c?xml version=\"1.0\" encoding=\"utf-8\"?\u003e\n \u003canimated-vector xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:aapt=\"http://schemas.android.com/aapt\" \u003e\n\n \u003caapt:attr name=\"android:drawable\" \u003e\n \u003cvector\n android:height=\"64dp\"\n android:width=\"64dp\"\n android:viewportHeight=\"600\"\n android:viewportWidth=\"600\" \u003e\n \u003cgroup\n android:name=\"rotationGroup\"\n android:pivotX=\"300.0\"\n android:pivotY=\"300.0\"\n android:rotation=\"45.0\" \u003e\n \u003cpath\n android:fillColor=\"#000000\"\n android:pathData=\"M300,70 l 0,-70 70,70 0,0 -70,70z\" /\u003e\n \u003c/group\u003e\n \u003c/vector\u003e\n \u003c/aapt:attr\u003e\n\n \u003ctarget android:name=\"rotationGroup\"\u003e\n \u003caapt:attr name=\"android:animation\" \u003e\n \u003cobjectAnimator\n android:duration=\"6000\"\n android:propertyName=\"rotation\"\n android:valueFrom=\"0\"\n android:valueTo=\"360\" /\u003e\n \u003c/aapt:attr\u003e\n \u003c/target\u003e\n \u003c/animated-vector\u003e\n ```\n\nThe XML tag `\u003caapt:attr \u003e` tells AAPT to treat the tag's child as a\nresource and extract it into its own resource file. The value in the attribute name specifies where\nto use the inline resource within the parent tag.\n\nAAPT generates resource files and names for all the inline resources.\nApplications built using this inline format are compatible with all versions of Android."]]