Android Studio3.0后起的四种属性动画

1.首先请看大屏幕

2.先布置Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff">

    <ImageView
        android:id="@+id/img"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:src="@drawable/timg" />

    <ImageView
        android:id="@+id/img1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/img"
        android:layout_marginEnd="30dp"
        android:layout_marginRight="30dp"
        android:src="@drawable/timg1" />


</RelativeLayout>
3.MainActiviity里的方法

package com.example.cartoon;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private ImageView img1;

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

        img = findViewById(R.id.img);
        img1 = findViewById(R.id.img1);
        //参数1:表示对其进行动画操作的View
        //平移
        ObjectAnimator translationAnim = ObjectAnimator.ofFloat(img, "translationX", 2f, 800f,0f);
        translationAnim.setDuration(5000);
        translationAnim.start();

        //旋转
        ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(img, "rotationY", 9f, 180f);
        rotationAnim.setDuration(5500);
        rotationAnim.start();

        //渐变
        ObjectAnimator alphaAnim = ObjectAnimator.ofFloat(img, "alpha", 0f, 3f);
        alphaAnim.setDuration(10000);
        //alphaAnim.setRepeatCount(1);//设置重复播放的次数
        //alphaAnim.setRepeatMode(ObjectAnimator.); 设置播放的模式
        alphaAnim.start();


        //渐变
        ObjectAnimator alphaAnim1 = ObjectAnimator.ofFloat(img1, "alpha", 0f, 3f);
        alphaAnim1.setDuration(30000);
        //alphaAnim.setRepeatCount(1);//设置重复播放的次数
        //alphaAnim.setRepeatMode(ObjectAnimator.); 设置播放的模式
        alphaAnim1.start();

        //缩放
        ObjectAnimator scaleAnim = ObjectAnimator.ofFloat(img, "scaleX", 1f, 2f, 1f);
        scaleAnim.setDuration(6000);
        scaleAnim.start();
        //缩放
        ObjectAnimator scaleAnim1 = ObjectAnimator.ofFloat(img, "scaleY", 1f, 2f, 1f);
        scaleAnim1.setDuration(6000);
        scaleAnim1.start();

        //旋转
        ObjectAnimator rotationAnim1 = ObjectAnimator.ofFloat(img1, "rotationY", 9f, 180f);
        rotationAnim1.setDuration(0);
        rotationAnim1.start();

        ObjectAnimator rotationAnimX = ObjectAnimator.ofFloat(ivIcon, "rotation", 0f, 360f);
        ObjectAnimator translationAnimX = ObjectAnimator.ofFloat(ivIcon, "translationX", 0f, 300f,0f);

        //ObjectAnimator translationAnimX = ObjectAnimator.ofFloat(ivIcon, "translationX", 0f, 300f,0f);


        //创建组合动画对象
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(rotationAnimX).with(translationAnimX);
        animatorSet.setDuration(3000);
        animatorSet.start();
    }

}
4.欲练此功必先自宫。


5.若不自宫也可成功


猜你喜欢

转载自blog.csdn.net/wyh_file/article/details/79102690