动画Animation
1.Animation有5个子类: AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 分别代表透明度动画,动画集合,旋转动画,缩放动画,平移动画
2.透明度动画AlphaAnimation:
//透明度显示,第一个参数是开始时的透明度,第二个参数是结束时的透明度,0代表完全透明
Animation animation=new AlphaAnimation(0.0f,1.0f);
animation.setDuration(3000);//设置播放时间
mImage.startAnimation(animation);//在Image上添加动画
3.平移动画TranslateAnimation
//平移,前两个参数是设置x轴从第一个参数位置平移到第二个参数位置,后两个参数设置y轴从第三个参数平移到第四个参数
TranslateAnimation translateAnimation=new TranslateAnimation(-mImage.getMeasuredWidth(),0,0,0);
translateAnimation.setDuration(3000);//设置播放时间
mImage.startAnimation(translateAnimation);//在Image上添加动画
4.旋转动画RotateAnimation
//旋转,第一个参数是设置起始角度,第二参数是旋转角度,第三个和第四个参数设置旋转中心
RotateAnimation rotateAnimation=new RotateAnimation(0,360,360,360);
rotateAnimation.setDuration(3000);
mImage.startAnimation(rotateAnimation);
5.缩放动画ScaleAnimation
//缩放,第一个和第二个参数表示沿x轴从第一个参数的比例缩放到第二个参数的比例,第三个和第四个是沿y轴进行缩放
ScaleAnimation scaleAnimation=new ScaleAnimation(1.0f,1.5f,1.0f,1.5f);
scaleAnimation.setDuration(3000);
mImage.startAnimation(scaleAnimation);
6.动画集合AnimationSet
AnimationSet animationSet=new AnimationSet(false);
Animation animation1=new AlphaAnimation(0.0f,1.0f);//透明度显示
animation1.setDuration(3000);
TranslateAnimation translateAnimation1=new TranslateAnimation(0,0,0,mImage.getMeasuredWidth());//平移
translateAnimation1.setDuration(3000);
RotateAnimation rotateAnimation1=new RotateAnimation(0,360,360,360);
rotateAnimation1.setDuration(3000);
ScaleAnimation scaleAnimation1=new ScaleAnimation(1.0f,0.5f,1.0f,0.5f);
scaleAnimation1.setDuration(3000);
animationSet.addAnimation(animation1);
animationSet.addAnimation(translateAnimation1);
animationSet.addAnimation(rotateAnimation1);
animationSet.addAnimation(scaleAnimation1);
mImage.startAnimation(animationSet);
利用xml来添加动画集合
1.在res文件夹下创建文件命名为anim,选择文件类型为animation
2.在anim文件下创建一个xml文档在里面可以添加动画集合
3.android:interpolator=”@android:anim/cycle_interpolator”表示Android自带的播放动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/cycle_interpolator">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="360"
android:pivotY="360"
android:repeatCount="2"
android:toDegrees="360"></rotate>
<scale
android:startOffset="3000"
android:duration="3000"
android:fromXScale="1.0"
android:toXScale="0.5"
android:fromYScale="1.0"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
></scale>
</set>
4.调用方法
Animation animaitonxml= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.animi_rotate);
mImage.startAnimation(animaitonxml);
Animator
Animator通常用来改变动画的某个属性,但是不一定只用于动画,还可以用到其他的地方##
1.在res文件夹下创建animator文件,选择文件类型为animator
2.创建一个xml文档
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="2000"
android:repeatCount="0"
android:propertyName="scaleX"
android:valueFrom="0.5"
android:valueTo="1"
>
</objectAnimator>
<objectAnimator
android:duration="2000"
android:repeatCount="0"
android:propertyName="scaleY"
android:valueFrom="0.5"
android:valueTo="1"
>
</objectAnimator>
</set>
3.调用
Animator animator=AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator_translate);
animator.setTarget(mImage);
animator.start();
LayoutTransition
1.用来给布局添加一个View时添加的动画
2.在布局中创建一个LinearLayout,然后在该LinearLayout中添加button
count++;
final Button button=new Button(getApplicationContext());
ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setText("按钮"+count);
button.setScaleX(0f);
button.setScaleY(0f);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLinear.removeView(button);
}
});
mLinear.addView(button);
3.添加button和删去button时添加的动画
LayoutTransition transition=new LayoutTransition();
transition.getDuration(2000);
transition.setAnimator(LayoutTransition.APPEARING,AnimatorInflater.loadAnimator(getApplicationContext(),R.animator.animator_translate));
transition.setAnimator(LayoutTransition.CHANGE_APPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
transition.setAnimator(LayoutTransition.CHANGING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
transition.setAnimator(LayoutTransition.DISAPPEARING,transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
mLinear.setLayoutTransition(transition);