当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

8.4.3 Android动画合集之属性动画-初见

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:27:45

本节引言:本节给带来的是Android动画中的第三种动画——属性动画(Property Animation),记得在上一节8.4.2 Android动画合集之补间动画为Fragment设置过渡动画的时候,说过,App包和V4包下的Fragment调用setCustomAnimations()对应的动画类型是不一样的,v4包下的是Animation,而app包下的是Animator;Animation一般动画就是我们前面学的帧动画和补间动画!Animator则是本节要讲的属性动画!关于属性动画,大牛郭大叔已经写了三篇非常好的总结文,写得非常赞,就没必要重复造轮子了,不过这里还是过一遍,大部分内容参考的下面三篇文章:Android属性动画完全解析(上),初识属性动画的基本用法Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法Android属性动画完全解析(下),Interpolator和ViewPropertyAnimator的用法写的非常好,或者说你可以直接跳过本文去看上面的三篇文章~当然,你愿意看我叨叨逼的话,也很欢迎,好了,开始本节内容吧~1.属性动画概念叨叨逼不BB,直接上图,就是这么暴力~2.ValueAnimator简单使用使用流程:1.调用ValueAnimator的ofInt(),ofFloat()或ofObject()静态方法创建ValueAnimator实例2.调用实例的setXxx方法设置动画持续时间,插值方式,重复次数等3.调用实例的addUpdateListener添加AnimatorUpdateListener监听器,在该监听器中可以获得ValueAnimator计算出来的值,你可以值应用到指定对象上~4.调用实例的start()方法开启动画!另外我们可以看到ofInt和ofFloat都有个这样的参数:float/int... values代表可以多个值!使用示例:代码实现:布局文件:activity_main.xml,非常简单,四个按钮,一个ImageView<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ly_root" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画1" /> <Button android:id="@+id/btn_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画2" /> <Button android:id="@+id/btn_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画3" /> <Button android:id="@+id/btn_four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画4" /> <ImageView android:id="@+id/img_babi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@mipmap/img_babi" /></LinearLayout>接着到MainActivity.java,首先需要一个修改View位置的方法,这里调用moveView()设置左边和上边的起始坐标以及宽高!接着定义了四个动画,分别是:直线移动,缩放,旋转加透明,以及圆形旋转!然后通过按钮触发对应的动画~public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_one; private Button btn_two; private Button btn_three; private Button btn_four; private LinearLayout ly_root; private ImageView img_babi; private int width; private int height; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindViews(); } private void bindViews() { ly_root = (LinearLayout) findViewById(R.id.ly_root); btn_one = (Button) findViewById(R.id.btn_one); btn_two = (Button) findViewById(R.id.btn_two); btn_three = (Button) findViewById(R.id.btn_three); btn_four = (Button) findViewById(R.id.btn_four); img_babi = (ImageView) findViewById(R.id.img_babi); btn_one.setOnClickListener(this); btn_two.setOnClickListener(this); btn_three.setOnClickListener(this); btn_four.setOnClickListener(this); img_babi.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_one: lineAnimator(); break; case R.id.btn_two: scaleAnimator(); break; case R.id.btn_three: raAnimator(); break; case R.id.btn_four: circleAnimator(); break; case R.id.img_babi: Toast.makeText(MainActivity.this, "不愧是coder-pig~", Toast.LENGTH_SHORT).show(); break; } } //定义一个修改ImageView位置的方法 private void moveView(View view, int rawX, int rawY) { int left = rawX - img_babi.getWidth() / 2; int top = rawY - img_babi.getHeight(); int width = left + view.getWidth(); int height = top + view.getHeight(); view.layout(left, top, width, height); } //定义属性动画的方法: //按轨迹方程来运动 private void lineAnimator() { width = ly_root.getWidth(); height = ly_root.getHeight(); ValueAnimator xValue = ValueAnimator.ofInt(height,0,height / 4,height / 2,height / 4 * 3 ,height); xValue.setDuration(3000L); xValue.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

[1] [2] [3] [4]  下一页


8.4.3 Android动画合集之属性动画-初见