Animation
AnimationActivity
package org.wp.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
/**
* 在res文件夹下新建一个名为anim的文件夹
*
* @author wp
*
*/
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);
// 移动效果
translateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
AnimationActivity.this, R.anim.translate);
imageView.startAnimation(animation);
}
});
// 渐变效果
alphaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
AnimationActivity.this, R.anim.alpha);
imageView.startAnimation(animation);
}
});
// 缩放效果
scaleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
AnimationActivity.this, R.anim.scale);
imageView.startAnimation(animation);
}
});
// 旋转效果
rotateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(
AnimationActivity.this, R.anim.rotate);
imageView.startAnimation(animation);
}
});
}
}
anim
alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- fromAlpha 开始的透明度 toAlpha 结束的透明度 duration 执行时间 startOffset 1秒后开始执行 --> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="3000" android:startOffset="1000" /> </set>
rotate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- fromDegrees 开始角度 toDegrees 结束角度 pivotX pivotY 50 绝对定位 Animation.ABSOLUTE 50% 相对于控件本身定位 Animation.RELATIVE_TO_SELF 50%p 相对于父控件定位 Animation.RELATIVE_TO_PARENT duration 执行时间 --> <rotate android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%p" android:pivotY="50%p" android:duration="3000" /> </set>
scale.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- fromXScale 缩放开始宽度 toXScale 缩放结束宽度 fromYScale 缩放开始高度 toYScale 缩放结束高度 pivotX pivotY 50 绝对定位 Animation.ABSOLUTE 50% 相对于控件本身定位 Animation.RELATIVE_TO_SELF 50%p 相对于父控件定位 Animation.RELATIVE_TO_PARENT duration 执行时间 --> <scale android:fromXScale="1.0" android:toXScale="0.5" android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%p" android:pivotY="50%p" android:duration="3000" /> </set>
translate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- fromXDelta x轴开始位置 toXDelta x轴结束位置 fromYDelta y轴开始位置 toYDelta y轴结束位置 duration 执行时间 --> <translate android:fromXDelta="50%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="100%" android:duration="3000" /> </set>
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>