Android动画二

1、XML中使用属性动画

  1.1、在res下建立animator文件夹,其他无效

<?xml version="1.0" encoding="utf-8"?>
  <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
  android:duration="1000"
  android:propertyName="scaleX"
  android:valueFrom="1.0"
  android:valueTo="2.0"
  android:valueType="floatType"
  >
</objectAnimator>

 

  1.2、程序中加载动画,并设置Tag,不设置无效

Animator animator = AnimatorInflater.loadAnimator(this, R.animator.object);
animator.setTarget(view);
animator.start();

2、Android布局动画

  ViewGroup中增加View时的动画效果

  1、布局文件XML中配置
  android:animateLayoutChanges="true",添加View时会显示一个过渡效果

  2、自定义动画
  LayoutAnimationController第一个参数表示视图动画,第二个参数是每个子View显示的delay时间,当delay不为0时,可以设置子View显示的顺序
  LayoutAnimationController.ORDER_NORMAL 顺序
  LayoutAnimationController.ORDER_RANDOM 随机
  LayoutAnimationController.ORDER_REVERSE 反序

    LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
    // 设置动画
    ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1);
    sa.setDuration(2000);
    // 设置显示属性
    LayoutAnimationController lac = new LayoutAnimationController(sa, 0.5f);
    lac.setOrder(LayoutAnimationController.ORDER_NORMAL);// 顺序
    ll.setLayoutAnimation(lac);
    TextView tv = new TextView(getApplication());
    tv.setText("分散精力福建省电力解放螺丝钉解放了就是大量飞机螺丝钉解放了世界辽阔的健康");
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.setMargins(10, 10, 10, 10);
    ll.addView(tv, params);

3、运行效果

  

4、部分源码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:animateLayoutChanges="true"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/xml_object_amin"
        android:id="@+id/button12"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/view_animate"
        android:id="@+id/button13"
        android:layout_below="@+id/button12"
        android:layout_centerHorizontal="true" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button13"
        android:layout_centerHorizontal="true"
        android:id="@+id/linearlayout"></LinearLayout>
</RelativeLayout>

  

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:propertyName="scaleX"
    android:valueFrom="1.0"
    android:valueTo="2.0"
    android:valueType="floatType"
    >

</objectAnimator>

  

public class ActivityObjectanimActivity extends Activity implements View.OnClickListener {


    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final RelativeLayout v = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_objectanim, null);
        setContentView(v);

        findViewById(R.id.button12).setOnClickListener(this);
        findViewById(R.id.button13).setOnClickListener(this);
        ll = (LinearLayout) findViewById(R.id.linearlayout);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                TextView tv = new TextView(getApplication());
                tv.setText("分散精力福建省电力解放螺丝钉解放了就是大量飞机螺丝钉解放了世界辽阔的健康");
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(10, 10, 10, 10);
                v.addView(tv, params);
            }
        }, 5000);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.button12:
                Animator animator = AnimatorInflater.loadAnimator(this, R.animator.object);
                animator.setTarget(view);
                animator.start();
                break;

            case R.id.button13:
                view.animate().alpha(0).y(300).setDuration(300).withStartAction(new Runnable() {
                    @Override
                    public void run() {

                    }
                }).withEndAction(new Runnable() { // API16才有的方法
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                System.out.println("------>动画结束");
                            }
                        });
                    }
                });


                // 设置动画
                ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1);
                sa.setDuration(2000);
                // 设置显示属性
                LayoutAnimationController lac = new LayoutAnimationController(sa, 0.5f);
                lac.setOrder(LayoutAnimationController.ORDER_NORMAL);// 顺序
                ll.setLayoutAnimation(lac);
                TextView tv = new TextView(getApplication());
                tv.setText("分散精力福建省电力解放螺丝钉解放了就是大量飞机螺丝钉解放了世界辽阔的健康");
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(10, 10, 10, 10);
                ll.addView(tv, params);
                break;
        }
    }
}

  


  

posted @ 2015-12-04 16:36  轻云沉峰  阅读(112)  评论(0)    收藏  举报