动画

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/Start_Anim"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="ValueAnim开始吧" />

    <Button
        android:id="@+id/Object_Anim"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="ObjectAnimator开始吧" />

    <Button
        android:id="@+id/XML_Anim"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="XMl开始吧" />
</LinearLayout>

 

animator:

set_animotor:

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially">
    <!--
      // 表示Set集合内的动画按顺序进行
    // ordering的属性值:sequentially & together
    // sequentially:表示set中的动画,按照先后顺序逐步进行(a 完成之后进行 b )
    // together:表示set中的动画,在同一时间同时进行,为默认值
    -->


    <set android:ordering="together">
        <!-- 下面的动画同时进行 -->
        <objectAnimator
            android:duration="2000"
            android:propertyName="translationX"
            android:valueFrom="0"
            android:valueTo="300"
            android:valueType="floatType"></objectAnimator>

        <objectAnimator
            android:duration="3000"
            android:propertyName="rotation"
            android:valueFrom="0"
            android:valueTo="360"
            android:valueType="floatType"></objectAnimator>
    </set>

    <set android:ordering="sequentially">
        <!--// 下面的动画按序进行-->
        <objectAnimator
            android:duration="1500"
            android:propertyName="alpha"
            android:valueFrom="1"
            android:valueTo="0"
            android:valueType="floatType"></objectAnimator>
        <objectAnimator
            android:duration="1500"
            android:propertyName="alpha"
            android:valueFrom="0"
            android:valueTo="1"
            android:valueType="floatType"></objectAnimator>
    </set>
</set>

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button Start_Anim;
    private Button Object_Anim;
    private Button XML_Anim;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //缺点
//        TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 100);
//        translateAnimation.setDuration(3000);
//        translateAnimation.setFillAfter(true);
//        Start_Anim.setAnimation(translateAnimation);
    }

    private void initView() {
        Start_Anim = (Button) findViewById(R.id.Start_Anim);
        Object_Anim = findViewById(R.id.Object_Anim);
        Start_Anim.setOnClickListener(this);
        Object_Anim.setOnClickListener(this);
        XML_Anim = findViewById(R.id.XML_Anim);
        XML_Anim.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Start_Anim:
                valueAnimStart();
                Toast.makeText(this, "吕小帅", Toast.LENGTH_SHORT).show();
                break;
            case R.id.Object_Anim:
                //参数1:给某个对象设置动画,参数2:是什么动画呢
//                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(Start_Anim, "alpha", 1f, 0f, 1f);
//                objectAnimator.setDuration(2000);
//                objectAnimator.start();

                // 步骤1:设置需要组合的动画效果
                ObjectAnimator translation = ObjectAnimator.ofFloat(Start_Anim, "translationX", 0, 300, 500);
                // 平移动画
                ObjectAnimator rotate = ObjectAnimator.ofFloat(Start_Anim, "rotation", 0f, 360f);
                // 旋转动画
                ObjectAnimator alpha = ObjectAnimator.ofFloat(Start_Anim, "alpha", 1f, 0f, 1f);
                // 透明度动画
                // 步骤2:创建组合动画的对象
                AnimatorSet animSet = new AnimatorSet();
                // 步骤3:根据需求组合动画
//                AnimatorSet.play(Animator anim)   :播放当前动画
//                AnimatorSet.after(long delay)   :将现有动画延迟x毫秒后执行
//                AnimatorSet.with(Animator anim)   :将现有动画和传入的动画同时执行
//                AnimatorSet.after(Animator anim)   :将现有动画插入到传入的动画之后执行
//                AnimatorSet.before(Animator anim) :  将现有动画插入到传入的动画之前执行

                animSet.play(translation).with(rotate).before(alpha);
                animSet.setDuration(5000);
                // 步骤4:启动动画
                animSet.start();
                break;
            case R.id.XML_Anim:
                AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.set_animotor);
                set.setTarget(Object_Anim);
                set.start();
                break;
        }
    }

    private void valueAnimStart() {
        //第一步创建对象 都是通过类名调用即可
        //ofFloat float...可变参数
        ValueAnimator valueAnimator = ValueAnimator.ofInt(Start_Anim.getLayoutParams().width, 500);
        valueAnimator.setDuration(3000);
        valueAnimator.setStartDelay(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //移动的单位  得值是跟你赋值对象的时候那个方法要一致
                int currentValue = (Integer) animation.getAnimatedValue();
                Start_Anim.getLayoutParams().width = currentValue;
                Log.e("currentValue", currentValue + "单位");
                //刷新自定义View的方法
//                invalidate();
//                postInvalidate()
                //RequestLayout 三个方法都会执行
                Start_Anim.requestLayout();
            }
        });
        valueAnimator.start();
    }
}
 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值