自定义动画的使用,主要介绍功能如下
首先是程序的入口
package com.example.myanimation;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private Button btn_animation, btn_rotatingAnimation, btnmobileAnimation,
btn_zoomAnimation, btn_hybridAnimation,btn_customAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_animation = (Button) findViewById(R.id.btn_animation);
btn_rotatingAnimation = (Button) findViewById(R.id.btn_rotatingAnimation);
btnmobileAnimation = (Button) findViewById(R.id.btnmobileAnimation);
btn_zoomAnimation = (Button) findViewById(R.id.btn_zoomAnimation);
btn_hybridAnimation=(Button) findViewById(R.id.btn_hybridAnimation);
btn_customAnimation=(Button) findViewById(R.id.btn_customAnimation);
btn_animation.setOnClickListener(this);
btn_rotatingAnimation.setOnClickListener(this);
btnmobileAnimation.setOnClickListener(this);
btn_zoomAnimation.setOnClickListener(this);
btn_hybridAnimation.setOnClickListener(this);
btn_customAnimation.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_animation:
// 下面透明动画中 0是为动画起始时透明度; 1是动画结束时透明度 1000是动画持续时间
// //第一种方法
// AlphaAnimation mAmimation=new AlphaAnimation(0, 1);
// mAmimation.setDuration(1000);
// v.startAnimation(mAmimation);
// 第二种方法
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.animation));
break;
case R.id.btn_rotatingAnimation:
// 第一个方法
// 旋转动画 起点 旋转角度 旋转中心点 像素
// RotateAnimation mRotateAnimation=new RotateAnimation(0,
// 360,100,50);
// 根据控件自身中心点旋转 起点 旋转角度 自身中心取X轴 取值0.5f 自身中心取Y轴 取值0.5f
// RotateAnimation mRotateAnimation = new RotateAnimation(0, 360,
// Animation.RELATIVE_TO_SELF, 0.5f,
// Animation.RELATIVE_TO_SELF, 0.5f);
// mRotateAnimation.setDuration(1000);
// v.startAnimation(mRotateAnimation);
// 第二个方法
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.rotate));
break;
case R.id.btnmobileAnimation:
// 第一种方法
// 移动动画 0 200 是在原地移动的0 200
// TranslateAnimation mTranslateAnimation=new TranslateAnimation(0,
// 200, 0, 200);
// mTranslateAnimation.setDuration(1000);
// v.startAnimation(mTranslateAnimation);
// 第二种方法
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.translate));
break;
case R.id.btn_zoomAnimation:
// 第一个方法
// ScaleAnimation mScaleAnimation=new ScaleAnimation(0, 1, 0, 1);//1
// ScaleAnimation mScaleAnimation=new ScaleAnimation(0, 1, 0,
// 1,100,50);//2
// ScaleAnimation mScaleAnimation=new ScaleAnimation(0, 1, 0, 1,
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
// 0.5f);
// mScaleAnimation.setDuration(1000);
// v.startAnimation(mScaleAnimation);
// 第二个方法
v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,
R.anim.scale));
break;
case R.id.btn_hybridAnimation:
//这是混合动画一起使用
//方式一 、java实现的代码
// AnimationSet mAnimation =new AnimationSet(true);//动画补间
// mAnimation.setDuration(1000);
//
// AlphaAnimation mAlphaAnimation=new AlphaAnimation(0, 1);
// mAlphaAnimation.setDuration(1000);
// mAnimation.addAnimation(mAlphaAnimation);
//
// TranslateAnimation mTranslateAnimation=new TranslateAnimation(200, 0, 200, 0);
// mTranslateAnimation.setDuration(1000);
// mAnimation.addAnimation(mTranslateAnimation);
//
// v.startAnimation(mAnimation);
//第二种方法
Animation at=AnimationUtils.loadAnimation(MainActivity.this, R.anim.hybrid);
v.startAnimation(at);
//动画侦听
at.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//animation start
}
@Override
public void onAnimationRepeat(Animation animation) {
//animation repeat
}
@Override
public void onAnimationEnd(Animation animation) {
// animation end
}
});
break;
case R.id.btn_customAnimation:
//自定义动画
CustomAnimation mCustomAnimation=new CustomAnimation();
mCustomAnimation.setDuration(1000);
v.startAnimation(mCustomAnimation);
break;
default:
break;
}
}
}
自定义动画的实现如下
package com.example.myanimation;
/**
* 自定义动画
*/
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class CustomAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.d("animation", interpolatedTime+"");//日志打印输出了动画执行是从0~1之间。0为开始,1结束动画
// t.setAlpha(interpolatedTime);//透明动画
// t.getMatrix().setTranslate(200*interpolatedTime, 200*interpolatedTime);//设置移动动画
t.getMatrix().setTranslate((float) (Math.sin(20*interpolatedTime)*50),0);//2个数值的数学计算给值控制动画的播放
super.applyTransformation(interpolatedTime, t);
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
//程序先执行这个方法
Log.d("animation","init");
super.initialize(width, height, parentWidth, parentHeight);
}
}
简单程序的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_animation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="透明动画" />
<Button
android:id="@+id/btn_rotatingAnimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="旋转动画" />
<Button
android:id="@+id/btnmobileAnimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="移动动画" />
<Button
android:id="@+id/btn_zoomAnimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="缩放动画" />
<Button
android:id="@+id/btn_hybridAnimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="混合动画" />
<Button
android:id="@+id/btn_customAnimation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="自定义动画" />
</LinearLayout>
动画在anim中xml这样写
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration="1000">
<!-- 上面第一个是为动画起始时透明度; 第二个是动画结束时透明度 第三个是动画持续时间 -->
</alpha>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:shareInterpolator="true" >
<!-- 这是混合动画 上面是添加补间 -->
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1" >
</alpha>
<translate
android:fromXDelta="200"
android:toXDelta="0"
android:fromYDelta="200"
android:toYDelta="200"
android:duration="1000">
</translate>
</set>
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
<!-- 这是旋转动画 -->
</rotate>
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="0"
xmlns:android="http://schemas.android.com/apk/res/android"
android:toXScale="1"
android:fromYScale="0"
android:toYScale="1"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%">
<!-- 这是缩放 -->
</scale>
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="200"
android:fromYDelta="0"
android:toYDelta="200"
android:duration="1000">
<!-- 这是移动动画 -->
</translate>