点击每个按钮都会有对应的动画显示

activity代码:

package com.tmacsky;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
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.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationDemoActivity extends Activity {
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//定义四个动画的button属性
imageView = (ImageView)findViewById(R.id.imageView);
Button alpha = (Button)findViewById(R.id.Alpha);
alpha.setOnClickListener(new AnimationClickListener(AnimationType.Alpha));
Button rotate = (Button)findViewById(R.id.Rotate);
rotate.setOnClickListener(new AnimationClickListener(AnimationType.Rotate));
Button scale = (Button)findViewById(R.id.Scale);
scale.setOnClickListener(new AnimationClickListener(AnimationType.Scale));
Button translate = (Button)findViewById(R.id.Translate);
translate.setOnClickListener(new AnimationClickListener(AnimationType.Translate));
Button complex = (Button)findViewById(R.id.Complex);
complex.setOnClickListener(new AnimationClickListener(AnimationType.Complex));
}
//定义animationType属性
enum AnimationType{
Alpha,
Rotate,
Scale,
Translate,
Complex
}
//定义一个函数
class AnimationClickListener implements OnClickListener{
private AnimationType animationType;
public AnimationClickListener(AnimationType animType){
animationType = animType;
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (animationType) {
case Alpha:
//定义渐变动画,重复5次,持续1分钟
/*AlphaAnimation _animation = new AlphaAnimation(1f, 0.1f);
_animation.setDuration(3000);
_animation.setRepeatCount(5);
//设置循环
_animation.setRepeatMode(Animation.REVERSE);
_animation.setAnimationListener(new AnimationListener() {
//设置animation监听,依次是启动,重复,然后结束
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.i("log", "animation Start");
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
Log.i("log", "animation Repeat");
}
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.i("log", "animation End");
}
});
//启动动画
imageView.startAnimation(_animation);*/
AlphaAnimation alphaAnimation = (AlphaAnimation)AnimationUtils.loadAnimation(AnimationDemoActivity.this, R.anim.alpha);
imageView.startAnimation(alphaAnimation);
break;
case Rotate:
//定义旋转动画,旋转一周持续1分钟,重复三次,在物体的中心位置
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setRepeatCount(3);
//启动动画
imageView.startAnimation(rotateAnimation);
break;
case Scale:
//定义缩放动画,从中心坐标开始,缩放1.5倍大小,持续1分钟,重复三次
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setRepeatCount(3);
//启动动画
imageView.startAnimation(scaleAnimation);
break;
case Translate:
//定义移动动画,都从自身坐标开始,移动2个位置,持续1分钟,重复三次
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
translateAnimation.setDuration(3000);
translateAnimation.setRepeatCount(3);
//启动动画
imageView.startAnimation(translateAnimation);
break; case Complex:
//设置复杂的操作步骤,点击按钮complex后,会运行四种动画效果叠加
AnimationSet sets = new AnimationSet(false);
//定义渐变动画
AlphaAnimation _animation1 = new AlphaAnimation(1f, 0.1f);
_animation1.setDuration(3000);
//定义旋转动画,在物体的中心位置
RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation1.setDuration(3000);
//定义缩放动画,从中心坐标开始,缩放1.5倍大小
ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 1.5f, 1, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation1.setDuration(3000);
//定义移动动画,都从自身坐标开始,移动2个位置
TranslateAnimation translateAnimation1 = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2);
translateAnimation1.setDuration(3000);
//启动动画
sets.addAnimation(_animation1);
sets.addAnimation(rotateAnimation1);
sets.addAnimation(scaleAnimation1);
sets.addAnimation(translateAnimation1);
imageView.startAnimation(sets);
break;
default:
break;
}
}
}
}

layout文件:

<?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:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/qa" />
<Button
android:id="@+id/Alpha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alpha" />
<Button
android:id="@+id/Rotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rotate" />
<Button
android:id="@+id/Scale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scale" />
<Button
android:id="@+id/Translate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate" />
<Button
android:id="@+id/Complex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Complex" />
</LinearLayout>

资源anim文件alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1" android:toAlpha="0.3" android:duration="2000" android:repeatCount="3">
</alpha>

Android中TweenAnimation四种动画切换效果的更多相关文章

  1. Android中的四种动画(一)

    TweenAnimation 变换动画(也叫作"补间动画"),有四种(alpha scale translate rote). FrameAnimation(也叫DrawableA ...

  2. Android中Button四种点击事件实现方式

    1.Xml添加监听属性,这里添加的doClick. <Button android:id="@+id/bt1" android:layout_width="wrap ...

  3. Activity中的四种启动模式

    在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作.在Android中Activity的启动模式决定了Activity的启动运行方式. An ...

  4. Android中使用ImageViewSwitcher实现图片切换轮播导航效果

    前面写过了使用ViewFlipper和ViewPager实现屏幕中视图切换的效果(ViewPager未实现轮播)附链接: Android中使用ViewFlipper实现屏幕切换 Android中使用V ...

  5. Android平台中的三种翻页效果机器实现原理

    本文给开发者集中展现了Android平台中的三种翻页效果机器实现原理,希望能够对开发者有实际的帮助价值! 第一种翻页效果如下:     实现原理: 当前手指触摸点为a,则 a点坐标为(ax,ay), ...

  6. Android中的Drawable和动画

    Android中Drawable是一种可以在Canvas上进行绘制抽象的概念,种类很多,常见的颜色和图片都可以是一个Drawable.Drawable有很多种,它们表示一种图像的概念,但是它们又不全是 ...

  7. Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式。

    原文:Android Activity 的四种启动模式 lunchMode 和 Intent.setFlags();singleTask的两种启动方式. Android Activity 的四种启动模 ...

  8. Android - TabHost 与 Fragment 制作页面切换效果

    Android - TabHost 与 Fragment 制作页面切换效果 Android API 19 , API 23 三个标签页置于顶端 效果图: 在文件BoardTabHost.java中定义 ...

  9. 使用AnimateWindow来实现窗口淡入淡出(主要有四种动画,滚动,滑动,折叠或展开,和淡入淡出)

    如果是在VC6下进行编译,应引入下面的预编译宏,注意放在windows.h的前面#undef  WINVER   #define  WINVER   0x500为什么要引入上面的宏呢?看看winuse ...

随机推荐

  1. poj 2184 Cow Exhibition(背包变形)

    这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...

  2. Android菜鸟的成长笔记(4)——你真的理解了吗?

    原文:Android菜鸟的成长笔记(4)--你真的理解了吗? 在上一篇中我们查看了QQ的apk源文件中的布局结构,并仿照QQ完成了我们第一个应用的界面,详细请看<Android菜鸟的成长笔记&g ...

  3. Android和java平台 DES加密解密互通程序及其不能互通的原因

    网上的demo一搜一大堆,但是,基本上都是一知半解(包括我).为什么呢?我在尝试分别在两个平台加密的时候,竟然发现Android DES 加密和java DES加密的程序不能互通.就是加密的结果不一样 ...

  4. javascript (二) 事件

    <script></script> 函数写法: function  fun_name(){ x=docment.getElementById("demo") ...

  5. android studio下的NDK开发详解(一)

    源地址:http://www.voidcn.com/blog/chengkaizone/article/p-5761016.html 好记性不如烂笔头,开始坚持写博客,学一点记一点,只为了生活更好. ...

  6. TCP连接的建立(二)

    被动打开 SYN cookies TCP协议开辟了一个比較大的内存空间请求连接队列来存储连接请求块,当SYN请求不断添加,请求连接数目到达上限时,会致使系统丢弃SYN连接请求.SYN cookies技 ...

  7. 降低http请求次数

    80%的终于用户响应时间花在前端程序上.而其大部分时间则花在各种页面元素,如图像.样式表.脚本和Flash等的下载上. 降低页面元素将会降低HTTP请求次数.这是高速显示页面的关键所在. 1.Imag ...

  8. [c++语法]类

    什么是类 类 是 面向对象的基础.c里面是没有对象的,只有数据,即静态的死物. 从面向过程升级到面向对象后,有了对象的概念,对象是数据与方法的合体,是动态的活物. 类代表着一类事物的特征.而对象,是类 ...

  9. 百度地图之UI控制

    在本文中主要介绍百度地图UI控制功能,即控制地图是否有缩放.平移.双击放大.旋转.俯视的功能以及控制是否显示内置缩放组件.指南针位置等.在文中采用标签监听使每个控制功能的方法见名知义,代码原型来源百度 ...

  10. 18.如何自我Struts2它Struts2标签和综合汇总文章有点早

    18.如何自我Struts2它Struts2标签和综合汇总文章有点早[视频] 之前写了一篇"打算做一个视频教程探讨怎样自学计算机相关的技术",优酷上传不了.仅仅好传到百度云上: h ...