关闭

android动画之补间动画

277人阅读 评论(0) 收藏 举报
补间动画(移动补间动画,缩放补间动画,旋转补间动画,透明补间动画):


1.移动补间动画(TranslateAnimation):
指定移动的绝对位置(以自己的左上角为参照点):
TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, 0, 500);
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
2.指定移动的相对位置(相对父控件):
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
0.5f);
translateAnimation.setFillAfter(true);//停留在动画最后一帧,不指定的话,动画播放完毕会回到初始状态
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);


3.指定移动的相对位置(相对自己)
      
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
0.0f,
Animation.RELATIVE_TO_SELF,
4.0f);
translateAnimation.setFillAfter(true);
translateAnimation.setDuration(5000);
imageview.startAnimation(translateAnimation);
         


4.在xml中定义动画:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXDelta="0" 
    android:toXDelta="0"
    android:fromYDelta="0"
    android:toYDelta="200"
    android:fillAfter="true">


</translate>


<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXDelta="0%" 
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="200%"
    android:fillAfter="true">


</translate>




<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXDelta=“0%p” 
    android:toXDelta=“0%p”
    android:fromYDelta=“0%p”
    android:toYDelta="200%p”
    android:fillAfter="true">


</translate>




TranslateAnimation translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(this, R.anim.anim_translate);
imageview.startAnimation(translateAnimation);


2.缩放补间动画:
以自己的左上角为圆心缩放:
ScaleAnimation scaleAnimation = new ScaleAnimation(

1.0f,

1.5f, 

1.0f,

1.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);


xml:


<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:toXScale="1.5"
    android:fromYScale="1.0"
    android:toYScale="1.5"
 >


</scale>


以自己的中心点为圆心进行缩放:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);


xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:toXScale="1.5"
    android:fromYScale="1.0"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%" >


</scale>




以父控件为参照:
ScaleAnimation scaleAnimation = new ScaleAnimation(
1.0f,
1.5f,
1.0f,
1.5f,
Animation.RELATIVE_TO_PARENT,
0.5f,
Animation.RELATIVE_TO_PARENT,
0.5f);
scaleAnimation.setFillAfter(true);
scaleAnimation.setDuration(5000);
imageview.startAnimation(scaleAnimation);


xml:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.0"
    android:toXScale="1.5"
    android:fromYScale="1.0"
    android:toYScale="1.5"
    android:pivotX="50%p"
    android:pivotY="50%p" >


</scale>


如果imageview在父控件中间,上面两种设置的效果并不一样,后面这个中心点并不在父控件中心,它是以imageview的左上角为参照,偏移父控件宽高的一半,这时候,动画的中心点并不在父控件的中心



3.旋转补间动画
以左上角为原点旋转:
RotateAnimation rotateAnimation = new RotateAnimation(
0,
180);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);
以自己的中心为原点旋转:
RotateAnimation rotateAnimation = 
new RotateAnimation(
0,
180,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(5000);
imageview.startAnimation(rotateAnimation);




4.透明补间动画:


Animation animation = new AlphaAnimation(1f,0.1f);
animation.setFillAfter(true);
animation.setDuration(5000);
imageview.startAnimation(animation);








5.多个动画同时进行:
AnimationSet set = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(1f,0.1f);
alphaAnimation.setDuration(5000);
set.addAnimation(alphaAnimation);

TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,300);
translateAnimation.setDuration(5000);
set.addAnimation(translateAnimation);

set.setFillAfter(true);

imageview.startAnimation(set);


xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >


    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5"
        android:duration="5000" >
    </scale>


    <translate
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="5000"
        android:fromXDelta="100"
        android:fromYDelta="0"
        android:toXDelta="100"
        android:toYDelta="300" >
    </translate>


</set>




AnimationSet animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.anim_set);
animation.setFillAfter(true);
imageview.startAnimation(animation);
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:863780次
    • 积分:8300
    • 等级:
    • 排名:第2490名
    • 原创:60篇
    • 转载:210篇
    • 译文:0篇
    • 评论:75条
    文章分类
    最新评论