准备一张名为picture的图片。
在main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="@+id/mlyw"
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/picture"/>
<Button
android:id="@+id/but"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="开始移动缩放"/>
</LinearLayout>
在MyAnimationDemo.java中:
package com.li.animation;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
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.Button;
import android.widget.ImageView;
import android.support.v4.app.NavUtils;
@SuppressLint("ParserError")
public class MyAnimationDemo extends Activity {
private ImageView mlyw = null;
private Button but = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mlyw = (ImageView)super.findViewById(R.id.mlyw);
this.but = (Button)super.findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
AnimationSet set = new AnimationSet(true);
TranslateAnimation tran = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0.0f , // X轴开始位置
Animation.RELATIVE_TO_SELF,0.5f , // X轴移动的结束位置
Animation.RELATIVE_TO_SELF,0.0f , // Y轴开始位置
Animation.RELATIVE_TO_SELF,1.5f ); // Y轴移动位置
ScaleAnimation scale = new ScaleAnimation(1, 0.0f, 1, 0.0f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setRepeatCount(2) ; //重复两次
set.addAnimation(tran); // 增加动画
set.addAnimation(scale); // 增加动画
set.setInterpolator(new AccelerateInterpolator()); //加速
set.setDuration(5000); // 5秒完成动画
MyAnimationDemo.this.mlyw.startAnimation(set); // 启动动画
}
}
}