Animation
AnimationActivity
package org.wp.activity;
import android.app.Activity;
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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class AnimationActivity extends Activity {
private ImageView imageView;
private Button translateButton;
private Button alphaButton;
private Button scaleButton;
private Button rotateButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) this.findViewById(R.id.imageViewId);
translateButton = (Button) this.findViewById(R.id.translateButtonId);
alphaButton = (Button) this.findViewById(R.id.alphaButtonId);
scaleButton = (Button) this.findViewById(R.id.scaleButtonId);
rotateButton = (Button) this.findViewById(R.id.rotateButtonId);
/**
* 移动效果
* fromXType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* fromXValue x轴开始位置
*
* toXType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* toXValue x轴结束位置
*
* fromYType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* fromYValue y轴开始位置
*
* toYType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* toYValue y轴结束位置
*/
translateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f);
// 执行时间
translateAnimation.setDuration(1000);
// 1秒以后开始执行
animationSet.setStartOffset(1000);
// 动画结束时画面停留在最后一帧
animationSet.setFillAfter(true);
// 动画结束时画面停留在第一帧
animationSet.setFillBefore(false);
animationSet.addAnimation(translateAnimation);
imageView.startAnimation(animationSet);
}
});
/**
* 渐变效果
* 1.0完全不透明/0.0完全透明
* fromAlpha 开始的透明度
* toAlpha 结束的透明度
*/
alphaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
alphaAnimation.setDuration(1000);
animationSet.addAnimation(alphaAnimation);
imageView.startAnimation(animationSet);
}
});
/**
* 缩放效果
* fromX 缩放开始宽度
* toX 缩放结束宽度
* fromY 缩放开始高度
* toY 缩放结束高度
*
* pivotXType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* pivotXValue 该对象缩放的X坐标,其中0是左边缘(这点是固定的,而对象更改大小)
*
* pivotYType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* pivotYValue 该对象缩放的y坐标,其中0是上边缘(这点是固定的,而对象更改大小)
*/
scaleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.5f,
1.0f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
}
});
/**
* 旋转效果
* fromDegrees 旋转开始位置
* toDegrees 旋转结束位置
*
* pivotXType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* pivotXValue 旋转对象的x坐标,0是左边缘
*
* pivotYType Animation.ABSOLUTE/Animation.RELATIVE_TO_SELF/Animation.RELATIVE_TO_PARENT
* pivotYValue 旋转对象的y坐标,0是上边缘
*
*/
rotateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0,
Animation.RELATIVE_TO_PARENT, 0);
rotateAnimation.setDuration(5000);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
}
});
}
}
main.xml
<?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" > <Button android:id="@+id/rotateButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="rotate动画效果" /> <Button android:id="@+id/scaleButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/rotateButtonId" android:text="scale动画效果" /> <Button android:id="@+id/alphaButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/scaleButtonId" android:text="alpha动画效果" /> <Button android:id="@+id/translateButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/alphaButtonId" android:text="translate动画效果" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@id/translateButtonId" > <ImageView android:id="@+id/imageViewId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/icon" /> </RelativeLayout> </RelativeLayout>