Android---矢量图(Vector)使用方法
来源:互联网 发布:华藏净宗网络直播台 编辑:程序博客网 时间:2024/06/05 09:58
概述
完全取代简单的图标,在各种分辨率的屏幕上都不会失真.
使用步骤
1.先在gradle里配置参数
目的是让矢量图在5.0前的手机上也正常显示
apply plugin: 'com.android.library' android { ... ... defaultConfig { //配置这个参数 vectorDrawables.useSupportLibrary = true } ... ... }
2.drawable –> 右键 –> new –> vector assets里建好。
(0)首先给布局指定命名空间
xmlns:app="http://schemas.android.com/apk/res-auto"
(1)给控件指定矢量图,必须下面用的方式。
app:srcCompat="@drawable/ic_xxx"
例如:
<android.support.design.widget.FloatingActionButton android:id="@+id/fab_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/fab_margin" android:tint="@android:color/white" app:backgroundTint="@android:color/holo_orange_light" app:layout_anchor="@id/app_bar" app:layout_anchorGravity="bottom|end" app:srcCompat="@drawable/ic_add" /></android.support.design.widget.CoordinatorLayout>
3.为矢量图动态设置颜色
(1)静态
如果需要静态指定颜色,在使用的地方给tint属性赋值。
android:tint="@android:color/white"
(2)动态
在需要的地方调用该方法
/** * 给使用了矢量图的ImageView设置颜色 * * @param context 上下文 * @param vectorRid xml矢量图的id * @param colorRid 颜色的id * @param imageView 要设置的控件 */public static void setVectorColorImgv(Context context, int vectorRid, int colorRid, ImageView imageView) { VectorDrawableCompat vectorDrawableCompat = VectorDrawableCompat.create(context.getResources(), vectorRid, context.getTheme()); //以下三种方式选一种 //1.设置单一的颜色 vectorDrawableCompat.setTint(context.getResources().getColor(colorRid)); //2.设置状态性的,比如点击一个颜色,未点击一个颜色 //vectorDrawableCompat.setTintList(ColorStateList.valueOf(colorRid)); //3.用这个v4提供的也可,这个适用于任意的drawable着色 //DrawableCompat.setTint(vectorDrawableCompat,.getResources().getColor(colorRid)); imageView.setImageDrawable(vectorDrawableCompat);}
例如:
//图片初始化 Utils.setVectorColorImgv(this, R.drawable.ic_index, R.color.deep_grey, imgvBottomMainIndex);
1 0
- Android---矢量图(Vector)使用方法
- Android 矢量图 vector 制作
- android vector矢量图
- android vector矢量图画图详解
- Android的Vector矢量图绘制
- Android Vector Drawable | SVG 矢量图
- Android中的矢量图Vector Drawable
- android vector矢量图的简单使用教程
- Android studio使用vector创建矢量图并加载
- vector构造SVG矢量图
- Android矢量图
- Android使用SVG矢量图打造酷炫动效! 以及用ps生成vector
- 安卓开发之Android Studio在drawable目录下添加矢量图vector XML文件
- vector使用方法
- vector使用方法
- vector使用方法
- vector使用方法
- vector使用方法
- 最小周期串
- nginx提示502 错误
- Keepalived + Nginx实现高可用 Web负载均衡
- java字符串排序
- ElasticSearch速学
- Android---矢量图(Vector)使用方法
- C++ Primer Chapter 2
- MPI之聚合通信-Scatter,Gather,Allgather
- log4j配置详解,附带hibernate
- leetcode 217. Contains Duplicate
- bootstrap学习总结-css样式设计(一)
- linux多线程编程关键字:__thread,__bulitin_expect
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- 设置CATALINA_HOME环境变量