属性动画的实现

本文详细介绍了一个使用Android属性动画实现视图平移、缩放、旋转和透明度变化的例子。通过MainActivity中的按钮触发不同类型的动画效果,并展示了如何组合多种动画。

1.activity_main.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/bt1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="平移" />
        <Button
            android:id="@+id/bt2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放" />
        <Button
            android:id="@+id/bt3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转" />
        <Button
            android:id="@+id/bt4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="透明" />
        <Button
            android:id="@+id/bt5"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="综合" />
    </LinearLayout>

    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>
2.MainActivity.java

package com.cwj.anim3;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView iv;
    private Button bt1, bt2, bt3, bt4, bt5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 属性动画
        iv = (ImageView) findViewById(R.id.iv);
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        bt3 = (Button) findViewById(R.id.bt3);
        bt4 = (Button) findViewById(R.id.bt4);
        bt5 = (Button) findViewById(R.id.bt5);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        iv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == bt1) {
            // 平移
            ObjectAnimator tAnim = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f, 200f, 300f, 100f, 200f, 500f);
            tAnim.setDuration(5 * 1000);
            tAnim.start();

        } else if (view == bt2) {
            // 缩放
            ObjectAnimator sAnim = ObjectAnimator.ofFloat(iv, "scaleX", 1f, 2f, 3f, 1f);
            sAnim.setDuration(5 * 1000);
            sAnim.start();

            ObjectAnimator syAnim = ObjectAnimator.ofFloat(iv, "scaleY", 1f, 2f, 3f, 1f);
            syAnim.setDuration(5 * 1000);
            syAnim.start();

        } else if (view == bt3) {
            // 旋转
            ObjectAnimator rAnim = ObjectAnimator.ofFloat(iv, "rotationX", 0f, 360f);
            rAnim.setDuration(5 * 1000);
            rAnim.start();


        } else if (view == bt4) {
            // 透明
            ObjectAnimator aAnim = ObjectAnimator.ofFloat(iv, "alpha", 1f, 0.5f, 1f, 0.5f, 0.8f, 0.2f, 0f,1f);
            aAnim.setDuration(5 * 1000);
            aAnim.start();

        } else if (view == bt5) {
            // 综合
            //平移
            ObjectAnimator tAnim = ObjectAnimator.ofFloat(iv, "translationX", 0f, 100f, 200f, 300f, 100f, 200f, 500f);
            tAnim.setDuration(5 * 1000);
            //旋转
            ObjectAnimator rAnim = ObjectAnimator.ofFloat(iv, "rotationX", 0f, 360f);
            rAnim.setDuration(5 * 1000);

            AnimatorSet animSet = new AnimatorSet();
            //平移和旋转一起
            animSet.playTogether(tAnim, rAnim);
            //开始动画
            animSet.start();
            //取消动画
//            animSet.cancel();
            //结束动画
//            animSet.end();
            //暂停动画
//            animSet.pause();
            //继续动画
//            animSet.resume();

        }else if (view == iv){
            Toast.makeText(this, "您点击了我!", Toast.LENGTH_SHORT).show();
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

举儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值