第一步创建布局文件:main.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="动画 测试"
/>
<ImageView
android:id="@+id/myimageview"
android:src="@drawable/me"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="60dp"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="平移动画效果"
/>
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="渐变动画效果"
/>
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="旋转动画效果"
/>
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onMyClick"
android:text="缩放动画效果"
/>
</LinearLayout>
第二步:java代码实现
package sun.zone.day29.animation.test;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
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.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.Transformation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class Day29AnimationsTestActivity extends Activity {
private ImageView imageview=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageview=(ImageView)findViewById(R.id.myimageview);
//缩放图片
Bitmap bitmap=BitmapFactory.decodeResource(this.getResources(), R.drawable.me);
int width=bitmap.getWidth();
int height=bitmap.getHeight();
Matrix matrix=new Matrix();
matrix.setScale(1*0.3f, 1*0.3f);
Bitmap bm=Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
imageview.setImageBitmap(bm);
}
//绑定监听
public void onMyClick(View v){
if(v.getId()==R.id.button1){
//定义一个平移动画对象
TranslateAnimation tla=new TranslateAnimation(0, 20, 0, 20);
//设置动画效果
tla.setDuration(2000);
tla.setRepeatCount(4);
imageview.setAnimation(tla);
//启动动画
tla.start();
}else if(v.getId()==R.id.button2){
//xml文件设置动画效果
//AnimationSet animationSet = new AnimationSet(true);
//Animation tranAnimation=AnimationUtils.loadAnimation(Day29AnimationsTestActivity.this, android.R.anim.fade_in);
//animationSet.addAnimation(tranAnimation);
//animationSet.start();
// imageview.setAnimation(tranAnimation);
// tranAnimation.start();
//定义一个渐变动画对象
AlphaAnimation alam=new AlphaAnimation(1, 0);
//设置动画
alam.setDuration(2000);
imageview.setAnimation(alam);
//设置并直接启动动画
//imageview.startAnimation(alam);
//启动动画
alam.start();
}else if(v.getId()==R.id.button3){
//定义一个旋转对象
RotateAnimation rotateAnimation=new RotateAnimation(30, 60);
rotateAnimation.setDuration(2000);
imageview.startAnimation(rotateAnimation);
}else if(v.getId()==R.id.button4){
//定义一个缩放动画
ScaleAnimation acaleAnimation=new ScaleAnimation(10, 20, 10, 20);
//设置动画
acaleAnimation.setDuration(1000);
imageview.setAnimation(acaleAnimation);
//启动动画
acaleAnimation.start();
}
}
}
第三步:演示效果