0
点赞
收藏
分享

微信扫一扫

android的基础Animation的实现


在android中,使用动画可以显示在ImageView上,设置并显示动画的代码如下:

img.startAnimation(AnimationUtils.loadAnimation(this, R.anim.anim_alpha));

除了显示在ImageView上,android的动画还可以用在Activity,ListView中,下面介绍一下他们的用法:

1.在ImageView上显示Animation的几种动画如下:

<--透明度动画-->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="3000"/>
</set>

<--大小变化动画-->

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="3000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"/>

</set>


<--图像旋转动画-->

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="2000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360"/>

</set>


<--图像位移动画-->

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromXDelta="10"
android:fromYDelta="10"
android:toXDelta="100"
android:toYDelta="100"/>

</set>


2.如果想要在切换Activity时显示动画,那么需要这样实现:

首先创建两个xml动画文件,分别表示前一个activity退出和后一个activity进入时播放的动画,这里可以利用上面的方法组合设置:


<--<span style="font-size:18px;">anim_activity_zoom_out.xml</span>-->

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.1"
android:toYScale="0.1"/>

<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0"/>

</set>

<--<span style="font-size:18px;">anim_activity_zoom_in.xml</span>-->

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<scale
android:duration="1000"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0"/>

<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1.0"/>

</set>


然后在调用startActivity之后设置动画,注意这里是在之后:

Intent intent1 = new Intent(getApplicationContext(), BactivityActivity.class);
startActivity(intent1);
/*
* Call immediately after one of the flavors of startActivity(Intent)
* or finish to specify an explicit transition animation to perform next.
*/
overridePendingTransition(R.anim.anim_activity_zoom_in, R.anim.anim_activity_zoom_out);

3.在ListView中设置动画,他的xml的anim实现和activity中的实现方式一样,代码中我们只需要如下设置即可:


LayoutAnimationController lac = new LayoutAnimationController(
AnimationUtils.loadAnimation(this, R.anim.anim_activity_zoom_in));
lac.setOrder(LayoutAnimationController.ORDER_RANDOM);
lv.setLayoutAnimation(lac);
lv.startLayoutAnimation();



举报

相关推荐

0 条评论