动画分为视图动画(view animation)和属性动画(property animation),视图动画又分为帧动画和补间动画

视图动画控件(iv)点击事件(OnClickListener接口)触发位置在原位置

1.帧动画(Frame animation)

res/drawable/xxx.xml

    //    false

android:duration="200"/>    //    显示时间

...    //    按先后顺序写

iv.setBackgroundResource(R.drawable.xxx);

((AnimationDrawable)iv.getBackground()).start();    //    View类的start()

2.补间动画(Tween animation)

分为平移、缩放、透明、旋转和混合

res/anim/xxx.xml

a.平移(TranslateAnimation)

android:fromXDelta="0"

android:fromYDelta="0"    //图片起始位置坐标(00为左上角)

android:toXDelta="500"

android:toYDelta="500"

android:duration="2000"    //    5个必要属性

android:fillAfter="true"       //    保持动画最后那个状态

android:repeatCount="1"   //    执行两次,"infinite":永久

android:repeatMode="restart"/>    //    "reverse"    两种重复模式

pubic void translate(View view) {

iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

a.2    代码构造平移动画

public void translate(View view){

TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);    //    float

//    fromXDelta = toXDelta = fromYDelta = 0;toYDelta = -500;    //    移动Y

animation.setDuration(2000);

animation.setFillAfter(true);

animation.setRepeatCount(1);    //    Animation.INFINITE

animation.setRepeatMode(Animation.RESTART);

iv.startAnimation(animation);

animation = new TranslateAnimation(

fromXType, fromXValue,    //    Animation.RELATIVE_TO_SELF, 0

toXType, toXValue,            //    Animation.RELATIVE_TO_PARENT, 0

fromYType, fromYValue,    //    Animation.RELATIVE_TO_SELF, 0

toYType, toYValue,            //    Animation.RELATIVE_TO_PARENT, 倍数

);

注意第四排参数,结束Y位置 = 原来动画Y + toYValue*布局的高度

改第四排参数为://    Animation.RELATIVE_TO_SELF, 倍数    后,结束Y位置 = 原来动画Y + toYValue*控件的高度/布局的高度

}

b.缩放(ScalaAnimation)

android:fromXScale="0"        //    "1"

android:toXScale="1"            //    "2"

android:fromYScale="0"        //    "1"

android:toYScale="1"            //    "2"

android:duration="2000"      //    5个必要属性

android:povotX="50%"

android:povotY="50%"         //缩放中心(默认为控件左上角),百分数:不能为小数,为控件宽/高的百分比

android:fillAfter="true"       //    保持动画最后那个状态

android:repeatCount="1"   //    执行两次,"infinite":永久

android:repeatMode="restart"/>    //    "reverse"    两种重复模式

public void scale(View view) {

iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

b.2 代码实现缩放动画

public void scale(View view) {

每个动画都有的属性:

1.构造方法     ScalAnimation animation = new ScalAnimation(fromX, toX, fromY, toY);    //    1, 0, 1, 0

2.时间:        animation.setDuration(2000);

3.是否停留    //    345可省略

4.重复次数

5.重复模式

6.让控件展示动画        img.setAnimation(animation);

animation = new ScalAnimation(1, 0, 1, 0, pivotX, pivotY);    //    pivotX = iv.getWidth()/2;    pivotY = iv.getHeight()/2;    //    iv表示控件

animation = new ScalAnimation(1, 0, 1, 0,

pivotXType, pivotX,            //    Animation.RELATIVE_TO_SELF, 0.5f

pivotYType, pivotY);           //    Animation.RELATIVE_TO_PARENT, 0.5f

}

c.透明(AlphaAnimation)

android:fromAlpha="1"    //    完全不透明(原图),[0,1]的小数,不能是百分数

android:toAlpha="0"        //    完全透明(消失)

android:duration="2000"/>

public void alpha(View view) {

iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

c.2 代码实现透明动画

public void alpha(View view) {

AlphaAnimation animation = new AlphaAnimation(1,0.2f);

animation.setDuration(2000);

animation.setFillAfter(true);

iv.startAnimation(animation);

}

d.旋转(RotateAnimation)

fromDegress="0"

toDegress="360"

pivotX="50%"    //    100%

pivotY="50%"    //    100%

duration="2000"/>

public void rotate(View view) {

iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

d.2 代码实现旋转动画

public void rotate(View view) {

RotateAnimation animation = new RotateAnimation(0,360);

animation = new RotateAnimation(0, -360, iv.getWidth()/2, iv.getHeight()/2);

animation = new RotateAnimation(0, 720,

Animation.RELATIVE_TO_PARENT, 0.5f,

Animation.RELATIVE_TO_PARENT, 0.5f);    //    ps:    不是屏幕中心

animation.setDuration(2000);

iv.startAnimation(animation);

}

e.混合(AnimationSet)

        //    动画集合

...

public void mix(View view) {

iv.startAnimation(AnimationUtils.loadAnimation(this, R.anim.xxx));

}

e.2 代码实现动画集合

AnimationSet set = new AnimationSet(false);        //    为true时set可设置所有类型动画的属性

set.addAnimation(translateAnimation);

...

iv.startAnimation(set);

2.2    动画监听器

animation.setAnimationListener(new AnimationListener() {

public void onAnimationStart(Animation animation) {}

public void onAnimationRepeat(Animation animation) {}

public void onAnimationEnd(Animation animation) {}

})

3.属性动画

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐