矢量圖資源
查找資源圖標 下載 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了