当前位置: 首页 > 编程日记 > 正文

android开发之动画的详解 整理资料 Android开发程序小冰整理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

/** * 作者:David Zheng on 2015/11/7 15:38 * * 
网站:http://www.93sec.cc * *
 微博:http://weibo.com/mcxiaobing * * 
微博:http://weibo.com/93sec.cc */ 个人交流QQ986945193

一、分类:
(一)、概要:
3.0以前,android支持两种动画模式,补间动画(tween animation),帧动画(frame animation),在android3.0中又引入了一个新的动画系统:属性动画(property animation)。
这三种动画模式在SDK中被称为view animation,drawable animation,property animation。

(二)、动画资源分类:
  1. 属性动画:Property Animation
  2. 帧动画:Frame Animation (Drawable Animation)
  3. 补间动画:Tween Animation (View Animation)
    • 透明度补间动画
    • 缩放补间动画
    • 旋转补间动画
    • 移动补间动画

二、补间动画:
View Animation就是一系列View形状的变换,如大小的缩放、透明度的改变、位置的改变、旋转位置改变,动画的定义既可以用java代码定义也可以用XML定义。建议用XML定义。
用XML定义的动画放在 /res/anim/文件夹内,XML文件的根元素为<set> , 二级节点可为< alpha>,< scale>,< translate>,< rotate>。
(一)、用xml资源实现补间动画:

(二)、用java代码实现补间动画:

public class MainActivity extends Activity {

private ImageView imageView_main;
private Animation animation = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
imageView_main = (ImageView) findViewById(R.id.imageView_main);
}

public void clickButton(View view) {
switch (view.getId()) {
case R.id.button_main_alpha:
animation = new AlphaAnimation(0.0f, 1.0f);
break;
case R.id.button_main_scale:animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 1.0f);break;case R.id.button_main_translate:animation = new TranslateAnimation(0, 150, 0, 0);break;case R.id.button_main_rotate:animation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);break;
default:break;}
animation.setDuration(3000);
imageView_main.setAnimation(animation);
}
}


三、帧动画:
Frame Animation(AnimationDrawable对象):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。
必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。XML文件要放在 /res/anim/或者/ res/animator或者 /res/drawable目录下。
(一)、实例代码:
一、res/anim/frame_animation.xml的代码:


<animation-listxmlns:android="http://schemas.android.com/apk/res/android"

android:oneshot="true">

<itemandroid:drawable="@drawable/anim1"android:duration="50"/>

<itemandroid:drawable="@drawable/anim2"android:duration="50"/>

<itemandroid:drawable="@drawable/anim3"android:duration="50"/>

<itemandroid:drawable="@drawable/anim4"android:duration="50"/>

<itemandroid:drawable="@drawable/anim5"android:duration="50"/>

<itemandroid:drawable="@drawable/anim6"android:duration="50"/>

<itemandroid:drawable="@drawable/anim7"android:duration="50"/>

<itemandroid:drawable="@drawable/anim8"android:duration="50"/>

<itemandroid:drawable="@drawable/anim9"android:duration="50"/>

<itemandroid:drawable="@drawable/anim10"android:duration="50"/>

<itemandroid:drawable="@drawable/anim11"android:duration="50"/>

<itemandroid:drawable="@drawable/anim12"android:duration="50"/>

</animation-list>


二、MainActivity.java代码:
public class MainActivity extends Activity {

private ImageView imageView_main_show;

private AnimationDrawable animationDrawable = null;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


imageView_main_show = (ImageView) findViewById(R.id.imageView_main_show);

imageView_main_show.setBackgroundResource(R.anim.frame_animation);

animationDrawable = (AnimationDrawable) imageView_main_show.getBackground();

}


public void clickButton(View view) {

switch (view.getId()) {

case R.id.button_main_start:

if (!animationDrawable.isRunning()) {

//一组动画是否只播放一次

animationDrawable.setOneShot(false);

animationDrawable.start();

}

break;

case R.id.button_main_stop:

if (animationDrawable.isRunning()) {

animationDrawable.stop();

}

break;

}

}


@Override

public void onWindowFocusChanged(boolean hasFocus) {

super.onWindowFocusChanged(hasFocus);

if (!animationDrawable.isRunning()) {

animationDrawable.setOneShot(false);

animationDrawable.start();

}

}

}



【备注:】
SDK中提到,不要在onCreate()中调用start(),因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在 onWindowFoucsChanged()中调用 start()。


四、属性动画:
(一)、概念:
属性动画,这个是在Android 3.0中才引进的。Property Animation故名思议就是通过动画的方式改变对象的属性.属性动画更改的是对象的实际属性,在View Animation(Tween Animation)中,其改变的是 View的绘制效果,真正的View的属性保持不变
比如无论如何缩放Button的大小,Button的有效点击区域还是没有应用动画时的区域,其位置与大小都不变。而在Property Animation中,改变的是对象的实际属性,如Button的缩放,Button的位置与大小属性值都改变了。
Property Animation不止可以应用于View,还可以应用于任何对象。Property Animation只是表示一个值在一段时间内的改变,当值改变时要做什么事情完全是你自己决定的。

(二)、常用属性:
  1. Duration动画的持续时间,默认300ms。
  2. Time interpolation:时间插值。LinearInterpolator、AccelerateDecelerateInterpolator,定义动画的变化率。
  3. Repeat count and behavior:重复次数、以及重复模式;可以定义重复多少次;重复时从头开始,还是反向。
  4. Animator sets: 动画集合,你可以定义一组动画,一起执行或者顺序执行。
  5. Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默认为10ms,但最终依赖系统的当前状态;基本不用管。
(三)、相关的类:
  1. ObjectAnimator 动画的执行类(常用属性:alpha,rotation,rotationX,rotationY,translationX,translationY,scaleX,scaleY)
  2. ValueAnimator 动画的执行类
  3. AnimatorSet 用于控制一组动画的执行:线性,一起,每个动画的先后执行等。
  4. AnimatorInflater 用户加载属性动画的xml文件
  5. TypeEvaluator 类型估值,主要用于设置动画操作属性的值。
  6. TimeInterpolator 时间插值
  • 总的来说,属性动画就是,动画的执行类来设置动画操作的对象的属性、持续时间,开始和结束的属性值,时间差值等,然后系统会根据设置的参数动态的变化对象的属性。

(一)、实例代码:
一、res/anim/property_anim.xml的代码:

<setxmlns:android="http://schemas.android.com/apk/res/android"

    android:ordering="sequentially">

<objectAnimator

        android:duration="4000"

        android:propertyName="x"

        android:valueTo="300"

        android:valueType="intType"/>

    <objectAnimator

        android:duration="4000"

        android:propertyName="y"

        android:valueTo="400"

        android:valueType="intType"/>

    

<objectAnimator

        android:duration="4000"

        android:propertyName="x"

        android:valueTo="0"

        android:valueType="intType"/>

    <objectAnimator

        android:duration="4000"

        android:propertyName="y"

        android:valueTo="0"

        android:valueType="intType"/>

</set>


二、MainActivity.java代码:

public class MainActivity extends Activity {

private ImageView imageView_main_obj;

private Move move;


@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageView_main_obj = (ImageView) findViewById(R.id.imageView_main_obj);

move = new Move();


imageView_main_obj.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

setTitle(move.getX() + ":" + move.getY());

}

});

}


class Move {

private int y;

private int x;


    publicint getY() {

return y;

}


public void setY(int y) {

    this.y = y;

imageView_main_obj.layout(imageView_main_obj.getLeft(), y,imageView_main_obj.getRight(),y + imageView_main_obj.getMeasuredHeight());

}

public int getX() {

return x;

}


publi cvoid setX(int x) {

    this.x = x;

imageView_main_obj.layout(x, imageView_main_obj.getTop(), x imageView_main_obj.getMeasuredWidth(),imageView_main_obj.getBottom());

}

}


public void clickButton(View view) {

// 装载属性动画资源

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this,

R.animator.property_anim);

// 设置要控制的对象

set.setTarget(move);

// 开始动画

set.start();

}

}


【备注说明:】

<objectAnimator

        android:duration="4000"

        android:propertyName="x"

        android:valueTo="300"

        android:valueType="intType"/>


【备注:】
  • android:ordering说明一系列动画动作的执行顺序,有两个选择: sequentially 和together,顺序执行还是一起执行;
  • objectAnimator 是设定动画实施的对象;
  • duration是该动画动作执行从开始到结束所用的时间;
  • android:repeatCount="infinite"   可以是整数或者infinite
  • android:repeatMode="restart"    可以是restart 或者 reverse
  • android:valueFrom=" "     整数|浮点数|颜色
/** * 作者:David Zheng on 2015/11/7 15:38 * * 

网站:http://www.93sec.cc * *

 微博:http://weibo.com/mcxiaobing * * 

微博:http://weibo.com/93sec.cc */ 个人交流QQ986945193

转载于:https://my.oschina.net/mcxiaobing/blog/647485

相关文章:

框架源码学习笔记

1.WebListener Servlet3.0提供WebListener注解将一个实现了特定监听器接口的类定义为监听器&#xff0c;这样我们在web应用中使用监听器时&#xff0c;也不再需要在web.xml文件中配置监听器的相关描述信息了。 Web应用启动时就会初始化这个监听器 WebListener public class M…

20万个法人、百万条银行账户信息,正在暗网兜售

导语&#xff1a;推特用户爆料&#xff0c;暗网上正在出售大量中国数个银行的账号信息&#xff0c;经记者调查&#xff0c;本次打包售价 3999 美金中包含 90 万条中国农业银行账号信息&#xff0c;另外一账号还宣称出售二十个数据包&#xff0c;其中包括百万条银行账号数据、12…

2010年9月blog汇总:敏捷个人和模型驱动开发

9月份指标产品开发开始同时进行两个客户的开发&#xff0c;所以考虑了客户化如何开发的问题&#xff1b;在企业定额产品上&#xff0c;参与清单综合单价库的产品架构并做了用户调研前期准备工作&#xff1b;再就是整理了一下模型驱动开发理论以及思考了OpenExpressApp的几个建模…

Tomcat的配置及优化

Tomcat 服务器是基于Apache 软件基金会项目开发的一个免费的开放源代码的Web 应用服务器它是开发和调试JSP 程序的首选&#xff0c;主要用在中小型系统和并发访问用户不是很多的场合&#xff0c;实际Tomcat 部分是Apache 服务器的扩展&#xff0c;但它是独立运行的&#xff0c;…

JAX-WS Web 服务开发调用和数据传输分析

一. 开发服务 新建maven的web项目就可以了&#xff0c; 1.新建一个web服务 2.服务名称定义 3.更改配置 4.默认建好的服务文件 5.增加一个add的服务 import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam;/**** author Administrator*/ WebSer…

如何在高精度下求解亿级变量背包问题?

导读&#xff1a;国际顶级会议WWW2020将于4月20日至24日举行。始于1994年的WWW会议&#xff0c;主要讨论有关Web的发展&#xff0c;其相关技术的标准化以及这些技术对社会和文化的影响&#xff0c;每年有大批的学者、研究人员、技术专家、政策制定者等参与。以下是蚂蚁金服的技…

收集到的一些网络工程师面试题 和大家分享下

1: 交换机是如何转发数据包的?交换机通过学习数据帧中的源MAC地址生成交换机的MAC地址表&#xff0c;交换机查看数据帧的目标MAC地址&#xff0c;根据MAC地址表转发数据&#xff0c;如果交换机在表中没有找到匹配项&#xff0c;则向除接受到这个数据帧的端口以外的所有端口广播…

incompatible with sql_mode=only_full_group_by

使用mysql 5.7.11-debug Homebrew时报错 错误信息如下&#xff1a; 26 Mar 2016 09:35:23,432 ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper:147 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘tv2.t_pic_news…

Java动态加载一个类的几种方法以及invoke

一.加载一个类的几种方法 接口 IUser package org.me.javaapp;/**** author Administrator*/ public interface IUser {}User.java /** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templ…

今晚20:00 | 港科大郑光廷院士详解人工视觉技术发展及应用

阳春三月&#xff0c;万象更新&#xff0c;2020年注定是不平凡的一年&#xff01;有激荡就会遇见变革&#xff0c;有挑战就会迎来机遇。今天总会过去&#xff0c;未来将会怎样&#xff1f;香港科大商学院内地办事处重磅推出全新升级的《袁老师访谈录》全新系列【问诊未来院长系…

Openoffice 安装与配置

1、软件下载 路径&#xff1a;http://download.openoffice.org/ 2、软件安装 [rootOpenbo linux]# tar zxvf OOo_3.2.1_Linux_x86_install-rpm-wJRE_zh-CN.tar.gz[rootOpenbo linux]# cd OOO320_m18_native_packed-1_zh-CN.9502/[rootOpenbo OOO320_m18_native_packed-1_zh-CN.…

比较分析与数组相关的sizeof和strlen

// 形如&#xff1a; int a[]{1,2,3,4,5}; char name[]"abcdef";无论是整型数组还是字符数组&#xff0c;数组名作为右值的时候都代表数组首元素的首地址。数组发生降级&#xff08;数组名退化为数组首元素的地址&#xff09;的情况&#xff1a;数组传参、数组名参与…

Python正则表达式,看这一篇就够了

作者 | 猪哥来源 | 裸睡的猪&#xff08;ID: IT--Pig&#xff09;大多数编程语言的正则表达式设计都师从Perl&#xff0c;所以语法基本相似&#xff0c;不同的是每种语言都有自己的函数去支持正则&#xff0c;今天我们就来学习 Python中关于 正则表达式的函数。re模块主要定义了…

Spring MVC 4

Spring MVC 4 项目文件结构 pom.xml依赖 <properties><endorsed.dir>${project.build.directory}/endorsed</endorsed.dir><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies> …

SQL Server 2008高可用性系列:数据库快照

SQL Server 2008高可用性系列&#xff1a;数据库快照http://database.51cto.com 2010-09-13 14:45 我爱菊花 博客园 我要评论(0)摘要&#xff1a;我们今天要讨论的话题是数据库快照。在SQL Server 2008高可用性中&#xff0c;快照是一项很重要的内容&#xff0c;可以提供至…

PostgreSQL 9.3 beta2 stream replication primary standby switchover bug?

[更新]已有patch. 请参见.PostgreSQL 9.1,9.2,9.3 clean switchover Primary and Standby Patch. http://blog.163.com/digoal126/blog/static/16387704020136197354054/打补丁前的测试 : PostgreSQL 9.3 beta2 无法完成正常的主备角色切换.Primary : psql checkpont; pg_cont…

Apache commons-io

添加引用 <dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency>按行写&#xff1a; public static void writeFileLineByApacheIO(String fileContent) throws…

Oracle Exadata 简介

随着企业业务的发展&#xff0c;大型数据仓库越来越多&#xff0c;其规模也在迅速扩大&#xff0c;平均每两年规模增大3倍。大型数据仓库要求以最高的磁盘读取速度扫描几十、几百或几千个磁盘&#xff0c;只有磁盘和服务器之间的管道带宽增加10倍或更多才能满足此要求&#xff…

推荐系统的价值观

作者丨gongyouliu来源丨大数据与人工智能&#xff08;ID: ai-big-data&#xff09;推荐系统作为满足人类不确定性需求的一种有效工具&#xff0c;是具有极大价值的&#xff0c;这种价值既体现在提升用户体验上&#xff0c;又体现在获取商业利润上。对绝大多数公司来说&#xff…

PostgreSQL md5 auth method introduce, with random salt protect

在上一篇BLOG中介绍了不要在pg_hba.conf中使用password认证方法, 除非你的客户端和数据库服务器之间的网络是绝对安全的.http://blog.163.com/digoal126/blog/static/1638770402013423102431541/MD5方法,认证过程 : Encrypting Passwords Across A Network The MD5 authenticat…

常用Maven收集以及Maven技巧

1.完整的Maven的pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://maven.apach…

大促下的智能运维挑战:阿里如何抗住“双11猫晚”?

作者 | 阿里文娱技术专家子霖出品 | AI科技大本营&#xff08;ID:rgznai100&#xff09;2019 双 11 猫晚在全球近 190 个国家和地区播出&#xff0c;海外重保是首要任务&#xff0c;如何提升海外用户观看猫晚的体验&#xff1f;本文将详解双 11 猫晚国际化的技术挑战和技术策略…

这次真的是下定决心了

这次我想是真的&#xff0c;真的。 上上周买了一本书 数据结构 c版 看到这本书的重点 线性表第三节&#xff0c;看不下去了&#xff0c;由于我模板学的不怎么样&#xff0c;数据结构c版大部分涉及了c 的模板&#xff0c;而且我觉得这本书上的代码有些漏洞。上上周买书的第三天…

子弹实例化的代码

using UnityEngine; using System.Collections;public class fire : MonoBehaviour {public float rate 0.2f;public GameObject bullet;private void Start(){OnFire();}//实例化子弹public void Fire(){GameObject.Instantiate(bullet, transform.position, Quaternion.iden…

Shiro源码学习之一

一.最基本的使用 1.Maven依赖 <dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.2.4</version></dependency><dependency> <groupId>org.slf4j</groupId> …

传百度要与阿里、腾讯争夺在线办公市场?“百度Hi”开放520人同时在线音视频会议

在线办公市场持续火热。4月20日&#xff0c;百度旗下在线办公平台“百度Hi”再升级&#xff0c;正式发布业内大规模的520人音视频会议&#xff0c;并支持多入口快速入会&#xff0c;加码在线办公。另有消息称&#xff0c;4月底&#xff0c;百度在线办公平台将发布重磅升级&…

SQL 2008 安装资料及下载地址

SQL Server 2008 序列号&#xff1a; Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYB Enterprise: JD8Y6-HQG69-P9H84-XDTPG-34MBB 服务器设置SQL Server 代理 NT AUTHORITY\SYSTEMSQL Server Database Engine NT AUTHORITY\NETWORK SERVICE SQL Server Browser 默认 SQL Ser…

Objective-C非正式协议与正式协议

为什么80%的码农都做不了架构师&#xff1f;>>> 类别与类扩展的区别&#xff1a; ①类别中只能增加方法&#xff1b; ②是的&#xff0c;你没看错&#xff0c;类扩展不仅可以增加方法&#xff0c;还可以增加实例变量&#xff08;或者合成属性&#xff09;&#xff…

Shiro源码学习之二

接上一篇 Shiro源码学习之一 3.subject.login 进入login public void login(AuthenticationToken token) throws AuthenticationException {clearRunAsIdentitiesInternal();Subject subject securityManager.login(this, token);PrincipalCollection principals;String hos…

Widgets 整理

1.滑动条 http://www.newnaw.com/pub/sl/031.html <--!grid中的内容--> <Grid x:Name"slidergrid" HorizontalAlignment"Left" VerticalAlignment"Center" Background"Azure" Margin"20"> <StackPane…