Android使用SVG小结
SVG的全称是Scalable Vector Graphics,叫可缩放矢量图形。它和位图(Bitmap)相对,SVG不会像位图一样因为缩放而让图片质量下降。它的优点在于节约空间,使用方便。
android也在5.0中新增了对使用svg矢量图支持,现在网上也有大把关于svg的文章但是使用时还是有遇到了许多坑,所以在这里我就总结了下我在使用svg过程中遇到的各种坑,希望对大家有所帮助。
VectorDrawable
要想在Android使用svg,首先要介绍的肯定是VectorDrawable,VectorDrawable是Android 5.0系统中引入了 VectorDrawable 来支持矢量图(SVG),同时还引入了 AnimatedVectorDrawable 来支持矢量图动画。
官方文档:
VectorDrawable,AnimatedVectorDrawable
VectorDrawable转换Bitmap
Bitmap bitmap;
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP){
Drawable vectorDrawable = context.getDrawable(R.id.img_vector);
bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(), vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
}else {
Drawable drawable = AppCompatDrawableManager.get().getDrawable(context, R.id.img_vector);
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
5.0以上版本使用
Android studio在2.0的版本中可以直接使用svg
新建一个SVGDemo项目,
新建Vector Asset文件
File -> New -> Vector Asset
导入SVG
可以选择Google提供的Material Icon进行导入也可以选择Local File选择本地svg文件进行导入,一般选择后者。对文件命名后点击Next ->Finish在drawable目录下就添加了一个.xml的文件
点击可以进行预览,一看是不是很像selector、animation-list只是标签为vector标签
width、height为对应的宽高,可以进行设置,viewportWidth、viewportHeight这个我也不太了解大概就是视图的宽高吧,好像作用不大,没有设置过。path为html中的一个标签用来定义路径,我们只关心path标签中的android:fillColor=”#FBDC00”的属性,用来改变颜色。
使用SVG
在layout新建一个activity_svg.xml文件
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_china"/>
就是这样简单,预览效果图:
也可以当背景使用
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_china">
</LinearLayout>
5.0以下版本使用SVG
上面说了在Android5.0以上使用svg图片是没有任何问题,但是怎么兼容5.0以下的机型,很多github第三方开源库可以解决,其实google已经给出了解决方案,我们主要了解原生的解决办法。
添加兼容性支持
首先,你需要在项目的build.gradle脚本中,增加对Vector兼容性的支持,代码如下所示:
android {
defaultConfig {
vectorDrawables.useSupportLibrary = true
}
}
在defaultConfig中添加了
vectorDrawables.useSupportLibrary = true
当然还需要添加appcompat的支持
compile 'com.android.support:appcompat-v7:23.4.0'
在ImageView中使用
1、我们要引用support:appcompat-v7中的view首先我们需要在XML布局文件头部添加:
xmlns:app="http://schemas.android.com/apk/res-auto"
2、 用V7下的AppCompatImageView代替ImageView
3、将android:src属性,换成app:srcCompat即可
代码如下 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_china"/>
</LinearLayout>
效果图
这里AppCompatImageView就不做过多介绍
官方文档;
在background使用
根据上文是不是能推测出有app:backgroundCompat属性呢,然而并不如我们所愿,没有这样的属性。所以我们只能用
android:background这个属性了,先不管这么多了直接4.4的机器上运行试试,果然崩了,在这里说明下在普通控件上使用Vector,就必须依附于StateListDrawable,InsetDrawable,LayerDrawable,LevelListDrawable,RotateDrawable我们选择selector代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_china"/>
</selector>
这样是不是完了呢?在运行试试还是崩了,这里又是一个坑…..
还需要在activity中添加如下代码:
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
完美。
iconfont的使用
上面介绍了把svg图片导入到项目中,但是一个个的svg是不是很麻烦,而且drawable会有大量的文件,阿里妈妈就提供了iconfont,
Iconfont-国内功能很强大且图标内容很丰富的矢量图标库,提供矢量图标下载、在线存储、格式转换等功能。
iconfont的简单使用
iconfont在Android中的使用官网已经做了非常详细介绍http://www.iconfont.cn/help/detail?helptype=code使用起来也很简单,我总结了几步:
- 首先在我的项目中新建一个自己的项目;
- 从iconfont平台选择要使用到的图标或者本地导入svg图片到项目中;
- 下载代码,把iconfont.svg和iconfont.ttf文件导入到项目中的assets文件夹中;
- 用TextView代替ImagerView,找到图标相对应的 HTML 实体字符码给textView设置;
- textview设置大小跟颜色,图标的大小颜色也会改变(这里大小最好用dp为单位,这样不会随着手机字体大小的改变而改变图标的大小);
- 为Textview设置指定的ttf文字;
<TextView
android:id="@+id/text_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/red"
android:textSize="50dp"/>
//为TextView设置指定ttf文字
Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");
TextView textview = (TextView)findViewById(R.id.text_icon);
textview.setTypeface(iconfont);
运行效果图
iconfont的封装
每次都给TextView设置指定文字是不是也很繁琐,而且一直不断的在读取iconfont.ttf文字,也很浪费内存,我们完全可以把这个抽离出来,说干就干。
首先我们要读取到的是assets目录下的iconfont.ttf文件;这里我把它放到自定义的Application中,这样就只要读取一次,代码如下:
public class MyApplication extends Application {
public static Typeface iconfont;
...
public static Typeface getIconfont(Context context) {
if (iconfont != null) {
return iconfont;
} else {
iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf");
}
return iconfont;
}
这里就实例化了iconfont。然后给每TextView设置Typeface,这肯定不是我们想要的,所以我们自定义一个TextView然后初始化时setTypeface就可以了代码如下:
public class TextViewIcon extends AppCompatTextView {
public TextViewIcon(Context context) {
super(context);
init(context);
}
public TextViewIcon(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TextViewIcon(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context) {
setTypeface(VcApplication.getIconfont(context));
}
}
就下了就可以直接在layout文件中使用了
<com.example.svgdemo.TextViewIcon
android:id="@+id/text_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="#ff0000"
android:textSize="50dp"/>
运行效果跟上图一样。
iconfont动态设置
动态设置通俗的说就是在代码里动态的调整图标的大小颜色或改变图片,iconfont改变大小颜色这很简单直接调用TextView的setTextSize和setTextColor就可以了,动态设置图片是不是setText呢?
textview.setText("");
运行发现并不如我们所愿这里涉及到unicode 字符的问题,
把代码稍改一下
textview.settext("\ue643");// "&#x" 替换成 "\u",用 unicode 字符来表示
这样问题就解决了
总结
通过这篇文章,我们基本就能掌握SVG在Android中的使用了,并且了解了阿里的iconfont的使用以及封装,其实SVG在Android中的应用还有很多列如文中提到的AnimatedVectorDrawable矢量图动画等,还有我把遇到的问题也贴出来希望大家来和我交流。怎么在Android中使用iconfont彩色图片,以及iconfont在除TextView其他控件的使用。谢谢!!!
Android使用SVG小结的更多相关文章
- Android 使用 SVG 矢量图
android svg矢量图 可能包含的操作有: SVG图还包括改变颜色,透明度,大小,矩阵操作(平移.旋转.缩放),selector, (图标,背景,按钮),动画,等 setTint(int Col ...
- Android Camera 使用小结。两种方法:一是调用系统camera app,二是自己写camera程序。
源文链接:http://www.cnblogs.com/franksunny/archive/2011/11/17/2252926.html Android Camera 使用小结 Android手机 ...
- Android Camera 使用小结
Android手机关于Camera的使用,一是拍照,二是摄像,由于Android提供了强大的组件功能,为此对于在Android手机系统上进行Camera的开发,我们可以使用两类方法:一是借助Inten ...
- Android使用SVG矢量创建很酷的动态效率!
尊重原创,欢迎转载.转载请注明: FROM GA_studio http://blog.csdn.net/tianjian4592 一个真正酷炫的动效往往让人虎躯一震,话不多说.咱们先瞅瞅效果: ...
- Android使用SVG矢量动画(二)
上篇我们学习了怎么显示SVG矢量图像,当然还有一个更强大的功能,就是让SVG图像动起来,先上一张效果图吧: 要实现上述动画效果,就得用AnimatedVectorDrawable这个类了,它就是负责V ...
- Android:Sqlitedatabase学习小结
今天刚刚学习完Sqlite数据库的基础知识,随即把学到的东西记录下来,以便随后查阅,以下是自己对Sqlite数据库的小结:1.Sqlite简介 Sqlite是一款轻型的数据库,它包含在一个 ...
- android: Android 权限管理小结
一. 概述 感谢郭神,自从Android6.0发布以来,在权限上做出了很大的变动,不再是之前的只要在manifest设置就可以任意获取权限,而是更加的注重用户的隐私和体验,不会再强迫用户因拒绝不该拥有 ...
- android 使用svg 和 webp
1.参考 https://chris.banes.dev/2016/02/25/appcompat-vector/#enabling-the-flag 2.使用svg 2.1 在中打开svg选项 an ...
- Android事件分发小结
******** ******** 第一部分: 介绍说明 ******** ******** 个人感觉在做交互的时候, 对于Android的按键分发的理解还是比较重要的. 这些内容在 ...
随机推荐
- Beanutils-No origin bean specified问题分析
copyProperties 时候系统中报错了,呵呵,源码已经说明了一切,就不BB了
- JavaWeb 之监听器
1. JavaWeb 监听器概述 在 JavaWeb 被监听的事件源为: ServletContext, HttpSession, ServletRequest, 即三大域对象. 监听域对象" ...
- 斯坦福大学Andrew Ng - 机器学习笔记(3) -- 神经网络模型
大概用了一个月,Andrew Ng老师的机器学习视频断断续续看完了,以下是个人学习笔记,入门级别,权当总结.笔记难免有遗漏和误解,欢迎讨论. 鸣谢:中国海洋大学黄海广博士提供课程视频和个人笔记,在此深 ...
- SQLServer: 用 ApexSQLLog 恢复 SQL Server 数据
https://blog.csdn.net/yenange/article/details/50512312
- 使用Kotlin开发Android应用(II):创建新工程
在基本了解什么是Kotlin以及Kotlin可以做什么之后,接下来就到了配置Android Studio并使用Kotlin开发Android apps的时候了.首次配置Android Studio需要 ...
- gearman background后台job状态获取
GearmanClient background job有一个方法叫: public array GearmanClient::jobStatus ( string $job_handle ) Get ...
- python如何实现多线程
一个线程就是一个轻量级进程,多线程能让我们一次执行多个线程. python是多线程语言,其内置有多线程工具包 python中GIL(全局解释器锁)确保一次执行单个线程.一个线程保存GIL并在将其传递给 ...
- PAT 天梯赛 L1-039. 古风排版 【字符串处理】
题目链接 https://www.patest.cn/contests/gplt/L1-039 思路 先根据 len 和 n 判断 有几个 列和几行,然后 从最右边 到 最左边 从上到下 将字符串 录 ...
- Raspberry Pi开发之旅-实现云平台监控
一.基本设置 1 sudo raspi-config 移动到第五项“Enable Camera”,回车进入,按tab键切换到“Enable”回车确认.回到主菜单,tab键切换到“Finish”回车确认 ...
- HISAT2的运用
功能: 用于有参考基因组存在的比对工具(适用于whole-genome, transcriptome, and exome sequencing data) 用法: hisat2-build [opt ...