Android中使用兼容库创建Vector Drawable

随着Android应用程序兼容性支持库v23.2的新版本到来,它的一些特性也向后兼容并且增加了一些新功能。随着Android Lolipop(API 21)的发布,一个新的组件被包括进来,它被叫做矢量绘制对象。但是这个组件仅仅能在API21+上使用,现在的Androidy应用程序兼容性支持库v23.2矢量绘制对象的到来,可以为可绘制性提供强大的向后兼容性,可兼容API7+设备。

什么是 Vector Drawable ?

正如它的名字所暗示的那样,Vector Drawable基于矢量图像。相对于光栅图形,矢量图形是描述使用几何形状图形元素的一种方式,它类似于SVG文件。在Vector Drawable中它是利用XML文件被创建出来。在Vector Drawable添加到Android SDK之前,开发者对于不同的显示分辨率必须创建多个版本的图像,这需要花费时间创建额外的asstes,并且消耗更多的空间,以致于增大apk文件的大小。如今这里不需要为mdpi,hdpi,xhdpi等等设计不同大小的图像,利用Vector Drawable你只需要创建图像仅仅一次作为一个XML文件,你将能够对于所有显示分辨率和不同的设备进行缩放。

如何使用Vector Drawable ?

升级builde.gradle 文件

在你项目中能够使用Vector Drawable之前,你需要添加兼容行代码到你的build.gradle文件下Gradle depencies模块中(以下版本必须满足v23.2+)然后重新编译一下项目

1
2
3
4
dependencies {
...
compile 'com.android.support:appcompat-v7:23.2.1'
}

如果你是使用的Gradle plugin插件版本在2.0或者及其以上,请添加以下代码:

1
2
3
4
5
6
// Gradle Plugin 2.0+
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}

如果你是使用的Gradle plugin插件版本在1.5或者及其以下,请添加以下代码:

1
2
3
4
5
6
7
8
9
10
// Gradle Plugin 1.5
android {
defaultConfig {
generatedDensities = []
}

// This is handled for you by the 2.0+ Gradle Plugin
aaptOptions {
additionalParameters "--no-version-vectors"
}

我当前的Android Studio是 2.2 priview 6:

vector_gradle

创建Vector Drawable

创建Vector Assets目录

在你的Android Studio中选中drawable文件夹>右键点击>new>Vector Assets

create_vector_one

Vector Asset Studio Tool

现在Vector Asset Studio 工具窗口会出现

create_vector_two

创建Vector drawable xml

创建Vector drawable xml 文件有两种方式:默认的Material 图标、加载本地SVG文件

Material 图标

选择Material图标选项,然后点击icon,它将会出现一个选择icon的窗口,在这里你可以选择任意一个你期望的图标。我这里选择了一个定位的图标,点击OK

create_vector_xml_way1_select_icon

这个窗口显示了刚刚选择的图标进行预览,我默认设置它的size为24x24dp,现在点击下一步

create_vector_xml_way1_select_icon_ok

这个窗口运行你设置目标Module和Res目录(可以选择debug或者release其中一种),配置完成后点击Finish完成

create_vector_xml_way1_confirm_icon_path

以下就是上面过程的结果,将会在drawable文件夹中生成Vector drawable ic_location_on_black_24dp.xml

create_vector_xml_way1_finish

加载本地SVG文件

选择Local SVG File,然后点击下面的path,弹出一个新的窗口,选择本地svg 文件。

create_vector_xml_way2_select_icon

这个窗口显示了刚刚导入的SVG图标进行预览,我默认设置它的size为24x24dp,现在点击下一步

create_vector_xml_way2_select_icon_ok

这个窗口运行你设置目标Module和Res目录(可以选择debug或者release其中一种),配置完成后点击Finish完成

create_vector_xml_way2_confirm_icon_path

以下就是上面过程的结果,将会在drawable文件夹中生成Vector drawable ic_dollar.xml

create_vector_xml_way2_finish

修改strings.xml

添加以下string values到res => values => strings.xml

1
2
3
4
5
<resources>
<string name="material_icon">Material Icon</string>
<string name="svg_file_icon">Local SVG File Icon</string>
<string name="dynamic_icon">Vector Image Dynamic Icon</string>
</resources>

修改layout.xml

vector_drawable_layout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shoewann.myapplication.MainActivity">

<TextView
android:id="@+id/tv_material_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="@string/material_icon"
android:textColor="@android:color/black"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintRight_toRightOf="@+id/activity_main"
app:layout_constraintTop_toTopOf="@+id/activity_main"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<ImageView
android:id="@+id/ic_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:contentDescription=""
android:gravity="center_vertical"
app:layout_constraintLeft_toLeftOf="@+id/tv_material_icon"
app:layout_constraintRight_toRightOf="@+id/tv_material_icon"
app:layout_constraintTop_toBottomOf="@+id/tv_material_icon"
app:srcCompat="@drawable/ic_location_on_black_24dp"
tools:ignore="ContentDescription"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<TextView
android:id="@+id/tv_svg_file_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="@string/svg_file_icon"
android:textColor="@android:color/black"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintRight_toRightOf="@+id/activity_main"
app:layout_constraintTop_toBottomOf="@+id/ic_location"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<ImageView
android:id="@+id/ic_dollar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:contentDescription=""
android:gravity="center_vertical"
app:layout_constraintLeft_toLeftOf="@+id/ic_location"
app:layout_constraintRight_toRightOf="@+id/ic_location"
app:layout_constraintTop_toBottomOf="@+id/tv_svg_file_icon"
app:srcCompat="@drawable/ic_dollar"
tools:ignore="ContentDescription"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<TextView
android:id="@+id/tv_dynamic_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
android:text="@string/dynamic_icon"
android:textColor="@android:color/black"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
app:layout_constraintRight_toRightOf="@+id/activity_main"
app:layout_constraintTop_toBottomOf="@+id/ic_dollar"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<ImageView
android:id="@+id/dynamic_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:gravity="center_vertical"
app:layout_constraintLeft_toLeftOf="@+id/tv_dynamic_icon"
app:layout_constraintRight_toRightOf="@+id/tv_dynamic_icon"
app:layout_constraintTop_toBottomOf="@+id/tv_dynamic_icon"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
tools:ignore="ContentDescription" />
</android.support.constraint.ConstraintLayout>

修改 MainActivity.java

vector_drawable_src

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.example.shoewann.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

private android.widget.ImageView dynamicicon;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.dynamicicon = (ImageView) findViewById(R.id.dynamic_icon);
this.dynamicicon.setImageResource(R.drawable.ic_dollar);
}
}

运行效果

vector_drawable_run_result

-------------本文结束感谢您的阅读-------------
if (本文对您有用) { Pay (请随意¥打赏) } else { Commit feedback (底部评论区提交建议、反馈) } 感谢支持!
0%