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

相关文章

使用Dify访问mysql数据库详细代码示例

《使用Dify访问mysql数据库详细代码示例》:本文主要介绍使用Dify访问mysql数据库的相关资料,并详细讲解了如何在本地搭建数据库访问服务,使用ngrok暴露到公网,并创建知识库、数据库访... 1、在本地搭建数据库访问的服务,并使用ngrok暴露到公网。#sql_tools.pyfrom

使用mvn deploy命令上传jar包的实现

《使用mvndeploy命令上传jar包的实现》本文介绍了使用mvndeploy:deploy-file命令将本地仓库中的JAR包重新发布到Maven私服,文中通过示例代码介绍的非常详细,对大家的学... 目录一、背景二、环境三、配置nexus上传账号四、执行deploy命令上传包1. 首先需要把本地仓中要

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

Java使用Mail构建邮件功能的完整指南

《Java使用Mail构建邮件功能的完整指南》JavaMailAPI是一个功能强大的工具,它可以帮助开发者轻松实现邮件的发送与接收功能,本文将介绍如何使用JavaMail发送和接收邮件,希望对大家有所... 目录1、简述2、主要特点3、发送样例3.1 发送纯文本邮件3.2 发送 html 邮件3.3 发送带

Nginx如何进行流量按比例转发

《Nginx如何进行流量按比例转发》Nginx可以借助split_clients指令或通过weight参数以及Lua脚本实现流量按比例转发,下面小编就为大家介绍一下两种方式具体的操作步骤吧... 目录方式一:借助split_clients指令1. 配置split_clients2. 配置后端服务器组3. 配

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Spring-AOP-ProceedingJoinPoint的使用详解

《Spring-AOP-ProceedingJoinPoint的使用详解》:本文主要介绍Spring-AOP-ProceedingJoinPoint的使用方式,具有很好的参考价值,希望对大家有所帮... 目录ProceedingJoinPoijsnt简介获取环绕通知方法的相关信息1.proceed()2.g