Android使用Vector进行适配和瘦身,2021Android常见面试题

本文主要是介绍Android使用Vector进行适配和瘦身,2021Android常见面试题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

app:layout_heightPercent=“50%”

app:layout_widthPercent=“100%”

app:srcCompat="@drawable/ic_icon_one" />

在代码中设置:

mImageview.setImageResource(R.drawable.ic_icon_one);

在TextView中使用:

因此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"/>

<target

android:name=“right”

android:animation="@animator/anim_right"/>

  • 使用动画

private void initView() {

mImageview = (ImageView) findViewById(R.id.imageview);

AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(

this, R.drawable.animated_vector

);

mImageview.setImageDrawable(animatedVectorDrawableCompat);

((Animatable) mImageview.getDrawable()).start();

}

  • 使用动画

private void initView() {

mImageview = (ImageView) findViewById(R.id.imageview);

AnimatedVectorDrawableCompat animatedVectorDrawableCompat = AnimatedVectorDrawableCompat.create(

this, R.drawable.animated_vector

);

mImageview.setImageDrawable(animatedVectorDrawableCompat);

((Animatable) mImageview.getDrawable()).start();

}

这篇关于Android使用Vector进行适配和瘦身,2021Android常见面试题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/276349

相关文章

使用Python开发Markdown兼容公式格式转换工具

《使用Python开发Markdown兼容公式格式转换工具》在技术写作中我们经常遇到公式格式问题,例如MathML无法显示,LaTeX格式错乱等,所以本文我们将使用Python开发Markdown兼容... 目录一、工具背景二、环境配置(Windows 10/11)1. 创建conda环境2. 获取XSLT

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

浅析如何使用xstream实现javaBean与xml互转

《浅析如何使用xstream实现javaBean与xml互转》XStream是一个用于将Java对象与XML之间进行转换的库,它非常简单易用,下面将详细介绍如何使用XStream实现JavaBean与... 目录1. 引入依赖2. 定义 JavaBean3. JavaBean 转 XML4. XML 转 J

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl