0
点赞
收藏
分享

微信扫一扫

Android 属性动画 实例简介



文章目录

  • 1、简介
  • 2 、简单示例
  • 1) 代码结构
  • 2、activity_main.xml 布局文件
  • 2)、Mainactivity.java 文件
  • 3)多个属性一起改变,多动画一起执行
  • 4) 多属性依次改变,控制动画先后
  • 5) 属性组合,控制动画随意组合和先后
  • 5) 动画事件监听


1、简介

在一定的时间内,通过不断对值进行改变,并不断讲值赋值给对象的属性,从而实现对该对象在该属性上的动画效果。

属性动画实现的大概逻辑:

Android 属性动画 实例简介_android

2 、简单示例

一个简单的 平移的动画

Android 属性动画 实例简介_xml_02

1) 代码结构

Android 属性动画 实例简介_android_03

2、activity_main.xml 布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/img_id"
    android:background="@drawable/d"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="450dp"
        android:layout_marginLeft="150dp"
        android:text="move"
        android:textSize="30dp"
        android:onClick="moveOnclick"/>
</RelativeLayout>

2)、Mainactivity.java 文件

package myapplication.lum.com.myanimation;

import android.animation.ObjectAnimator;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private String TAG = "lum: ";
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.img_id);
    }

    public void moveOnclick(View view) {
        /*
        * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
        *
        * 这个就是在1S的时间里,将 imageView 对象的属性 translationX ,从0 ~ 250 ,这个属性是控制 距离x轴距离的
        * */
        ObjectAnimator.ofFloat(imageView,"translationX",0F,250F).setDuration(1000).start();
    }
}

3)多个属性一起改变,多动画一起执行

public void moveOnclick(View view) {
        /*
        * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
        * */
        ObjectAnimator.ofFloat(imageView,"translationX",0F,250F).setDuration(1000).start();  //x轴距离
        ObjectAnimator.ofFloat(imageView,"translationY",0F,250F).setDuration(1000).start();  //y 轴距离
        ObjectAnimator.ofFloat(imageView,"rotation",0F,120F).setDuration(1000).start(); //旋转角度
    }

Android 属性动画 实例简介_属性值_04

我们看到,我们写了三个属性的改变,x,y ,roat,在运行的时候时同时进行的。

当然也可以这样写;效果一样

public void moveOnclick(View view) {
        /*
        * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
        * */
//        ObjectAnimator.ofFloat(imageView,"translationX",0F,250F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"translationY",0F,250F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"rotation",0F,120F).setDuration(1000).start();
        PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX",0F,250F);
        PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,250F);
        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation",0F,120F);
        ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();
    }

或者这样写:

public void moveOnclick(View view) {
        /*
         * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
         * */
//        ObjectAnimator.ofFloat(imageView,"translationX",0F,250F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"translationY",0F,250F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"rotation",0F,120F).setDuration(1000).start();
        
          ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView,"translationX",0F,250F);
          ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView,"translationY",0F,250F);
          ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView,"rotation",0F,120F);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animator1,animator2,animator3);
        animatorSet.setDuration(1000);
        animatorSet.start();
    }

4) 多属性依次改变,控制动画先后

Android 属性动画 实例简介_android_05


public void moveOnclick(View view) {
 /*
 * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
 * */
 // ObjectAnimator.ofFloat(imageView,“translationX”,0F,250F).setDuration(1000).start();
 // ObjectAnimator.ofFloat(imageView,“translationY”,0F,250F).setDuration(1000).start();
 // ObjectAnimator.ofFloat(imageView,“rotation”,0F,120F).setDuration(1000).start();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView,"translationX",0F,250F);
      ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView,"translationY",0F,250F);
      ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView,"rotation",0F,120F);
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playSequentially(animator1,animator2,animator3);
    animatorSet.setDuration(1000);
    animatorSet.start();
}

5) 属性组合,控制动画随意组合和先后

Android 属性动画 实例简介_属性值_06

public void moveOnclick(View view) {
        /*
         * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
         * */
//        ObjectAnimator.ofFloat(imageView,"translationX",0F,250F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"translationY",0F,250F).setDuration(1000).start();
//        ObjectAnimator.ofFloat(imageView,"rotation",0F,120F).setDuration(1000).start();

          ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView,"translationX",0F,250F);
          ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView,"translationY",0F,250F);
          ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView,"rotation",0F,120F);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(animator1).with(animator2);
        animatorSet.play(animator3).after(animator1);
        animatorSet.setDuration(1000);
        animatorSet.start();
    }

5) 动画事件监听

public void moveOnclick(View view) {
        /*
         * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
         * */
        ObjectAnimator animator =  ObjectAnimator.ofFloat(imageView,"translationX",0F,250F);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                Log.i(TAG,"动画开始");
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                Log.i(TAG,"动画结束");
            }

            @Override
            public void onAnimationCancel(Animator animator) {
                Log.i(TAG,"动画删除");
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                Log.i(TAG,"动画重复播放");
            }
        });
        animator.setDuration(1000);
        animator.start();
    }

或者:可以加载一个监听函数

public void moveOnclick(View view) {
        /*
         * 移动的对像,改变对象的属性,属性值的变化范围。变化的时间,开始动画
         * */
        ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 250F);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Log.i(TAG,"动画结束");
            }
        });
    }

待续····


举报

相关推荐

0 条评论