本博文主要讲述的是,Animations在android开发中的用来循环播放动画的效果:
MainActivity.java:
package com.example.animationtest3;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
/*
* Frame by Frame Animation
* */
public class MainActivity extends Activity {
private ImageView imageView = null;
private Button scaleButton = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView)findViewById(R.id.myImage);
scaleButton = (Button)findViewById(R.id.scaleButton);
scaleButton.setOnClickListener(new setScaleListener());
}
//动画缩放效果监听器
class setScaleListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//可以用在修改背景图片中
imageView.setBackgroundResource(R.drawable.img_list);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
主布局文件main.xml:
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/imgLayout"
android:layout_below="@id/myText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="55dp"
>
<ImageView
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<Button
android:id="@+id/scaleButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imgLayout"
android:text="启动动画"
/>
</RelativeLayout>
drawable文件夹下新加的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/list1" android:duration="500"/>
<item android:drawable="@drawable/list2" android:duration="500"/>
<item android:drawable="@drawable/list3" android:duration="500"/>
<item android:drawable="@drawable/list4" android:duration="500"/>
</animation-list>
原文地址:http://blog.csdn.net/ajhsdj/article/details/41946289