介绍一下android的属性动画,为什么需要android的属性动画?Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View Animation相当简单,不过只能支持简单的缩放、平移、旋转、透明度基本的动画,且有一定的局限性;比如我们使用动画将按钮变大,你希望当动画停止时,View的位置就是当前的位置;这些View Animation都无法做到;

布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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:gravity="center_horizontal" android:orientation="vertical" tools:context="com.example.zhiwenyan.animationdemo.ButtonActivity">    <Button  android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画属性" />    <Button  android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="动画属性" />    <Button  android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="组合动画属性" />    <Button  android:id="@+id/btn3" android:background="#f57171" android:layout_width="200dp" android:layout_height="200dp" android:text="动画属性" />  <Button  android:id="@+id/btn4" android:background="#f57171" android:layout_width="200dp" android:layout_height="200dp" android:text="动画属性" /></LinearLayout>
package com.example.zhiwenyan.animationdemo;import android.animation.Animator;import android.animation.AnimatorListenerAdapter;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.animation.PropertyValuesHolder;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.view.animation.AccelerateDecelerateInterpolator;import android.view.animation.BounceInterpolator;import android.widget.Button;import android.widget.Toast;public class ButtonActivity extends AppCompatActivity {    private Button btn;    private Button btn1;    private Button btn2;    private Button btn3;    private Button btn4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_button);        btn = (Button) findViewById(R.id.btn);        btn1 = (Button) findViewById(R.id.btn1);        btn2 = (Button) findViewById(R.id.btn2);        btn3 = (Button) findViewById(R.id.btn3);        btn4 = (Button) findViewById(R.id.btn4);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //使按钮的宽度变为500像素                //直接设置Width                ObjectAnimator.ofInt(btn, "width", 500).setDuration(1000).start();            }        });        btn1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                ObjectAnimator oa = ObjectAnimator.ofFloat(v, "alpha", 1.0f, 0.5f);                oa.setDuration(1000);                oa.start();            }        });        btn2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //组合动画的使用                PropertyValuesHolder pv1 = PropertyValuesHolder.ofInt("width", 600);                PropertyValuesHolder pv2 = PropertyValuesHolder.ofFloat("alpha", 1, 0f, 0.5f);                ObjectAnimator.ofPropertyValuesHolder(v, pv1, pv2).setDuration(1000).start();            }        });        btn3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //ObjectAnimator动画执行的类                ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "rotation", 0.0f, 360.0f);                //x轴缩放 1.0f-1.2f                ObjectAnimator oa2 = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 1.2f, 1.0f, 1.2f);                //y轴缩放 1.0f-1.2f                ObjectAnimator oa3 = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 1.2f, 1.0f, 1.2f);                AnimatorSet as = new AnimatorSet();                as.playTogether(oa1, oa2, oa3);                as.setDuration(2000);                as.start();            }        });        btn4.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(final View v) {                ObjectAnimator oa1 = ObjectAnimator.ofFloat(v, "rotation", 0.0f, 360.0f);                ObjectAnimator oa2 = ObjectAnimator.ofFloat(v, "scaleX", 1.0f, 1.2f, 1.0f, 1.2f);                ObjectAnimator oa3 = ObjectAnimator.ofFloat(v, "scaleY", 1.0f, 1.2f, 1.0f, 1.2f);                //Y轴平移100像素               ObjectAnimator oa4 = ObjectAnimator.ofFloat(v, "translationY", 0f, 100f);                //动画一起执行的类                AnimatorSet as = new AnimatorSet();                //一起执行oa1,oa2,oa3,接着执行oa4                as.play(oa1).with(oa2);                as.play(oa2).with(oa3);                as.play(oa4).after(oa3);                //设置动画插值器                //AccelerateDecelerateInterpolator 加速插值器                //BounceInterpolator //弹性插值器                as.setInterpolator(new AccelerateDecelerateInterpolator());                as.setInterpolator(new BounceInterpolator());                as.setDuration(2000);                as.start();                //动画监听的适配器                as.addListener(new AnimatorListenerAdapter() {                    @Override                    public void onAnimationEnd(Animator animation) {                        super.onAnimationEnd(animation);                        //通过ViewGroup删除                        ViewGroup vg = (ViewGroup) v.getParent();                        if (vg != null) {                            vg.removeView(v);                            Toast.makeText(ButtonActivity.this, "移除", Toast.LENGTH_SHORT).show();                        }                    }                });// //动画监听器// as.addListener(new Animator.AnimatorListener() {// @Override// public void onAnimationStart(Animator animation) {// }// //动画结束后,移除掉// @Override// public void onAnimationEnd(Animator animation) {// //通过ViewGroup删除// ViewGroup vg = (ViewGroup) v.getParent();// if (vg != null) {// vg.removeView(v);// Toast.makeText(ButtonActivity.this, "移除", Toast.LENGTH_SHORT).show();// }// }// @Override// public void onAnimationCancel(Animator animation) {// }// @Override// public void onAnimationRepeat(Animator animation) {//// }// });//            }        });    }}

当然还可以通过xml文件设置属动画,在这就不介绍了,关键是要理解属性的动画的用途

小编的能力有限,如果那里错了,欢迎指出来!

更多相关文章

  1. Android(安卓)Layout XML属性
  2. Android(安卓)控件七 ImageView 控件
  3. Android中Animator & Animation比较
  4. Android手势识别ViewFlipper触摸动画
  5. android:configChanges属性
  6. LinearLayout 线性布局管理器
  7. ScrollView常用属性汇总
  8. Android常用布局及属性--LinearLayout
  9. 【android】TextView属性大全

随机推荐

  1. 关于Unity中Android设备调用震动接口的实
  2. Android配置ssh服务
  3. NativeActivity原理
  4. 关于android开发中的@Override
  5. android 按键控制飞机
  6. Android 系统启动流程简介
  7. 32、adb INSTALL_FAILED_TEST_ONLY 处理
  8. 【Android Developers Training】 42. 从
  9. android手机通过USB共享电脑网络
  10. 如何使用好android的可访问性服务(Accessi