关于动画的实现,Android提供了Animation,在Android SDK介绍了2种Animation模式:
1. Tween Animation:通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果,即是一种渐变动画;
2. Frame Animation:顺序播放事先做好的图像,是一种画面转换动画。
动画类型
下面先来看看Android提供的动画类型。Android的animation由四种类型组成
在XML文件中:
- alpha 渐变透明度动画效果
- scale 渐变尺寸伸缩动画效果
- translate 画面转换位置移动动画效果
- rotate 画面转移旋转动画效果
动画的实现方法:
animation可以定义在xml中,也 可以在类中动态定义,和控件的定义类似。
这里用的是静态定义。
首先需要在项目的res文件夹 中新建anim文件夹,然后在其中添加动画文件。
在类中调用的时候,要用到下面的两个语句:
nimation ani = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_anim);
View.startAnimation(ani);
今天的例子要实现的是TweenAnimation,类似于Flash中的补间动画。
最终效果:
很小的一个bug却卡了一个晚上(xml文件定义的时候,尖括号位置敲错了),如果再给我遇到这样的bug,
我会轻轻地对它说:艹!
代码明天上...睡觉。
代码清单:
主Activity:
package com.example.animationtest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class AnimationActivity extends Activity {
private ImageView myImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animation);
myImageView=(ImageView)findViewById(R.id.myImageView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,1,R.string.scale);
menu.add(0,2,2,R.string.rotate);
menu.add(0,3,3,R.string.alpha);
menu.add(0,4,4,R.string.translate);
menu.add(0,5,5,R.string.exit);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1) scaleAnim();
if(item.getItemId()==2) rotateAnim();
if(item.getItemId()==3) alphaAnim();
if(item.getItemId()==4) transAnim();
if(item.getItemId()==5) finish();
return super.onMenuItemSelected(featureId, item);
}
private void rotateAnim() {
// TODO Auto-generated method stub
Animation ani = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_anim);
myImageView.startAnimation(ani);
}
private void alphaAnim() {
// TODO Auto-generated method stub
Animation ani = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_anim);
myImageView.startAnimation(ani);
}
private void scaleAnim() {
// TODO Auto-generated method stub
Animation ani = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_anim);
myImageView.startAnimation(ani);
}
private void transAnim()
{
Animation ani = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate_anim);
myImageView.startAnimation(ani);
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:background="#FFFFFF" >
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:scaleType="matrix"
android:src="@drawable/pic" >
</ImageView>
</LinearLayout>
动画文件(注意尖括号的位置,格式一定要正确!)
移动动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:repeatCount="2"
android:fromXDelta="-30"
android:fromYDelta="-30"
android:toXDelta="-80"
android:toYDelta="200"
android:duration="3000"
/>
</set>
旋转动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="2"
android:fromDegrees="0"
android:toDegrees="+270"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
</set>
透明度动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0" >
</alpha>
</set>
缩放动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="500"
android:fillAfter="true"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50"
android:toXScale="1.0"
android:toYScale="1.0" >
</scale>
</set>
分享到:
相关推荐
适合初学者学习,Animation的各个方法的使用。
android 简单的帧动画,轮换播放几张图片
比较全的android动画例子,包括基础的tween animation,这个就不必说了,还有比较新的属性动画:property animation
Android 中 Tweened animation的实例详解 Tweened animation有四种类型,下面主要介绍Scale类型。 运行效果如下: Android SDK提供了2种方法:直接从XML资源中读取Animation,使用Animation子类的构造函数来初始化...
android 的动画也是比较常用的技术 主要是tween动画分为Animation和ActivityAnimation两种 适合初学者
Android 补间动画 Animation 各个动画的例子 Animation使用到自定义View里
Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View Animation相当简单,不过只能支持简单的缩放、平移、旋转、透明度基本的动画,且有一定的局限性。比如:你希望View有...
Android动画例子(AnimationExample),写了四种效果 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面转移旋转动画效果 文章:...
view动画例子
Android Animation动画演示简单实例,相信信息,请到本人博客查看http://blog.csdn.net/zml_2015/article/details/49791103
Android Animation的例子
代码为博客实例代码:http://blog.csdn.net/lmj623565791/article/details/38067475 有问题博客留言
本文实例为大家分享了利用animation-list实现帧动画的具体代码,供大家参考,具体内容如下 将要顺序播放的图片放在资源目录下 再drawable目录下新建animation1文件和animation2文件 一个是按顺序显示动画,一个是...
Animation动画实例源代码,包括详细实例代码,测试环境ADT10+ECLIPSE JUNO
Android使用TweenAnimation生成动画效果的例程,生成补间动画的例子,是一个比较基础简单的Android动画特效实例。
自己写的一个animation实例。。很适合学习animation
在这个例子中包括了众多Android Spinner 控件的用法,以一个下拉框的效果来展示很多Spinner 的使用技巧,示例中的myspinner_dropdown自定义下拉菜单模式定义在res/layout目录下,代码中演示了将ArrayAdapter添加...
Android实例(区域显示或隐藏加翻页动画)
在Android开发中,Animation是用来给控件制作效果的。大多数的控件都可以用这个类,这个类包含了4种基本动作,分别为移动,旋转,淡入淡出,缩放。在使用Animation时,可以在.java文件中用java代码对其进行设置,...