1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.compose.animation.graphics.res
18
19 import android.content.res.Resources
20 import android.util.Xml
21 import androidx.annotation.DrawableRes
22 import androidx.compose.animation.graphics.vector.AnimatedImageVector
23 import androidx.compose.animation.graphics.vector.compat.parseAnimatedImageVector
24 import androidx.compose.animation.graphics.vector.compat.seekToStartTag
25 import androidx.compose.runtime.Composable
26 import androidx.compose.runtime.remember
27 import androidx.compose.ui.platform.LocalContext
28 import androidx.compose.ui.platform.LocalResources
29 import org.xmlpull.v1.XmlPullParserException
30
31 /**
32 * Load an [AnimatedImageVector] from an Android resource id.
33 *
34 * @param id the resource identifier
35 * @return an animated vector drawable resource.
36 * @sample androidx.compose.animation.graphics.samples.AnimatedVectorSample
37 */
38 @Composable
animatedVectorResourcenull39 public fun AnimatedImageVector.Companion.animatedVectorResource(
40 @DrawableRes id: Int
41 ): AnimatedImageVector {
42 val context = LocalContext.current
43 val res = LocalResources.current
44 val theme = context.theme
45 return remember(id) { loadAnimatedVectorResource(theme, res, id) }
46 }
47
48 @Throws(XmlPullParserException::class)
loadAnimatedVectorResourcenull49 internal fun loadAnimatedVectorResource(
50 theme: Resources.Theme? = null,
51 res: Resources,
52 resId: Int
53 ): AnimatedImageVector {
54 val parser = res.getXml(resId).seekToStartTag()
55 val attrs = Xml.asAttributeSet(parser)
56 return parser.parseAnimatedImageVector(res, theme, attrs)
57 }
58