您的位置:首页 > 移动开发 > Android开发

Android 使用SVG矢量图

2017-03-05 16:55 453 查看

下载矢量图

矢量图,是什么,就不用讲了,不清楚,自行百度哈。

阿里矢量图库 http://www.iconfont.cn/,这里有各式各样的矢量图,基本能满足我们日常用图需求。

首先,下载*.svg格式的矢量图



Android studio 使用矢量图

项目中,在要放置矢量图的目录右击->new->Vector Asset,如下图



开始转换



最后,生成ic_menu_collection.xml矢量图xml文件

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="1024.0"
android:viewportWidth="1024.0">
<path
android:fillColor="#FF000000"
android:pathData="M895.8,517.7c88.3,-91.9 88.2,-240.9 0,-332.8 -45.1,-47 -104.5,-69.9 -163.6,-68.9 -82.7,1.5 -219,115.4 -219,115.4s-140.2,-115.6 -224.9,-115.4c-57.7,0.1 -115.4,23.1 -159.4,68.9 -88.2,91.9 -88.3,240.9 0,332.8l383.5,399.3L895.8,517.7z" />
</vector>


使用

和图片一样使用即可

代码动态改变SVG顔色

package com.xuewei.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.widget.ImageView;

import com.xuewei.XWApplication;

/**
*
* Created by Administrator on 2017/3/11.
*/

public class SVGUtils {

public static SVGUtils svgUtils;
private Context mContext;
private SVGUtils(){
mContext = XWApplication.getInstance();
}
public static SVGUtils getInstance(){
if(svgUtils==null){
svgUtils = new SVGUtils();
}
return svgUtils;
}

/**
* 改变SVG图片着色
* @param imageView
* @param iconResId svg资源id
* @param color 期望的着色
*/
public void changeSVGColor(ImageView imageView,int iconResId,int color){
Drawable drawable = ContextCompat.getDrawable(mContext, iconResId);
imageView.setImageDrawable(drawable);
Drawable.ConstantState state = drawable.getConstantState();
Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
DrawableCompat.setTint(drawable1, ContextCompat.getColor(mContext, color));
imageView.setImageDrawable(drawable1);
}
}


调用

if(groupXueWei.getCollection()){
SVGUtils.getInstance().changeSVGColor(holder.collectionIcon,R.drawable.ic_collection,R.color.collection);
}else{
SVGUtils.getInstance().changeSVGColor(holder.collectionIcon,R.drawable.ic_collection,R.color.unCollection);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息