Android常用动画Animation的使用
Andriod中有几种常用的Animation
AlphaAnimation 淡入淡出效果
RotateAnimation 旋转效果
ScaleAnimation 缩放动画
TranslaAnimation 移动动画
这几种动画可以通过xml实现也可以通过java代码实现,先看下在代码中是怎样实现了
在布局文件(animation.xml)中声明几个效果的按钮和图片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/rotate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="rotate演示" /> <Button
android:id="@+id/scale"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="scale演示" /> <Button
android:id="@+id/translate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="translate演示" /> <Button
android:id="@+id/alpha"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="alpha演示" /> <ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dip"
android:src="@drawable/ic_launcher" /> </LinearLayout>
在Activity中
package com.example.animation; import com.example.widgetdemo.R; 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 AnimationDemo extends Activity {
private Button rotate = null;
private Button scale = null;
private Button translate = null;
private Button alpha = null;
private ImageView image = null; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.animation);
rotate = (Button) findViewById(R.id.rotate);
scale = (Button) findViewById(R.id.scale);
translate = (Button) findViewById(R.id.translate);
alpha = (Button) findViewById(R.id.alpha);
image = (ImageView) findViewById(R.id.image); rotate.setOnClickListener(new rotateListener());
scale.setOnClickListener(new scaleListener());
translate.setOnClickListener(new translateListener());
alpha.setOnClickListener(new alphaListener());
} /**
* 旋转动画
* @author Administrator
*
*/
class rotateListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation totateAnimation = new RotateAnimation(0, //旋转开始角度
360, //旋转结束角度
Animation.RELATIVE_TO_SELF, //X轴的旋转类型有三种选择Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT
2f, //X轴的旋转值
Animation.RELATIVE_TO_SELF,
0f);
//动画持续时间
totateAnimation.setDuration(2000);
//添加动画效果
animationSet.addAnimation(totateAnimation);
//为图片添加动画
image.startAnimation(animationSet);
}
} /**
* 缩放动画
* @author Administrator
*
*/
class scaleListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,
0.1f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet);
}
} /**
* 移动
* @author Administrator
*
*/
class translateListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
translateAnimation.setDuration(2000);
animationSet.addAnimation(translateAnimation);
image.startAnimation(animationSet);
} } /**
* 渐变动画 淡入淡出
* @author Administrator
*
*/
class alphaListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
animationSet.setStartOffset(1000); // 1s后开始
animationSet.addAnimation(alphaAnimation);
image.startAnimation(animationSet);
}
}
}
难点主要是每个动画的构造函数如何设置参数。
第二种实现方法
下面再来看下如何在xml中设置动画
首先在res目录下创建anim目录动画效果的配置文件
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" > <alpha
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500" /> </set>
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" > <rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360" /> </set>
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" > <scale
android:duration="3000"
android:fromXScale="1.0"
android:toXScale="0.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"/> </set>
translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" > <translate
android:duration="3000"
android:fromXDelta="50%"
android:toXDelta="100%"
android:fromYDelta="0%"
android:toYDelta="100%" /> </set>
再来看下在Activity中如何实现
package com.example.animation; 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; import com.example.widgetdemo.R; public class AnimationXmlDemo extends Activity {
private Button rotate = null;
private Button scale = null;
private Button translate = null;
private Button alpha = null;
private ImageView image = null; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.animation_xml);
rotate = (Button) findViewById(R.id.rotate);
scale = (Button) findViewById(R.id.scale);
translate = (Button) findViewById(R.id.translate);
alpha = (Button) findViewById(R.id.alpha);
image = (ImageView) findViewById(R.id.image); rotate.setOnClickListener(new rotateListener());
scale.setOnClickListener(new scaleListener());
translate.setOnClickListener(new translateListener());
alpha.setOnClickListener(new alphaListener());
} /**
* 旋转动画
* @author Administrator
*
*/
class rotateListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.rotate);
image.startAnimation(animation);
} } /**
* 缩放动画
* @author Administrator
*
*/
class scaleListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.scale);
image.startAnimation(animation); } } /**
* 移动
* @author Administrator
*
*/
class translateListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.translate);
image.startAnimation(animation);
}
} /**
* 渐变动画
* @author Administrator
*
*/
class alphaListener implements OnClickListener { @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Animation animation = AnimationUtils.loadAnimation(AnimationXmlDemo.this, R.anim.alpha);
image.startAnimation(animation);
} }
}
在代码中和xml中实现的效果都是一样的,最后上图
完整的代码可以到以下链接下载
Android常用动画Animation的使用的更多相关文章
- Android常用动画Frame-By-Frame Animations的使用
在Android的动画中有一种叫做Frame by Frame 的动画效果,就是跟Flash播放一样,是一帧一帧地显示,如果动画是连续并且有规律的话,就跟播放视频一样. 首先在drawable目录下添 ...
- Android常用动画alpha和rotate同时使用
Android的动画可以是一种动画,也可以多种动画作用于一张图片上,如RotaeAnimation和AlphaAnimation同时放到一个配置文件中 alpha1.xml <?xml vers ...
- Android 常用动画
一.动画类型 Android的animation由四种类型组成:alpha.scale.translate.rotate XML配置文件中 alpha :渐变透明度动画效果 scale :渐变尺寸伸缩 ...
- 虾扯蛋:Android View动画 Animation不完全解析
本文结合一些周知的概念和源码片段,对View动画的工作原理进行挖掘和分析.以下不是对源码一丝不苟的分析过程,只是以搞清楚Animation的执行过程.如何被周期性调用为目标粗略分析下相关方法的执行细节 ...
- Android 常用动画小结
1. 渐入动画 // Request the next activity transition (here starting a new one). startActivity(new Intent( ...
- Android 常用动画之RotateAnimation
前两天接到任务做一个UI,有用到动画,于是抽空看了下Android动画相关知识. Android Animation共有四大类型,分别是 Alpha 透明度动画 Scale 大小伸 ...
- Android 曲线动画animation,类似加入购物车动画
按照惯例先放效果图:图中小球做抛物线运动 资源图片 1.首先布局文件activity_main.xml,布局很简单,就一个测试按钮 <RelativeLayout xmlns:android=& ...
- Android 一般动画animation和属性动画animator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- css3常用动画+动画库
一.animates.css animate.css是来自dropbox的工程师Daniel Eden开发的一款CSS3的动画效果小类库.包含了60多款不同类型的CSS3动画,包括:晃动,闪动,各种淡 ...
随机推荐
- 输出1到最大的N位数
题目:输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. 分析:这是一道很有意思的题目.看起来很简单,其实里面却有不少的玄机. 应聘者在解决这个问 ...
- Linux Centos 系统上安装BT客户端 Transmission
Linux Centos 系统上安装BT客户端 Transmission Transmission是一种BitTorrent客户端,特点是一个跨平台的后端和其上的简洁的用户界面,以MIT许可证和G ...
- 使用Boost.Asio编写通信程序
摘要:本文通过形像而活泼的语言简单地介绍了Boost::asio库的使用,作为asio的一个入门介绍是非常合适的,可以给人一种新鲜的感觉,同时也能让体验到asio的主要内容. Boost.Asio是一 ...
- Mysql 创建联合主键
Mysql 创建联合主键2008年01月11日 星期五 下午 5:21使用primary key (fieldlist) 比如: create table mytable ( ...
- Android JNI入门第四篇——Android.mk文件分析
ndroid.mk文件是在使用NDK编译C代码时必须的文件,Android.mk文件中描述了哪些C文件将被编译且指明了如何编译.掌握Android.mk文件的编写主要是掌握其里头将要使用的一些关键字, ...
- 「OC」@property @synthesize和id
一.@property @synthesize关键字 这两个关键字是编译器特性,让Xcode可以自动生成getter和setter. (一)@property 关键字 @property 关键字可以自 ...
- Sublime Text 2使用技巧汇总
一.下载链接: Windows-64bit: http://pan.baidu.com/s/1o6QdKYu 其它版本请移步官网: http://www.sublimetext.com/ 二.破解Li ...
- js this [转]
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...
- Linux(CentOS或RadHat)下MySQL源码安装
安装环境: CentOS6.3 64位 软件: Mysql-5.6 所需包: gcc/g++ :MySQL 5.6开始,需要使用g++进行编译.cmake :MySQL 5.5开始,使用cmake进 ...
- Introduction to Guid ( globally unique identifier )
什么是 GUID? 全球唯一标识符 (GUID) 是一个字母数字标识符,用于指示产品的唯一性安装. 在许多流行软件应用程序(例如 Web 浏览器和媒体播放器)中,都使用 GUID. GUID 的格式为 ...