矢量图资源
查找资源图标 下载 svg
在AS中打开
点击Local File(SVG PSD)
找到存放路径 并命名 Next
在drawable
目录生成文件,如下图
在appcompat 23.2.0开始,提供了vectorDrawable
VectorDrawableCompat,AnimatedVectorDrawableCompat
两种支持库一个用于兼容矢量图,但是这个支持库要使用的话,还得在app的gradle里面加个这样的配置:
//在gradle2.0及以上:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}}
//在gradle 1.5以前
android {
defaultConfig {
// Stops the Gradle plugin’s automatic rasterization of vectors
generatedDensities = []
}
// Flag to tell aapt to keep the attribute ids around
aaptOptions {
additionalParameters "--no-version-vectors"
}
}
使用app:srcCompat="@drawable/icon_shopping"
以上为网站使用 .,自己使用的时候直接使用的android:src=""
也是可以的
用在TextView
Button
的使用,只能如下使用
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/icon_shopping"/>
</selector>
并且在布局中(Activity Fragment)添加
static {
AppCompatDelegate.setCompatVectorFromSourcesEnabled(true);
}
**
Vector 使用
**
矢量图库资源
在网站下载批量图库资源,
解压,将一下文件copy到项目assets
下(其他是web端使用)
创建类
public class IconText extends android.support.v7.widget.AppCompatTextView {
private static Typeface typeface = null;
public IconText(Context context) {
this(context, null);
}
public IconText(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public IconText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initFont(attrs);
}
private void initFont(AttributeSet attrs) {
TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.IconText);
String path = array.getString(R.styleable.IconText_assetsPath);
if (path == null || path.isEmpty()) {
throw new RuntimeException("图片ttf路径错误");
}
if (typeface == null) {
typeface = Typeface.createFromAsset(getContext().getAssets(), path);
}
setTypeface(typeface);
}
}
text
设置图标 textSize
设置图标大小textColor
设置图标颜色
<android.biginner.com.refretest.IconText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text=""
android:textColor="#f00"
android:textSize="100dp"
app:assetsPath="iconfont.ttf" />
其中android:text=""
中的
是从
一定要copy 全,注意最后的 ;
在应用中app:assetsPath="iconfont.ttf"
直接写活了,不过基本不需要, 只要在项目之前把该用的图标全部都放到里边就ok了