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

相关文章

Java使用ANTLR4对Lua脚本语法校验详解

《Java使用ANTLR4对Lua脚本语法校验详解》ANTLR是一个强大的解析器生成器,用于读取、处理、执行或翻译结构化文本或二进制文件,下面就跟随小编一起看看Java如何使用ANTLR4对Lua脚本... 目录什么是ANTLR?第一个例子ANTLR4 的工作流程Lua脚本语法校验准备一个Lua Gramm

Java Optional的使用技巧与最佳实践

《JavaOptional的使用技巧与最佳实践》在Java中,Optional是用于优雅处理null的容器类,其核心目标是显式提醒开发者处理空值场景,避免NullPointerExce... 目录一、Optional 的核心用途二、使用技巧与最佳实践三、常见误区与反模式四、替代方案与扩展五、总结在 Java

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

QT进行CSV文件初始化与读写操作

《QT进行CSV文件初始化与读写操作》这篇文章主要为大家详细介绍了在QT环境中如何进行CSV文件的初始化、写入和读取操作,本文为大家整理了相关的操作的多种方法,希望对大家有所帮助... 目录前言一、CSV文件初始化二、CSV写入三、CSV读取四、QT 逐行读取csv文件五、Qt如何将数据保存成CSV文件前言

Qt中QUndoView控件的具体使用

《Qt中QUndoView控件的具体使用》QUndoView是Qt框架中用于可视化显示QUndoStack内容的控件,本文主要介绍了Qt中QUndoView控件的具体使用,具有一定的参考价值,感兴趣的... 目录引言一、QUndoView 的用途二、工作原理三、 如何与 QUnDOStack 配合使用四、自

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

使用Python构建一个Hexo博客发布工具

《使用Python构建一个Hexo博客发布工具》虽然Hexo的命令行工具非常强大,但对于日常的博客撰写和发布过程,我总觉得缺少一个直观的图形界面来简化操作,下面我们就来看看如何使用Python构建一个... 目录引言Hexo博客系统简介设计需求技术选择代码实现主框架界面设计核心功能实现1. 发布文章2. 加

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a