AnDROID VECTOR资源怎么使用
时间: 2024-02-06 15:04:32 浏览: 40
Android Vector 资源是一种矢量图形格式,可以实现无限缩放而不失真,而且占用的空间比位图小得多。在 Android 开发中,可以通过两种方式使用 Vector 资源:
1. 在布局文件中使用:在布局文件中定义一个 ImageView 控件,并通过 app:srcCompat 属性来引用 Vector 资源,例如:
```xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_vector" />
```
2. 在代码中使用:在代码中通过 VectorDrawableCompat 类来加载 Vector 资源,并设置到 ImageView 控件的 src 属性中,例如:
```java
ImageView imageView = findViewById(R.id.image_view);
Drawable drawable = VectorDrawableCompat.create(getResources(), R.drawable.ic_vector, null);
imageView.setImageDrawable(drawable);
```
需要注意的是,使用 Vector 资源需要在 app/build.gradle 文件中添加以下代码,以支持 API 级别 21 及以下的设备:
```groovy
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
}
```
其中,vectorDrawables.useSupportLibrary = true 表示使用支持库中的 VectorDrawableCompat 类来加载 Vector 资源。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)