栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android属性动画特点详解

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android属性动画特点详解

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

MainActivity.java


//注释后面有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);

  }

  
  @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




  


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

objectanimator.xml




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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155299.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号