Android 动画---property(属性)动画学习

来源:互联网 发布:vs mfc数据库编程实例 编辑:程序博客网 时间:2024/06/08 20:15

由于3.0之前已有的动画框架存在一些局限性–动画改变只显示,并不能响应事件。因此,在Android 3.0之后,Google 就提出了属性动画这样的一个动画框架,帮助开发者实现更加丰富的动画效果。
本质:修改对象的属性值实现动画
由于是3.0以上的版本,如果需要在3.0一下的平台上兼容的话需要下载NineoldAndroids.jar架包
为什么会推出这个动画框架呢,现在来分析下与Tween的区别:

  1. Tween动画只能施加View对象,Porperty可以施加到任何对象
  2. Tween 动画只是绘制效果动画,view的真正属性没有改变,porperty会改变属性
    现在看看一些常用的接口方法
    packet:android.animation ObjectAnimation
    Animator.AnimatorListener
    TileAnimator.TimeListener
    TimeInterpolator 插值器
    Keyframe 关键帧类
    PropertyValuesHolder 属性集合类
    使用:
    1.xml定制
    (TimeAnimator不支持)res/animator/
    调用:
 //加载xml布局 AnimatorInflater.loadAnimator(context,R.animator.example); a.setTarget(view);//设置作用的view a.start();//开启动画

代码中调用:

    使用ObjectAnimator         example.setPivotX(50); example.setPivotY(50);         //y轴旋转         ObjectAnimator.ofFloat(example, "rotationY", 0.0F, 360.0F)//     .setDuration(3000).start();//X轴旋转    ObjectAnimator.ofFloat(example, "rotationX", 0.0F, 360.0F)//     .setDuration(3000).start();//使用PropertyValuesHolder   组合动画 PropertyValuesHolder x = PropertyValuesHolder.ofFloat("x", 0, 250);                    PropertyValuesHolder y = PropertyValuesHolder.ofFloat("y", 0, 250);                    PropertyValuesHolder sx = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f);                    PropertyValuesHolder sy = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f);                    PropertyValuesHolder ry = PropertyValuesHolder.ofFloat("rotationY", 0, 360f);                    ObjectAnimator.ofPropertyValuesHolder(example, x, y, sx, sy, ry).setDuration(3000).start();//PropertyValuesHolder +Keyframe   实现回弹动画 Keyframe k1 = Keyframe.ofFloat(0, 0);                    k1.setInterpolator(new LinearInterpolator());                    Keyframe k2 = Keyframe.ofFloat(0.5f, 400);                    k2.setInterpolator(new BounceInterpolator());                    Keyframe k3 = Keyframe.ofFloat(1, 800);                    k3.setInterpolator(new BounceInterpolator());                    PropertyValuesHolder ty = PropertyValuesHolder.ofKeyframe("y", k1, k2, k3);                    ObjectAnimator.ofPropertyValuesHolder(ball, ty).setDuration(3000).start();//实现抛物线效果 ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 500);                    valueAnimator.setDuration(3000);                    valueAnimator.setInterpolator(new LinearInterpolator());                    valueAnimator.setTarget(ball);                    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                        @Override                        public void onAnimationUpdate(ValueAnimator animation) {                            float value = (float) animation.getAnimatedValue();                            ball.setX(value);                            ball.setY(0.008f * value * value);                        }                    });                    valueAnimator.start();

上面都是基本的使用方法,其中还有两个拓展的功能
ViewPropertyAnimator

view.animate().alpha(0)            .y(300)            .setDuration(300)            .withStartAction(new Runnable(){                    @Override                    public void run(){}            })            .withEndAction(new Runnable(){                    @Override                    public void run(){}            }).start();

LayoutAnimator:
场景:有view 添加,删除,隐藏,显示 自身动画和view动画
参数:
LayoutTransition.APPEARING 添加出现动画
LayoutTransition.CHANGE_APPEARING 当添加view导致布局改变时整个布局动画
LayoutTrasition.DISAPPEARING 当view消失或者隐藏式view消失动画
LayoutTransition.CHANGE_DISAPPEARING 删除或隐藏view致整个布局改变时的布局动画
LayoutTransition.CHANGE 当不是由于view的原因造成其他view改变时的布局动画

public class LayoutAnimaActivity extends Activity implements        CompoundButton.OnCheckedChangeListener {    private ViewGroup viewGroup;    private GridLayout mGridLayout;    private int mVal;    private LayoutTransition mTransition;    private CheckBox mAppear, mChangeAppear, mDisAppear, mChangeDisAppear, mChange, mAppearself;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layoutanimation);        viewGroup = (ViewGroup) findViewById(R.id.id_container);        mAppear = (CheckBox) findViewById(R.id.id_appear);        mChangeAppear = (CheckBox) findViewById(R.id.id_change_appear);        mDisAppear = (CheckBox) findViewById(R.id.id_disappear);        mChangeDisAppear = (CheckBox) findViewById(R.id.id_change_disappear);        mChange = (CheckBox) findViewById(R.id.id_change_disappear);        mAppearself = (CheckBox) findViewById(R.id.id_appear2);        mAppear.setOnCheckedChangeListener(this);        mChangeAppear.setOnCheckedChangeListener(this);        mDisAppear.setOnCheckedChangeListener(this);        mChangeDisAppear.setOnCheckedChangeListener(this);        mChange.setOnCheckedChangeListener(this);        mAppearself.setOnCheckedChangeListener(this);        // 创建一个GridLayout        mGridLayout = new GridLayout(this);        // 设置每列5个按钮        mGridLayout.setColumnCount(3);        // 添加到布局中        viewGroup.addView(mGridLayout);        //默认动画全部开启        mTransition = new LayoutTransition();        mGridLayout.setLayoutTransition(mTransition);        findViewById(R.id.addBtn).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                final Button button = new Button(LayoutAnimaActivity.this);                button.setText((++mVal) + "");                mGridLayout.addView(button, Math.min(1, mGridLayout.getChildCount()));                button.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        mGridLayout.removeView(button);                    }                });            }        });    }    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {        mTransition = new LayoutTransition();        mTransition.setAnimator(                LayoutTransition.APPEARING,                (mAppear.isChecked() ? mTransition                        .getAnimator(LayoutTransition.APPEARING) : null));        mTransition                .setAnimator(                        LayoutTransition.CHANGE_APPEARING,                        (mChangeAppear.isChecked() ? mTransition                                .getAnimator(LayoutTransition.CHANGE_APPEARING)                                : null));        mTransition.setAnimator(                LayoutTransition.DISAPPEARING,                (mDisAppear.isChecked() ? mTransition                        .getAnimator(LayoutTransition.DISAPPEARING) : null));        mTransition.setAnimator(                LayoutTransition.CHANGE_DISAPPEARING,                (mChangeDisAppear.isChecked() ? mTransition                        .getAnimator(LayoutTransition.CHANGE_DISAPPEARING)                        : null));        mTransition.setAnimator(                LayoutTransition.DISAPPEARING,                (mChange.isChecked() ? mTransition                        .getAnimator(LayoutTransition.CHANGING) : null));        mTransition.setAnimator(                LayoutTransition.APPEARING,                (mAppearself.isChecked() ? ObjectAnimator.ofFloat(this, "scaleY", 0, 1) : null));        mGridLayout.setLayoutTransition(mTransition);    }}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/id_container"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/addBtn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="addBtns" />    <CheckBox        android:id="@+id/id_appear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="APPEARING" />    <CheckBox        android:id="@+id/id_change_appear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="CHANGE_APPEARING" />    <CheckBox        android:id="@+id/id_disappear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="DISAPPEARING" />    <CheckBox        android:id="@+id/id_change_disappear"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="CHANGE_DISAPPEARING " />    <CheckBox        android:id="@+id/id_change"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:text="CHANGE" />    <CheckBox        android:id="@+id/id_appear2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="false"        android:text="APPEARING-selfdef" /></LinearLayout>

看的别人的视频,总结下,方面以后用!

0 0
原创粉丝点击