在代码中可以通过set来设置多个动画属性,这里分开来设置不同的属性。

首先先贴上布局文件,里面的imageview是用来做动画的控件

<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="${relativePackage}.${activityClass}" > <LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:orientation="vertical" > <Button
android:id="@+id/alpha_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="透明度改变" /> <Button
android:id="@+id/rotate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="旋转动画" /> <Button
android:id="@+id/scale_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="缩放动画" /> <Button
android:id="@+id/translate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonListener"
android:text="移动动画" />
</LinearLayout> <ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:src="@drawable/ic_launcher" /> </RelativeLayout>

在activity中的listener中写上不同的动画效果

主要是

 AnimationSet set = new AnimationSet(true);
 set.addAnimation(alpha);
package com.kale.anim;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
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.ImageView; public class MainActivity extends Activity { ImageView iV; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iV = (ImageView)findViewById(R.id.imageView);
} public void buttonListener(View v) {
AnimationSet set = new AnimationSet(true);
switch (v.getId()) {
case R.id.alpha_button:
//设置渐变从不透明->透明,1表示不透明,0表示透明
AlphaAnimation alpha = new AlphaAnimation(1f, 0f);
//设置执行的时间
alpha.setDuration(1000);
set.addAnimation(alpha);
break;
case R.id.rotate_button:
//RotateAnimation rotate = new RotateAnimation(0, 180, 0, 0);//从0度旋转到180度,以左上角(0,0)为圆心
//从相对于自身(圆心在图片的中心)旋转360度,
//x轴坐标相对于父控件宽的一半,y轴相对于自身高的一半,于是确定一个圆心
RotateAnimation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f, //0.5 = 1/2的自己父控件的长度
Animation.RELATIVE_TO_SELF, 0.5f);//0.5 = 1/2的自己的长度
rotate.setDuration(5000);
set.addAnimation(rotate);
break;
case R.id.scale_button:
//缩放动画,x坐标从1f->2f,y坐标从1f->2f。缩放的轴是相对于自己的一半,等于是自己的中心
ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(1000);
set.addAnimation(scale);
break;
case R.id.translate_button:
//移动动画.x:从相对于自己x轴为0的位置移动到相对于自己x轴为1的位置。等于自己向右边移动一个身位
//y:从相对于自己y轴为0的位置移动到相对于自己y轴为1的位置。等于自己向下移动了两个身位
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 2f);
translate.setDuration(1000);
set.addAnimation(translate);
break;
default:
break;
}
//设置开始动画
iV.startAnimation(set);
}
}

补充:

        set.setStartOffset(1000);//一秒后再执行动画 = 等待1秒后执行动画
set.setFillAfter(true);//设置动画执行后保持最后状态
set.setFillBefore(false);//设置动画执行后不回到原来状态
set.setRepeatCount(3);//设置动画重复执行的次数

最新文章

  1. android 学习资源总结
  2. codeforces 556C. Case of Matryoshkas 解题报告
  3. mono 3.4.0 make install的时候出现&quot;找不到 Microsoft.Portable.Common.targets 文件”的错误提示解决方法
  4. JSP-03-实现数据传递
  5. [Java Web – Maven – 1A]maven 3.3.3 for windows 配置(转)
  6. Windows下环境变量配置
  7. mysql空间数据相关操作
  8. linux nc命令
  9. dubbo+zookeeper+spring+springMVC+mybatis的使用
  10. .NET Core 使用RabbitMQ
  11. Linux 高性能服务器编程——TCP/IP协议族
  12. idea在Terminal中使用maven指令
  13. Python系列之 - 前端总结
  14. mysql从入门到精通
  15. Http User Agent Example
  16. 流媒体压力测试rtmp&amp;hls(含推流和拉流)
  17. mysql 开发进阶篇系列 27 数据库字符集设置
  18. C# 实现Bezier曲线(vs2008)
  19. HTML5 Canvas ( 线段端点的样式 ) lineCap
  20. STL - 容器 - Map(一)

热门文章

  1. AWS 使用经验
  2. IScroll5安卓重复点击兼容问题处理
  3. kgtemp文件转mp3工具
  4. P1091 合唱队形 DP 最长升序列维护
  5. 001.iSCSI简介
  6. AP、路由、中继、桥接、客户端模式之间的区别
  7. faker php测试数据库生成
  8. BZOJ 2821作诗(Poetize) 分块
  9. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)
  10. 历数PC发展史上的祖先们