Android属性动画特点详解

所属分类: 软件编程 / Android 阅读数: 92
收藏 0 赞 0 分享

本文实例为大家分享了Android属性动画使用的具体代码,供大家参考,具体内容如下

MainActivity.java

/*
属性动画的特点:动画效果会改变控件的位置.且开启动画的是动画对象,而不是控件对象.
    只有旋转的属性动画是经常用的,注意参数.
    注意:这些方法都是安卓在3.0以后出现的新特性,所以要把AndroidManifest.xml里的android:minSdkVersion值修改为11以上
*/
//注释后面有222的暂时不用管.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  private ImageButton imageView;
  private Button alpha_bt;
  private Button rotationY_bt;
  private Button scaleX_bt;
  private Button translationX_bt;
  private Button AnimatorSet_bt;


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
//    对控件进行初始化
    init();

//   此处用的是xml的形式.引用在Xml里的属性动画资源. AnimatorInflater.loadAnimator(上下文,R.animator..)222
    Animator Xmlanimator = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
//   把要做动画控件对象放进去.Animator.setTarget(View对象);222
    Xmlanimator.setTarget(imageView);
//   开启动画.Animator.start.222
    Xmlanimator.start();

  }

  // 对于控件进行初始化
  private void init() {
    //找到ImageView控件对象
    imageView = (ImageButton) findViewById(R.id.animation_iv);
    //找到Button控件对象.
    alpha_bt = (Button) findViewById(R.id.alpha_bt);
    rotationY_bt = (Button) findViewById(R.id.rotationY_bt);
    scaleX_bt = (Button) findViewById(R.id.scaleX_bt);
    translationX_bt = (Button) findViewById(R.id.translationY_bt);
    AnimatorSet_bt = (Button) findViewById(R.id.AnimatorSet_bt);
    //为button设置点击事件
    alpha_bt.setOnClickListener(this);
    rotationY_bt.setOnClickListener(this);
    scaleX_bt.setOnClickListener(this);
    translationX_bt.setOnClickListener(this);
    AnimatorSet_bt.setOnClickListener(this);

  }

  /**
   * 根据点击事件类型.调用控件做属性动画的
   *
   * @param view
   */
  @Override
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.alpha_bt:
        //做透明动画,参数1:View,代表你要修改那个控件的属性. 参数2:propertyName代表实现什么样子的动画:"alpha",String类型.
        //参数3:float... values,控件修改的参数,new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
        //设置动画执行时长.setDuration
        alpha.setDuration(2000);
        //设置动画执行的模式setRepeatMode,参数用ObjectAnimator引用.
        alpha.setRepeatMode(ObjectAnimator.RESTART);
        //设置动画执行的次数.setRepeatCount
        alpha.setRepeatCount(1);
        //使用ObjectAnimator对象开启动画.
        alpha.start();

        break;
      case R.id.rotationY_bt:
        //做旋转动画,"rotationY".rotationX,rotation new float[]{90f, 180f, 270f, 360f}
        ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", new float[]{90f, 180f, 270f, 360f});
        rotationY.setDuration(2000);
        rotationY.setRepeatMode(ObjectAnimator.RESTART);
        rotationY.setRepeatCount(1);
        rotationY.start();
        break;
      case R.id.scaleX_bt:
        //做缩放动画,scaleX,scaleY new float[]{1f, 2f, 3f, 4f, 5f, 6f,1f}
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 1f});
        scaleX.setDuration(2000);
        scaleX.setRepeatMode(ObjectAnimator.RESTART);
        scaleX.setRepeatCount(1);
        scaleX.start();

        break;
      case R.id.translationY_bt:
        //做平移动画,translationY,translationX new float[]{10f, 20f, 30f, 40f, 60f, 80f}
        ObjectAnimator translationY = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
        translationY.setDuration(2000);
        translationY.setRepeatMode(ObjectAnimator.RESTART);
        translationY.setRepeatCount(1);
        translationY.start();

        //做动画集合AnimatorSet,分别创建两个动画对象.注意playTogether(动画对象...)和playSequentially的区别.最后开启动画.start
      case R.id.AnimatorSet_bt:
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(imageView, "translationX", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
        oa.setDuration(3000);
        ObjectAnimator oa2 = ObjectAnimator.ofFloat(imageView, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
        oa2.setDuration(3000);
        set.playTogether(oa, oa2);
        set.start();
        break;
      default:
        break;
    }
  }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
<!--  注意background的属性置为null -->

  <Button
    android:id="@+id/alpha_bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="alpha"/>

  <Button
    android:id="@+id/translationY_bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="translationY"/>

  <Button
    android:id="@+id/scaleX_bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="scaleX"/>

  <Button
    android:id="@+id/rotationY_bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="rotationY"/>

  <Button
    android:id="@+id/AnimatorSet_bt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="AnimatorSet"/>

  <ImageButton
    android:onClick="yyyy"
    android:id="@+id/animation_iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/a8"
    android:background="@null"/>

</LinearLayout>


在res目录下创建animator文件夹在文件夹里创建以下xml

objectanimator.xml

<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  android:propertyName="rotationX"
  android:duration="3000"
  android:repeatCount="1"
  android:repeatMode="reverse"
  android:startOffset="0"
  android:valueFrom="360.0">
</objectAnimator>

<!--注意:在xml定义动画类的属性,浮点型小数,直接写小数即可,不用再带f.
提示:控件位移的参照单位在xml文件里有所不同,不过更简单,不用再特意去定义参照物属性了,直接是根据值,区分两种方式:
  一种是以整个屏幕为参照物,在xml文件属性定义值是int%p;  一种以控件自身大小为参照物,在xml文件属性定义值是int-->

---------------------
作者:FanRQ_
来源:CSDN
原文:https://blog.csdn.net/FanRQ_/article/details/84072052
版权声明:本文为博主原创文章,转载请附上博文链接!

更多精彩内容其他人还在看

Android样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多