Android使用Vector进行适配和瘦身,从入门到精通

因此Textview(包括Butten等控件)中使用Vector只能使用代码设置:

mTextview = (TextView) findViewById(R.id.textview);

mTextview.setBackgroundResource(R.drawable.ic_icon_one);

Vector在布局选择器中的使用


  • 首先构建两个Vector

vector1.xml

<vector xmlns:android=“http://schemas.android.com/apk/res/android”

android:width=“24dp”

android:height=“24dp”

android:viewportHeight=“24.0”

android:viewportWidth=“24.0”>

<path

android:fillColor=“#FF000000”

android:pathData=“M14.59,8L12,10.59 9.41,8 8,9.41 10.59,12 8,14.59 9.41,16 12,13.41 14.59,16 16,14.59 13.41,12 16,9.41 14.59,8zM12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z” />

vector2.xml

<vector xmlns:android=“http://schemas.android.com/apk/res/android”

android:width=“24dp”

android:height=“24dp”

android:viewportHeight=“24.0”

android:viewportWidth=“24.0”>

<path

android:fillColor=“#fa0303”

android:pathData=“M14.59,8L12,10.59 9.41,8 8,9.41 10.59,12 8,14.59 9.41,16 12,13.41 14.59,16 16,14.59 13.41,12 16,9.41 14.59,8zM12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z” />

  • 创建选择器

selector.xml

<?xml version="1.0" encoding="utf-8"?>
  • 使用选择器
<?xml version="1.0" encoding="utf-8"?>

<android.support.percent.PercentFrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“tsou.com.simple.mytestdemo.SVGButtenActivity”>

<Button

android:id=“@+id/butten”

android:layout_width=“100dp”

android:layout_height=“100dp”

android:layout_gravity=“center”

android:background=“@drawable/selector” />

<RadioButton

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_margin=“10dp”

android:button=“@drawable/selector”

android:text=“我是RadioButton” />

</android.support.percent.PercentFrameLayout>

动态Vector(使用Vector实现动画)


  • 使用Vector构建动画内容

<vector xmlns:android=“http://schemas.android.com/apk/res/android”

android:width=“120dp”

android:height=“120dp”

android:viewportHeight=“24.0”

android:viewportWidth=“24.0”>

<path

android:fillColor=“#FF000000”

android:pathData=“M9.01,14L2,14v2h7.01v3L13,15l-3.99,-4v3”/>

<path

android:fillColor=“#FF000000”

android:pathData=“M14.99,13v-3L22,10L22,8h-7.01L14.99,5L11,9l3.99,4”/>

可以发现,这里的Vector图像比之前我们看见的要多了一个group标签。group标签的作用有两个:

对Path进行分组,由于我们后面需要针对Path进行动画,所以可以让具有同样动画效果的Path在同一个Group中;

拓展动画效果,单个的path标签是没有translateX和translateY属性的,因此无法使用属性动画来控制path translateY,而group标签是有的,所以我们需要先将相关的path标签元素包裹在一个个的group标签中.

  • 动画效果

anim_left.xml

<objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android”

android:duration=“1000”

android:interpolator=“@android:interpolator/anticipate_overshoot”

android:propertyName=“translateX”

android:repeatCount=“infinite”

android:repeatMode=“reverse”

android:valueFrom=“0”

android:valueTo=“-10”

android:valueType=“floatType” />

anim_right.xml

<objectAnimator

xmlns:android=“http://schemas.android.com/apk/res/android”

android:duration=“1000”

android:interpolator=“@android:interpolator/anticipate_overshoot”

android:propertyName=“translateX”

android:repeatCount=“infinite”

android:repeatMode=“reverse”

android:valueFrom=“0”

android:valueTo=“10”

android:valueType=“floatType”/>

  • 动态Vector

<animated-vector

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:drawable=“@drawable/ic_arrow”

tools:targetApi=“lollipop”>

<target

android:name=“left”

android:animation=“@animator/anim_left”/>

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

上面这些公司都是时下最受欢迎的互联网大厂,他们的职级、薪资、福利也都讲的差不多了,相信大家都是有梦想和野心的人,心里多少应该都有些想法。

也相信很多人也都在为即将到来的金九银十做准备,也有不少人的目标都是这些公司。

我这边有不少朋友都在这些厂工作,其中也有很多人担任过面试官,上面的资料也差不多都是从朋友那边打探来的。除了上面的信息,我这边还有这些大厂近年来的面试真题及解析,以及一些朋友出于兴趣和热爱一起整理的Android时下热门知识点的学习资料

部分文件:


上面这些公司都是时下最受欢迎的互联网大厂,他们的职级、薪资、福利也都讲的差不多了,相信大家都是有梦想和野心的人,心里多少应该都有些想法。

也相信很多人也都在为即将到来的金九银十做准备,也有不少人的目标都是这些公司。

我这边有不少朋友都在这些厂工作,其中也有很多人担任过面试官,上面的资料也差不多都是从朋友那边打探来的。除了上面的信息,我这边还有这些大厂近年来的面试真题及解析,以及一些朋友出于兴趣和热爱一起整理的Android时下热门知识点的学习资料

部分文件:
[外链图片转存中…(img-6JUEUSL2-1711734832504)]
[外链图片转存中…(img-GbW2t6Jz-1711734832504)]
[外链图片转存中…(img-Lq7FkJPB-1711734832504)]

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 23
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值