android 提供的了两种机制你可以用来创建简单的动画:tweedned animation(渐变动画) 和 frame-by-frame animation(逐帧动画)(有道翻译的,汗!!!) 。这里主要介绍tweedned animation中的四种动画形式:alpha(淡入淡出效果)、scale(放缩)、rotate(旋转) 、translate(平移)。

那么怎么来实现这样的效果呢?大家向下看:(这里是在代码内部实现,虽然容易看,但是不利于维护和重用,下面还会介绍一种在外部实现的方法)

内部实现:

1.创建一个AnimationSet对象

2.创建一个你想要实现效果的Animation对象,如AlphaAnimations

3.为动画设置一些属性

4.将alpha对象加入到AnimationSet中

5.对图片开始执行AnimationSet

    private class alphaButtonListener implements OnClickListener{

        @Override
public void onClick(View v) {
//建立一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,设置透明度为由1到0
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置整个动画持续的时间
alphaAnimation.setDuration(10000);
//将Animation对象添加到animationSet对象中
animationSet.addAnimation(alphaAnimation);
//对图片开始执行AnimationSet
imagView.startAnimation(animationSet);
}
}

=========================

这里介绍一些常用的动画属性的设置

  • setDuration(long durationMillis):用于设置动画的持续时间,单位是毫秒。
  • setFillAfter:设置为true后,该动画专化在动画结束后应用,也就是说,动画将会停留在结束时的状态
  • setFillBefore:设置为true后,在动画开始前应用动画转化。(这个系统默认是设置为true的)(这个我有点不太懂,网上有的说是设置动画完成后回到起始位置,好像不太对,如果你知道的话,一定要留言告诉我)
  • setStartOffset:设置当发出开始动画的指令后经过多长时间开始执行动画
  • setRepeatMode:设置动画的重复次数

===========================

下面是具体的ManiActivity.java代码,对各个动画都有详细的解释:

package com.mecury.animationstest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imagView;
private Button alphaButton;
private Button scaleButton;
private Button rotateButton;
private Button translateButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imagView = (ImageView) findViewById(R.id.imageView);
alphaButton = (Button) findViewById(R.id.alphaButton);
alphaButton.setOnClickListener(new alphaButtonListener());
scaleButton = (Button) findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new scaleButtonListener());
rotateButton = (Button) findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new rotateButtonListener());
translateButton = (Button) findViewById(R.id.translateButton);
translateButton.setOnClickListener(new translateButtonListener());
} /*
* 淡入淡出效果
*/
private class alphaButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
//建立一个AnimationSet对象
AnimationSet animationSet = new AnimationSet(true);
//创建一个AlphaAnimation对象,设置透明度为由1到0
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置整个动画持续的时间
alphaAnimation.setDuration(10000);
//将Animation对象添加到animationSet对象中
animationSet.addAnimation(alphaAnimation);
//对图片开始执行AnimationSet
imagView.startAnimation(animationSet);
}
} /*
* 缩放效果
*/
private class scaleButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
/*这里用的是百分比表示法,f表示浮点型
* 四个参数代表缩放的比例,前两个代表了x轴的缩放(这里用的是百分比表示法:1f代表原来的宽度,0.5f代表原来宽度的一半)后两个代表了y轴的缩放
* Animation中有两个常用的属性,RELATIVE_TO_SELF(依赖自身),RELATIVE_TO_PARENT(依赖父控件)
* 后面四个表明缩放的轴为依赖自身的在x为0.5f,y为0.5f的地方
*/
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.1f, 1f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(2000); imagView.startAnimation(animationSet);
}
} /*
* 旋转效果
*/
private class rotateButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
/*第一个参数,表示图片相对于起始位置的角度,
*第二个参数:表示动画结束时图片的偏转角度。这里就是由0变到360即原点
*后面的四个参数是为了设置图片旋转围绕的轴,意思是轴的横坐标是依赖父控件的0f处,纵坐标是依赖父控件的-1f处
*这里要着中说的是:关于坐标的问题。这这个动画中,坐标原点是图片的左上角,原点左x轴和下y轴代表正轴
*此时的围绕的点的坐标轴就在图片的正上方
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, -1f);
rotateAnimation.setDuration(2000);
animationSet.addAnimation(rotateAnimation);
imagView.startAnimation(animationSet);
}
} /*
* 移动效果
*/
private class translateButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
/*
* 前两个参数决定了动画开始时图片的x轴起始位置
* 第三四个参数表示图片将要到达位置的x轴
* 后面四个是表示y轴的和上面类似
* 代码中表示:由起始位置位移到右上角
*/
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_PARENT, 1f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_PARENT, -1f);
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
imagView.startAnimation(animationSet);
}
}
}

下面是我的布局文件:

<RelativeLayout 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="com.mecury.animationstest.MainActivity" > <Button
android:id="@+id/translateButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:text="Translate平移"/>
<Button
android:id="@+id/rotateButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_above="@id/translateButton"
android:text="Rotate旋转"/>
<Button
android:id="@+id/scaleButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_above="@id/rotateButton"
android:text="Scale缩放"/>
<Button
android:id="@+id/alphaButton"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_above="@id/scaleButton"
android:text="Alpha淡入淡出"/> <ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
     android:layout_centerInParent="true"
android:src="@drawable/china"/> </RelativeLayout>

在外部资源中实现:

1.在res文件中新建一个名为anim的文件

2.在文件中中新建.xml文件(这里以alpha为例),如alpha.xml

3.在文件中设置相应的属性

4.使用AnimationUtils来装载动画设置文件

这里我的anim中四个动画代码文件:

<?xml version="1.0" encoding="utf-8"?>
<!-- aplha是一个淡入淡出的效果
startOffser时间偏移量,在动画开始之前,停顿500毫秒 -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:startOffset="500"
android:duration="2000"/>
</set> <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- scale是实现缩放
privat 是设置缩放围绕的轴的缩放
privatX="50"使用的是绝对位置
="%50"相对控件本身定位,在控件的1/2处
="50%p" 相对于父控件的位置定位-->
<scale
android:fromXScale="1"
android:toXScale="0.1"
android:fromYScale="1"
android:toYScale="0.1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="500"
android:duration="2000"/> </set> <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="100%p"
android:pivotY="0%p"
android:startOffset="500"
android:duration="2000"/>
</set> <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%"
android:toXDelta="100%p"
android:fromYDelta="0%"
android:toYDelta="-100%p"
android:interpolator="@android:anim/decelerate_interpolator"
android:startOffset="500"
android:duration="30000"
/> </set>

intepolator是用来设置动画执行的速度

Accelerate_Decelerate_Interpolator:在动画开始和结束时速率较慢,中间加速

Accelerate_Interpolator:在动画开始的时候速度较慢,逐渐加速

Cycle_Interpolator:速度改变沿着动画循环播放的次数,以正弦曲线式变化

Deceletrate_Interpolator:在的动画开始是速度改变比较慢,然后突然减速

Linear_Interpolator:动画匀速改变

使用:android:Interpolator="@android:anim/....."

mianActivity.java:

package com.example.animation_01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imagView;
private Button alphaButton;
private Button scaleButton;
private Button rotateButton;
private Button translateButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagView = (ImageView) findViewById(R.id.imageView);
alphaButton = (Button) findViewById(R.id.alphaButton);
alphaButton.setOnClickListener(new alphaButtonListener());
scaleButton = (Button) findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new scaleButtonListener());
rotateButton = (Button) findViewById(R.id.rotateButton);
rotateButton.setOnClickListener(new rotateButtonListener());
translateButton = (Button) findViewById(R.id.translateButton);
translateButton.setOnClickListener(new translateButtonListener());
} class alphaButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
//使用AnimaionUtils来装载动画设置文件
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imagView.startAnimation(animation);
}
}
class scaleButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);
imagView.startAnimation(animation);
}
}
class rotateButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
imagView.startAnimation(animation);
}
}
class translateButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);
imagView.startAnimation(animation);
}
} }

布局文件和第一个一样。

android 之 animations 动画的更多相关文章

  1. 有趣的动画视图集合:Android View Animations

    Android View Animations这个项目收集了各种有趣的动画效果. 所有效果: Attension Flash, Pulse, RubberBand, Shake, Swing, Wob ...

  2. Android 三种动画详解

    [工匠若水 http://blog.csdn.net/yanbober 转载请注明出处.点我开始Android技术交流] 1 背景 不能只分析源码呀,分析的同时也要整理归纳基础知识,刚好有人微博私信让 ...

  3. Android立体旋转动画实现与封装(支持以X、Y、Z三个轴为轴心旋转)

    本文主要介绍Android立体旋转动画,或者3D旋转,下图是我自己实现的一个界面 立体旋转分为以下三种: 1. 以X轴为轴心旋转 2. 以Y轴为轴心旋转 3. 以Z轴为轴心旋转--这种等价于andro ...

  4. Android中矢量动画

    Android中矢量动画 Android中用<path> 标签来创建SVG,就好比控制着一支画笔,从一点到一点,动一条线. <path> 标签 支持一下属性 M = (Mx, ...

  5. Android Animation(动画)

    前言 Android 平台提供实现动画的解决方案(三种) 一.3.0以前,android支持两种动画: (1)Frame Animation:顺序播放事先做好的图像,与gif图片原理类似,是一种逐帧动 ...

  6. android 补间动画和Animation

    介绍: 补间动画是一种设定动画开始状态.结束状态,其中间的变化由系统计算补充.这也是他叫做补间动画的原因. 补间动画由Animation类来实现具体效果,包括平移(TranslateAnimation ...

  7. Android中过场动画

    overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left); 第一参数为进入的动画 第二参数为退出的动画 进入的动画 ...

  8. Android 自定义波浪动画 --"让进度浪起来~"

    原文链接:http://www.jianshu.com/p/0e25a10cb9f5 一款效果不错的动画,实现也挺简单的,推荐阅读学习~ -- 由 傻小孩b 分享 waveview <Andro ...

  9. Android自定义窗口动画

    第一步,设置出现和消失的xml 1.在res/anim下创建enter_anim.xml,设置窗口出现的动画 <?xml version="1.0" encoding=&qu ...

随机推荐

  1. 【转载】Grunt常用插件介绍

    项目名称 grunt-contrib v0.8.0 项目地址 https://github.com/gruntjs/grunt-contrib 项目介绍 此项目是对grunt常用插件的集合,刚接触gr ...

  2. NOP登录验证管理

    截取Nop登录代码如下: 其实 public ActionResult Login(LoginModel model, string returnUrl, bool captchaValid) { / ...

  3. SQL Server技术问题之视图优缺点

    优点: 一.简单性.视图不仅可以简化用户对数据的理解,也可以简化他们的操作.那些被经常使用的查询可以被定义为视图,从而使用户不必为以后的操作每次都指定全部的条件. 二.安全性.通过视图用户只能查询和修 ...

  4. 第二sprint总结

    总结:在第二轮sprint中,团队的开发工作出现了极大的懈怠,团队成员分配的工作完成情况较差,进度较慢,希望能在最后一周中,完成预定的功能模块 个人贡献分: 124陈彦生:22=20*5*22% 14 ...

  5. Github教程(3)

    Pull Request Pull Request 是自己修改源代码后,请求对方仓库采纳该修改时采取的一种行为. 场景1: 用户A在fork完用户B的项目时,A修改了代码并提交了一个Pull Requ ...

  6. 用Qt写软件系列三:一个简单的系统工具之界面美化

    前言 在上一篇中,我们基本上完成了主要功能的实现,剩下的一些导出.进程子模块信息等功能,留到后面再来慢慢实现.这一篇来讲述如何对主界面进行个性化的定制.Qt库提供的只是最基本的组件功能,使用这些组件开 ...

  7. Chrome弹窗的简单应用(选择结构与循环结构)

    ★选择结构★ ★JS实现弹窗显示随机数 示例代码效果图   ★ 弹窗实现对随机数的进一步判断 示例代码效果图 ★综合应用   比较大小 ★ 判断成绩等级 ): : : : : alert(" ...

  8. [水]用vb写了个PCB

    这学期我们学操作系统,所以得写个PCB. 于是我借鉴了一下windows的PCB,写了这个 Imports System.Runtime.InteropServices ''' <summary ...

  9. 【AngularJS学习笔记】02 小杂烩及学习总结

    表格示例 <div ng-app="myApp" ng-controller="customersCtrl"> <table> < ...

  10. C#的回调方法

    C# 里面回调方法一般指某个委托.也可以说是接口. using System; using System.Collections.Generic; using System.Linq; using S ...