旋转,淡化,放大缩小,。。四种动画
下载这个图片改为压缩文件就可以看到源码。右键这个显示不出来的图片 复制图片地址 用工具下载之后,把后缀改为rar就是源码项目。
package zhang.Animation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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;
public class Animation_ extends Activity {
private Button rotate;
private Button scale;
private Button alpha;
private Button translate;
private ImageView imageView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rotate=(Button)findViewById(R.id.rotate);
scale=(Button)findViewById(R.id.scale);
alpha=(Button)findViewById(R.id.alpha);
translate=(Button)findViewById(R.id.translate);
imageView=(ImageView)findViewById(R.id.img);
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0,360,
Animation.RELATIVE_TO_PARENT,1f,
Animation.RELATIVE_TO_PARENT,0f
);
rotateAnimation.setDuration(1000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
});
alpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
});
scale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation =new ScaleAnimation(1,0.1f,1,0.1f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF,0.5f);
animationSet.addAnimation(scaleAnimation);
animationSet.setDuration(2000);
imageView.startAnimation(animationSet);
}
});
translate.setOnClickListener(new TranslateListener());
}
class TranslateListener implements OnClickListener{
public void onClick(View arg0) {
// TODO Auto-generated method stub
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation=new TranslateAnimation(
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,10f,
Animation.RELATIVE_TO_SELF,0f,
Animation.RELATIVE_TO_SELF,1.0f);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_centerInParent="true"
android:layout_marginTop="100dip"
/>
<Button android:text="Rotate" android:id="@+id/rotate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/scale"></Button>
<Button android:text="Scale" android:id="@+id/scale" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/alpha" ></Button>
<Button android:text="Alpha" android:id="@+id/alpha" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/translate"></Button>
<Button android:text="Translate" android:id="@+id/translate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true"></Button>
</RelativeLayout>