动画中Interpolator 加速器的使用

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

 


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

<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;
    @Override
    protected 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分支了,但是动画就是不显示

@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        
        // TODO Auto-generated method stub
        switch (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;

 

————————————————
版权声明:本文为CSDN博主「ecstatic」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/ecstatic/article/details/42100313

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值