Android属性动画(透明,平移,旋转,缩放,组合)

布局就是几个按钮和实现动画的载体

<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_alpha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="透明" />

        <Button
            android:id="@+id/btn_translation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="平移" />

        <Button
            android:id="@+id/btn_rotation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转" />

        <Button
            android:id="@+id/btn_scale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放" />

        <Button
            android:id="@+id/btn_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="颜色" />
        <Button
            android:id="@+id/btn_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="组合" />
    </LinearLayout>
    <!--改变的View-->
    <TextView
        android:id="@+id/tv_text"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#d43c3c" />
</LinearLayout>

实现动画代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mText = (TextView) findViewById(R.id.tv_text);
        findViewById(R.id.btn_alpha).setOnClickListener(this);
        findViewById(R.id.btn_translation).setOnClickListener(this);
        findViewById(R.id.btn_rotation).setOnClickListener(this);
        findViewById(R.id.btn_scale).setOnClickListener(this);
        findViewById(R.id.btn_color).setOnClickListener(this);
        findViewById(R.id.btn_all).setOnClickListener(this);
        mText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "---", Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_alpha://实现透明度
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mText, "alpha", 1, 0.8f, 0.6f, 0.2f, 0, 0.2f, 0.5f, 1);
                objectAnimator.setDuration(6000);
                objectAnimator.start();
                break;
            case R.id.btn_translation://平移
                //translationY Y轴平移 translationX  X轴平移
                ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(mText, "translationY", 0, 100, 300, 500, 300, 0);
                objectAnimator1.setDuration(2000);
                objectAnimator1.setRepeatCount(10);
                objectAnimator1.start();
                break;
            case R.id.btn_rotation://旋转
                ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(mText, "rotation", 0, 90, 270);
                objectAnimator2.setDuration(3000);
                objectAnimator2.start();
                break;
            case R.id.btn_scale://缩放
                ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(mText, "scaleY", 1, 2, 3, 2, 1, 0.5f, 0.1f);
                objectAnimator3.setDuration(3000);
                objectAnimator3.start();
                break;
            case R.id.btn_color://改变颜色
                ObjectAnimator objectAnimator4 = ObjectAnimator.ofInt(mText, "backgroundColor",    
                       Color.RED, Color.YELLOW, Color.BLACK, Color.BLUE);
                objectAnimator4.setDuration(3000);
                objectAnimator4.start();
                break;
            case R.id.btn_all://组合
                int width = getWindowManager().getDefaultDisplay().getWidth();
                int height = getWindowManager().getDefaultDisplay().getHeight();
                ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(mText,
                                              "translationX", 0, width - dip2px(50));
                ObjectAnimator objectAnimator5_Y = ObjectAnimator.ofFloat(mText,
                                             "translationY", 0, height - dip2px(175));
                ObjectAnimator objectAnimator8=ObjectAnimator.ofInt(mText,
                          "backgroundColor", Color.RED,Color.YELLOW,Color.BLACK,Color.BLUE);
                //after 先执行哪个
                //before 后执行哪个
                //with 同时执行
                AnimatorSet animatorSet = new AnimatorSet();
                // animatorSet.playSequentially(objectAnimator5,objectAnimator5_Y);
                animatorSet.play(objectAnimator5).with(objectAnimator5_Y).with(objectAnimator8);
                animatorSet.setDuration(3000);

                animatorSet.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Toast.makeText(MainActivity.this, "END", Toast.LENGTH_LONG).show();
                    }
                    @Override
                    public void onAnimationCancel(Animator animation) {
                    }
                    @Override
                    public void onAnimationRepeat(Animator animation) {
                    }
                });
                animatorSet.start();
                break;
        }
    }
    public int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值