【补间动画示例】Tweened Animation

代码中定义动画示例

public class MainActivity extends ListActivity {
    private ImageView iv;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] array = { "alpha""trans""scale""rotate""set",//
                "只要 fillAfter = true,不管其他怎么设置,都是使用最后一帧",//
                "只要 fillAfter = false,不管其他怎么设置,都是使用第一帧(没有进行任何缩放)"};
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1new ArrayList<String>(Arrays.asList(array))));
        iv = new ImageView(this);
        iv.setImageResource(R.drawable.icon);
        getListView().addHeaderView(iv);
    }
    @Override
    protected void onListItemClick(ListView l, View view, int position, long id) {
        switch (position) {
        case 0:
            Toast.makeText(this"我是图片", Toast.LENGTH_SHORT).show();
            break;
        case 1:
            alpha(iv);
            break;
        case 2:
            trans(iv);
            break;
        case 3:
            scale(iv);
            break;
        case 4:
            rotate(iv);
            break;
        case 5:
            set(iv);
            break;
        case 6:
            scale(ivnew Random().nextBoolean()new Random().nextBoolean()true);//最后一帧,即fillBefore的值无效,fillAfter的值有效
            break;
        case 7:
            scale(ivnew Random().nextBoolean()new Random().nextBoolean()false);//第一帧(没有进行任何缩放)
            break;
        }
    }
    //透明度动画
    public void alpha(View view) {
        AlphaAnimation aa = new AlphaAnimation(1.0f, 0.1f);//开始、结束时的透明度。1为全不透明,0为全透明
        aa.setDuration(2000);//播放时间
        aa.setRepeatCount(1);//重复次数,默认为0。【播放次数=重复次数+1】。设为Animation.INFINITE = -1 表示不停止的播放
        aa.setRepeatMode(Animation.RESTART);//【REVERSE】倒序重复播放,【RESTART】重新开始执行(默认)
        aa.setInterpolator(new AccelerateInterpolator());//加速
        view.startAnimation(aa);
    }
    //位移动画
    public void trans(View view) {
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0.5f,//
                Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_PARENT, 1f)//fromXType, fromXValue, toXType, toXValue【开始/结束】时【相对谁】的距离
        ta.setDuration(1000);
        ta.setRepeatCount(Animation.INFINITE);
        ta.setRepeatMode(Animation.REVERSE);
        ta.setInterpolator(new BounceInterpolator());//动画结束的时候弹起
        view.startAnimation(ta);
    }
    //缩放动画
    public void scale(View view) {
        ScaleAnimation sa = new ScaleAnimation(0.5f, 2.5f, 0.5f, 1.5f, //【开始/结束时x/y的缩放比例】
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)//【x/y缩放时所使用的模式和中心点】
        sa.setDuration(2000);
        sa.setInterpolator(new AccelerateDecelerateInterpolator());//先加速后减速
        view.startAnimation(sa);
    }
    //旋转动画
    public void rotate(View view) {
        RotateAnimation ra = new RotateAnimation(0, 360 * 5, //【开始/结束时旋转的角度】
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)//【x/y旋转时所使用的模式和中心点】
        ra.setDuration(1000);
        ra.setInterpolator(new LinearInterpolator());//匀速
        view.startAnimation(ra);
    }
    //组合动画
    public void set(View view) {
        AnimationSet set = new AnimationSet(true);//是否使用共同的插值器
        //位移
        TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, //
                Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
        ta.setDuration(500);
        //缩放
        ScaleAnimation sa = new ScaleAnimation(1f, 2f, 1f, 1.5f, Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.5f);
        sa.setDuration(500);
        sa.setStartOffset(1000);//延迟时间 When this Animation should start, in milliseconds from the start time of the root AnimationSet
        //旋转
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
        ra.setDuration(500);
        ra.setStartOffset(2000);
        //将上面这些动画放到集合中
        set.addAnimation(ta);
        set.addAnimation(sa);
        set.addAnimation(ra);
        //        set.setFillEnabled(true);
        set.setFillAfter(true);
        //        set.setFillBefore(false);
        view.startAnimation(set);
    }
    public void scale(View view, boolean fillEnable, boolean fillBefore, boolean fillAfter) {
        ScaleAnimation sa = new ScaleAnimation(0.2f, 3f, 0.2f, 3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        sa.setDuration(200);
        sa.setFillEnabled(fillEnable);
        sa.setFillBefore(fillBefore);
        sa.setFillAfter(fillAfter);
        view.startAnimation(sa);
    }
}

XML中定义动画示例

public class SecondActivity extends ListActivity {
    int[] array_id = { R.anim.alpha_in, R.anim.disappear_bottom_right_in
R.anim.disappear_bottom_right_out, R.anim.disappear_top_left_in,
            R.anim.disappear_top_left_out, R.anim.drawroll_ani_in
R.anim.drawroll_ani_out, R.anim.fade_in, R.anim.fade_out,
            R.anim.flip_horizontal_in, R.anim.flip_horizontal_out, R.anim.flip_vertical_in
R.anim.flip_vertical_out, R.anim.gallery_in, R.anim.grow_from_top,
            R.anim.left_in, R.anim.left_out, R.anim.mi_laucher_alpha
R.anim.mi_laucher_del_done R.anim. mi_laucher_del_down , R.anim. mi_laucher_out ,
            R.anim.mi_laucher_scale_in, R.anim.mi_laucher_scale_out, R.anim.pophidden_anim
R.anim.popshow_anim, R.anim.push_left_in, R.anim.push_left_out,
            R.anim.push_up_in, R.anim.push_up_out, R.anim.rbm_in_from_left
R.anim.rbm_out_to_left, R.anim.refreshable_list_rotate, R.anim.right_in,
            R.anim.right_out, R.anim.shrink_from_bottom, R.anim.slide_down_out
R.anim.slide_left, R.anim.slide_right, R.anim.slide_up_in, R.anim.small_2_big,
            R.anim.umeng_fb_slide_in_from_left, R.anim.umeng_fb_slide_in_from_right
R.anim.umeng_fb_slide_out_from_left, R.anim.umeng_fb_slide_out_from_right,
            R.anim.umeng_socialize_fade_in, R.anim.umeng_socialize_fade_out
R.anim.umeng_socialize_shareboard_animation_in,
            R.anim.umeng_socialize_shareboard_animation_out
R.anim.umeng_socialize_slide_in_from_bottom, R.anim.umeng_socialize_slide_out_from_bottom,
            R.anim.unzoom_in, R.anim.unzoom_out, R.anim.welcome_fade_in_scale
R.anim.welcome_fade_out, R.anim.zoom_enter, R.anim.zoom_exit };
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] array_name = new String[array_id.length];
        for (int i = 0; i < array_id.length; i++) {
            array_name[i] = getResources().getResourceName(array_id[i]).replace(getPackageName() + ":anim/""");
        }
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1new ArrayList<String>(Arrays.asList(array_name))));
    }
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        v.setAnimation(AnimationUtils.loadAnimation(thisarray_id[position]));
    }
}

AnimationUtils中的几个方法简介

AnimationUtils 中的方法
795730-20170317195212104-767600931.png
public static long currentAnimationTimeMillis() {
return SystemClock.uptimeMillis();
}
public static Animation makeInAnimation(Context c, boolean fromLeft) {
Animation a;
if (fromLeft) a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_left);
else a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_right);
a.setInterpolator(new DecelerateInterpolator());
a.setStartTime(currentAnimationTimeMillis());
return a;
}
public static Animation makeOutAnimation(Context c, boolean toRight) {
Animation a;
if (toRight) a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_right);
else a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_left);
a.setInterpolator(new AccelerateInterpolator());
a.setStartTime(currentAnimationTimeMillis());
return a;
}
public static Animation makeInChildBottomAnimation(Context c) {
Animation a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_child_bottom);
a.setInterpolator(new AccelerateInterpolator());
a.setStartTime(currentAnimationTimeMillis());
return a;
}

常用的在XML中定义的补间动画

slide_out_right
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_out_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-50%p"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>

slide_in_right

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_in_left
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
slide_in_child_bottom
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@interpolator/decelerate_quad">
<translate android:fromYDelta="100%" android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime" />
</set>
默认时间
<integer name="config_mediumAnimTime">400</integer>

常用的Activity转场动画中的补间动画

public void overridePendingTransition (int enterAnim, int exitAnim)。
【淡入淡出效果】  overridePendingTransition(android.R.anim. fade_in,android.R.anim. fade_out);
【由左向右滑入的效果】  overridePendingTransition(android.R.anim. slide_in_left,android.R.anim. slide_out_right);
其中,时间为400毫秒。 <integer name="config_mediumAnimTime">400</integer>
 
    
  1. fade_in 淡入
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:duration="@android:integer/config_longAnimTime"
  4. android:fromAlpha="0.0"
  5. android:interpolator="@interpolator/decelerate_quad"
  6. android:toAlpha="1.0" />
  7. fade_out 淡出
  8. <alpha xmlns:android="http://schemas.android.com/apk/res/android"
  9. android:duration="@android:integer/config_mediumAnimTime"
  10. android:fromAlpha="1.0"
  11. android:interpolator="@interpolator/accelerate_quad"
  12. android:toAlpha="0.0" />
  13. slide_in_left 从左边淡入到屏幕
  14. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  15. <translate
  16. android:duration="@android:integer/config_mediumAnimTime"
  17. android:fromXDelta="-50%p"
  18. android:toXDelta="0" />
  19. <alpha
  20. android:duration="@android:integer/config_mediumAnimTime"
  21. android:fromAlpha="0.0"
  22. android:toAlpha="1.0" />
  23. </set>
  24. slide_out_right 淡出到右边屏幕
  25. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  26. <translate
  27. android:duration="@android:integer/config_mediumAnimTime"
  28. android:fromXDelta="0"
  29. android:toXDelta="50%p" />
  30. <alpha
  31. android:duration="@android:integer/config_mediumAnimTime"
  32. android:fromAlpha="1.0"
  33. android:toAlpha="0.0" />
  34. </set>

常用的pop/dialog窗口显示/消失动画

通过下面代码可以实现在 Dialog或AlertDialog 显示、消失时的具有可爱的动画效果。
dialog.getWindow().setWindowAnimations(R.style.dialog_anim);

通过下面代码可以实现在popupWindow 显示、消失时的具有可爱的动画效果。
popWindow.setAnimationStyle(R.style. dialog_anim );

其中,R.style. dialog_anim 为在styles.xml中定义的一个样式
 
     
  1. <style name="dialog_animation" parent="@android:style/Animation">
  2. <!--窗体进入动画--><item name="android:windowEnterAnimation">@anim/popshow_anim</item>
  3. <!--窗体退出动画--><item name="android:windowExitAnimation">@anim/pophidden_anim</item>
  4. </style>
其中引用的便是两个自定义的补间动画。
常用的效果的设置如下:
 
       
  1. popshow_anim.xml 由下往上淡入
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  4. <translate
  5. android:duration="1000"
  6. android:fromYDelta="100%p"
  7. android:toYDelta="0" />
  8. <alpha
  9. android:duration="1000"
  10. android:fromAlpha="0.0"
  11. android:toAlpha="1.0" />
  12. </set>
  13. pophidden_anim.xml 由上往下淡出
  14. <?xml version="1.0" encoding="utf-8"?>
  15. <set xmlns:android="http://schemas.android.com/apk/res/android" >
  16. <translate
  17. android:duration="1000"
  18. android:fromYDelta="0"
  19. android:toYDelta="50%p" />
  20. <alpha
  21. android:duration="1000"
  22. android:fromAlpha="1.0"
  23. android:toAlpha="0.0" />
  24. </set>
2017-3-17

附件列表

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值