属性动画

在上一篇博客中,介绍了补间动画,这篇博客中将介绍属性动画

常见的属性动画和补间动画一样也包括平移动画、缩放动画、透明度变化动画、旋转动画等

平移动画的演示效果:



缩放动画的展示效果:



透明度变化的动画的展示效果:



旋转动画的展示效果:



一起飞动画的展示效果,一起飞表示所有的动画一起播放:



xml定义的属性动画的实现效果



实现方式

第一步:使用Android Studio创建一个Android工程,并且在drawable文件夹中放一张图片



第二步:新建一个objanimator.xml文件并在文件中添加下面的代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <objectAnimator
        android:propertyName="translationX"
        android:duration="200"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:valueFrom="-100"
        android:valueTo="100"
        >

    </objectAnimator>
</set>


第三步:修改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:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="平移"
            android:onClick="translation"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缩放"
            android:onClick="scale"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="透明"
            android:onClick="alpha"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转"
            android:onClick="rotation"/>

    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一起飞"
            android:onClick="flay"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xml定义的属性动画"
            android:onClick="xml"/>

    </LinearLayout>
        
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/gold"
            android:layout_centerInParent="true"/>

    </RelativeLayout>

</LinearLayout>

最后一步:修改MainActivity.java中的代码

package com.fyt.propertyanimate;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

    //用于创建ImageView对象
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获得布局文件上的ImageView
        iv = (ImageView) findViewById(R.id.iv);
    }

    //平移按钮响应函数
    public void translation(View view) {

        /***
         * 创建修改组件的x坐标的属性动画
         * 第一个参数:动画作用于那个组件
         * 第二个参数:组件的属性
         * 第三个参数:组件第一次移动的x坐标 = 组件的真实的x坐标 + 10
         * 第四个参数:组件第二次移动的x坐标 = 组件的真实的x坐标 + 70
         * 第五个参数:组件第三次移动的x坐标 = 组件的真实的x坐标 + 20
         * 第六个参数:组件第四次移动的x坐标 = 组件的真实的x坐标 + 100
         */
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100);

        //设置动画的播放时间
        oa.setDuration(2000);

        //设置动画循环播放的次数
        oa.setRepeatCount(1);

        //设置第二次播放动画的方向为相反播放
        oa.setRepeatMode(ValueAnimator.REVERSE);

        //播放属性动画
        oa.start();
    }

    //缩放按钮响应函数
    public void scale(View view) {

        //创建缩放的属性动画
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2);
        oa.setDuration(200);
        oa.start();
    }

    //修改透明度按钮响应函数
    public void alpha(View view) {

        //创建改变透明度的属性动画
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.6f, 0.2f, 1);
        oa.setDuration(200);
        oa.start();
    }

    //旋转按钮响应函数
    public void rotation(View view) {

        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);
        oa.setDuration(200);
        oa.start();
    }

    //一起飞按钮响应函数
    public void flay(View view) {

        //创建动画师集合
        AnimatorSet set = new AnimatorSet();

        ObjectAnimator oa1 = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 20, 100);
        oa1.setDuration(2000);
        oa1.setRepeatCount(1);
        oa1.setRepeatMode(ValueAnimator.REVERSE);

        ObjectAnimator oa2 = ObjectAnimator.ofFloat(iv, "translationY", 10, 70, 20, 100);
        oa2.setDuration(2000);
        oa2.setRepeatCount(1);
        oa2.setRepeatMode(ValueAnimator.REVERSE);

        ObjectAnimator oa3 = ObjectAnimator.ofFloat(iv, "scaleX", 1, 1.6f, 1.2f, 2);
        oa3.setDuration(2000);
        oa3.setRepeatCount(1);
        oa3.setRepeatMode(ValueAnimator.REVERSE);

        ObjectAnimator oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);
        oa4.setDuration(2000);
        oa4.setRepeatCount(1);
        oa4.setRepeatMode(ValueAnimator.REVERSE);

        //设置动画挨个播放
        //set.playSequentially(oa1, oa2, oa3, oa4);

        //设置所有的动画一起播放
        set.playTogether(oa1, oa2, oa3, oa4);

        //播放动画
        set.start();
    }

    //属性动画按钮响应函数
    public void xml(View view)  {

        //使用动画填充器将用xml文件创建好的属性动画文件填充成动画师对象
        Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator);

        //设置动画作用于那个组件
        at.setTarget(iv);

        //播放动画
        at.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值