Android动画之属性动画

Android动画之属性动画

概述

补间动画,只是一个动画效果,组件其实还在原来的位置上,xy没有改变,属性动画则反之

Demo

MainActivity.java

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 {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }


    // 平移
    public void translate(View v){

        /**
         * 参数说明:
         * Object target         : 动画作用于哪个组件 
         * String propertyName   : 指定要改变组件的哪个属性
         * float... values       : 传入0,代表x起始坐标:当前x + 0
         *                            传入100,代表x终点坐标:当前x + 100
         */                         
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 0, 100);
//      ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "translationX", 10, 70, 50, 200);
        oa.setDuration(2000);
        oa.setRepeatCount(1);
        oa.setRepeatMode(ValueAnimator.REVERSE);
        oa.start();
    }

    // 缩放
    public void scale(View v){

        /**
         * 参数说明:
         * Object target         : 动画作用于哪个组件
         * String propertyName   : 指定要改变组件的哪个属性
         * float... values       : 缩放比例,2表示本身高度2倍,放大到本身高度的3倍
         */ 
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "scaleY", 2, 3f);
        oa.setDuration(2000);
        oa.start();
    }

    // 透明
    public void alpha(View v){

        /**
         * 参数说明:
         * Object target         : 动画作用于哪个组件
         * String propertyName   : 指定要改变组件的哪个属性
         * float... values       : 透明度变化 参数0--1表示从透明到不透明 
         */ 
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "alpha", 0, 1f);
        oa.setDuration(2000);
        oa.start();
    }

    // 旋转
    public void rotate(View v){

        /**
         * 参数说明:
         * Object target         : 动画作用于哪个组件
         * String propertyName   : 指定要改变组件的哪个属性
         * float... values       : 透明度变化 参数0--1表示从透明到不透明 
         */
        ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "rotationX", 0, 360);
        oa.setDuration(2000);
        oa.setRepeatCount(1);
        oa.setRepeatMode(ValueAnimator.REVERSE);
        oa.start();
    }

    // 播放动画集合
    public void fly(View v){
        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();
    }

    // 加载xml配置文件来加载动画
    public void xml(View v){
        Animator at = AnimatorInflater.loadAnimator(this, R.animator.objanimator);
        // 设置作用于哪个组件 
        at.setTarget(iv);
        at.start();
    }

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout 
        android:id="@+id/ll"
        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="translate"
        />
    <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="rotate"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="一起飞" 
        android:onClick="fly"
        />
</LinearLayout>
 <Button
     android:layout_below="@id/ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="xml定义的属性动画" 
        android:onClick="xml"
        />

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

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值