1 补间动画

在assets/anim下新建补间动画的xml文件tween_ani.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="6000" />
<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="0.0"
android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%"
android:duration="9000" />
<translate android:fromXDelta="30" android:toXDelta="0"
android:fromYDelta="30" android:toYDelta="0" android:duration="10000" />
<rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%"
android:pivotY="50%" android:duration="10000" />
</set>

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView android:id="@+id/imageview" android:src="@drawable/img01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
<Button android:id="@+id/button" android:text="补间动画"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>

在程序中加载动画并传给ImageView启动动画:

button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView iv=(ImageView) findViewById(R.id.imageview);
Animation animation=AnimationUtils.loadAnimation(mainActivity.this, R.anim.tween_ani);
iv.startAnimation(animation);
}});

2 帧动画

在res/anim下新建帧动画xml文件frame_ani.xml,其中包括3个图像帧

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/img01" android:duration="500"
android:visible="true" />
<item android:drawable="@drawable/img02" android:duration="500"
android:visible="true" />
<item android:drawable="@drawable/img03" android:duration="500"
android:visible="true" />
</animation-list>

在主函数中调用帧动画启动,注意AnimationDrawable对象的start方法不可以在Activity的onCreate方法中调用,因此如果需要程序在一开始就播放动画,则应该在onWindowFocusChanged()方法中调用。

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView iv=(ImageView) findViewById(R.id.imageview);
iv.setBackgroundResource(R.anim.frame_ani);
AnimationDrawable ad=(AnimationDrawable) iv.getBackground();
ad.start();
}});
}

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android settings命令
  5. Android(安卓)Studio下“Error:Could not find com.android.tool
  6. Android中的AnimationSet使用
  7. Android隐藏标题栏,全屏显示
  8. android中判断网络是否连接
  9. Android获取系统时间的多种方法

随机推荐

  1. Android上传文件,客户端+服务器源码
  2. Android(安卓)Studio 3.6.3 中遇到无法创
  3. Android(安卓)ADT 插件安装
  4. android不能显示log的问题
  5. Android软键盘弹出不影响布局的方法
  6. Android代碼執行shell 命令
  7. Android利用tcpdump抓包
  8. 使用 Android(安卓)和 XML 构建动态用户
  9. ADT20新建项目Android(安卓)Support libr
  10. 分享到某个地方