Android动画中Interpolator 加速器的使用

来源:互联网 发布:网络协议是 编辑:程序博客网 时间:2024/05/01 03:46

遇到一个项目需求,想让动画变得更活泼一点,于是想到了动画属性中的Interpolator,写了基本例子测试一下android提供给我们现成的加速器的效果:

效果代码中方法xml中属性越来越快AccelerateInterpolator()@android:anim/accelerate_interpolator越来越慢DecelerateInterpolator()@android:anim/decelerate_interpolator先快后慢AccelerateDecelerateInterpolator()@android:anim/accelerate_decelerate_interpolator先后退一小步然后向前加速AnticipateInterpolator()@android:anim/anticipate_interpolator快速到达终点超出一小步然后回到终点OvershootInterpolator()@android:anim/overshoot_interpolator到达终点超出一小步然后回到终点AnticipateOvershootInterpolator()@android:anim/anticipate_overshoot_interpolator弹球效果,弹几下回到终点BounceInterpolator()@android:anim/bounce_interpolator均匀速度LinearInterpolator()@android:anim/linear_interpolator


设置布局的时候,直接找了一个背景 还有一个需要移动的图片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bg"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:src="@drawable/peo" /></RelativeLayout>

在页面中,直接让ImageView执行平移动画也没问题,图片成功的加速的向左运行

public class MainActivity extends Activity {ImageView img;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); img=(ImageView)findViewById(R.id.imageView1);  TranslateAnimation translate=new TranslateAnimation(0, -750, 0, 0);translate.setDuration(4000);translate.setFillAfter(true);translate.setInterpolator(new AccelerateInterpolator() );img.setAnimation(translate);}}

写到这儿,突然想把所有的加速动画效果都看一遍,于是就在ActionBar上把menu都添加上了,运行的时候,ActionBar上那三个小点不见了,只能通过点击物理键盘的菜单键,才能弹出menu,所以在代码中,有加上getOverflowMenu()方法

//显示ActionBar上隐藏目录的三个点private void getOverflowMenu() {        try {           ViewConfiguration config = ViewConfiguration.get(this);           Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");           if(menuKeyField != null) {               menuKeyField.setAccessible(true);               menuKeyField.setBoolean(config, false);           }       } catch (Exception e) {           e.printStackTrace();       }   }


然后在onMenuItemSelected方法中,开始设置每一个加速度的效果,但是发现一个问题,明明已经运行进入case R.id.morefast分支了,但是动画就是不显示

@Overridepublic boolean onMenuItemSelected(int featureId, MenuItem item) {// TODO Auto-generated method stubswitch (item.getItemId()) {case R.id.morefast:  //越来越快  TranslateAnimation translate=new TranslateAnimation(0, -750, 0, 0);translate.setDuration(4000);translate.setFillAfter(false);translate.setInterpolator(new AccelerateInterpolator() );img.setAnimation(translate);break;case R.id.moreslow: //越来越慢 TranslateAnimation translate_1=new TranslateAnimation(0, -750, 0, 0); translate_1.setDuration(4000); translate_1.setFillAfter(true); translate_1.setInterpolator(new DecelerateInterpolator() );img.setAnimation(translate_1);break;

后来在每一个case分支,都先对ImageView 清除一下动画显示,再增加新的动画就可以实现效果了

case R.id.morefast:  //越来越快  img.clearAnimation();  TranslateAnimation translate=new TranslateAnimation(0, -750, 0, 0);translate.setDuration(4000);translate.setFillAfter(false);translate.setInterpolator(new AccelerateInterpolator() );img.setAnimation(translate);break;

测试程序的下载地址:点击打开链接






0 0
原创粉丝点击