您的位置:首页 > 其它

【补间动画示例】Tweened Animation

2017-03-14 18:14 197 查看

代码中定义动画示例

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_1, new 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(iv, new Random().nextBoolean(), new Random().nextBoolean(), true);//最后一帧,即fillBefore的值无效,fillAfter的值有效 break; case 7: scale(iv, new 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_1, new ArrayList<String>(Arrays.asList(array_name)))); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { v.setAnimation(AnimationUtils.loadAnimation(this, array_id[position])); }}

AnimationUtils中的几个方法简介

AnimationUtils 中的方法


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>
fade_in 淡入

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

android:duration="@android:integer/config_longAnimTime"

android:fromAlpha="0.0"

android:interpolator="@interpolator/decelerate_quad"

android:toAlpha="1.0" />

fade_out 淡出

<alpha xmlns:android="http://schemas.android.com/apk/res/android"

android:duration="@android:integer/config_mediumAnimTime"

android:fromAlpha="1.0"

android:interpolator="@interpolator/accelerate_quad"

android:toAlpha="0.0" />

slide_in_left 从左边淡入到屏幕

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate

    android:duration="@android:integer/config_mediumAnimTime"

android:fromXDelta="-50%p"

android:toXDelta="0" />

<alpha

    android:duration="@android:integer/config_mediumAnimTime"

    android:fromAlpha="0.0"

    android:toAlpha="1.0" />

</set>

slide_out_right  淡出到右边屏幕

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate

    android:duration="@android:integer/config_mediumAnimTime"

android:fromXDelta="0"

android:toXDelta="50%p" />

<alpha

    android:duration="@android:integer/config_mediumAnimTime"

    android:fromAlpha="1.0"

    android:toAlpha="0.0" />

</set>

[/code]

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

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

通过下面代码可以实现在popupWindow 显示、消失时的具有可爱的动画效果。popWindow.setAnimationStyle(R.style.dialog_anim);
其中,R.style.dialog_anim为在styles.xml中定义的一个样式
<style name="dialog_animation" parent="@android:style/Animation">

<!--窗体进入动画--><item name="android:windowEnterAnimation">@anim/popshow_anim</item>

<!--窗体退出动画--><item name="android:windowExitAnimation">@anim/pophidden_anim</item>

</style>

[/code]其中引用的便是两个自定义的补间动画。
常用的效果的设置如下:
popshow_anim.xml 由下往上淡入

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate

android:duration="1000"

android:fromYDelta="100%p"

android:toYDelta="0" />

<alpha

android:duration="1000"

    android:fromAlpha="0.0"

    android:toAlpha="1.0" />

</set>

pophidden_anim.xml 由上往下淡出

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate

android:duration="1000"

android:fromYDelta="0"

android:toYDelta="50%p" />

<alpha

android:duration="1000"

    android:fromAlpha="1.0"

    android:toAlpha="0.0" />

</set>

[/code]2017-3-17

附件列表

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: