属性动画--动画的方法+全部+图片

主页面

xml实现有点bug,有些动画一直执行

package com.example.lx_shuxingdonghua;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
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 Button mpy;
    private Button mxz;
    private Button mjb;
    private Button msf;
    private ImageView mimg;
    private Button myq;
    private Button mpwx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        initView();
    }

    private void initView() {
        mpy = findViewById(R.id.py);
        mxz = findViewById(R.id.xz);
        mjb = findViewById(R.id.jb);
        msf = findViewById(R.id.sf);
        myq = findViewById(R.id.yq);
        mpwx = findViewById(R.id.pwx);
        mimg = findViewById(R.id.img);
        mjb.setOnClickListener(this);
        mpy.setOnClickListener(this);
        msf.setOnClickListener(this);
        mxz.setOnClickListener(this);
        myq.setOnClickListener(this);
        mimg.setOnClickListener(this);
    }
    //用xml文件实现属性动画
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.py:
                Animator animator5 = AnimatorInflater.loadAnimator(this, R.animator.translation);
                animator5.setTarget(mimg);
                animator5.start();
                break;
            case R.id.sf:
                Animator animator1 = AnimatorInflater.loadAnimator(this, R.animator.fangda);
                animator1.setTarget(mimg);
                animator1.start();
                break;
            case R.id.jb:
                Animator animator4 = AnimatorInflater.loadAnimator(this, R.animator.rotation);
                animator4.setTarget(mimg);
                animator4.start();
                break;
            case R.id.xz:
                Animator animator = AnimatorInflater.loadAnimator(this, R.animator.alpha);
                animator.setTarget(mimg);
                animator.start();
                break;
            case R.id.yq:
                Animator animator2 = AnimatorInflater.loadAnimator(this, R.animator.quanbu);
                animator2.setTarget(mimg);
                animator2.start();
                break;
            case R.id.img:
                Animator animator3 = AnimatorInflater.loadAnimator(this, R.animator.quanbu);
                animator3.setTarget(mimg);
                animator3.start();
                break;
        }
    }
//直接在代码中实现属性动画的所有动画
//    @Override
//    public void onClick(View v) {
//        switch (v.getId()){
//            case R.id.py:
//                //图片平移出去弹不回来
////                ObjectAnimator translationX = ObjectAnimator.ofFloat(mimg, "translationX", 200f);
////                ObjectAnimator translationY = ObjectAnimator.ofFloat(mimg, "translationY", 200f);
//
//
//                //图片平移出去弹回来
//                //如果没有mimg.getTranslationX()则图片不会弹回来
//                ObjectAnimator translationX = ObjectAnimator.ofFloat(mimg, "translationX",mimg.getTranslationX(), 200f,mimg.getTranslationX());
//                ObjectAnimator translationY = ObjectAnimator.ofFloat(mimg, "translationY",mimg.getTranslationY(), 200f,mimg.getTranslationY());
//                //动画时间
//                translationX.setDuration(3000);
//                translationY.setDuration(3000);
//                //执行动画
//                translationY.start();
//                translationX.start();
//                break;
//            case R.id.xz:
//                //旋转
//                ObjectAnimator rotation = ObjectAnimator.ofFloat(mimg, "rotation", 0.0f, 360f);
//                rotation.setDuration(3000);
//                rotation.start();
//                break;
//            case R.id.sf:
//                //缩放
//                ObjectAnimator scaleX = ObjectAnimator.ofFloat(mimg, "scaleX", 1.0f, 0.0f, 2.0f, 1.0f);
//                scaleX.setDuration(3000);
//                scaleX.start();
//                break;
//            case R.id.jb:
//                //透明
//                ObjectAnimator alpha = ObjectAnimator.ofFloat(mimg, "alpha", 0.0f, 1.0f);
//                alpha.setDuration(3000);
//                alpha.start();
//                break;
//                //抛物线
//            case R.id.pwx:
////                ObjectAnimator translationXp = ObjectAnimator.ofFloat(mimg, "translationX", 200f);
////                ObjectAnimator translationYp = ObjectAnimator.ofFloat(mimg, "translationY",20f,40f,60f,80f,100f,120f,140f,160f,180f, 200f);
////                //动画时间
////                translationXp.setDuration(3000);
////                translationYp.setDuration(3000);
////                //执行动画
////                translationXp.start();
////                translationYp.start();
//                break;
//            case R.id.yq:
//                //执行全部动画
//               getquanbudonghua();
//                break;
//            case R.id.img:
//                //图片的点击事件
//                getquanbudonghua();
//                Toast.makeText(this,"执行所有动画,并倒着执行一遍",Toast.LENGTH_SHORT).show();
//                break;
//        }
//    }

    //实现全部动画方法
    public void getquanbudonghua() {
        PropertyValuesHolder alpha1 = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
        PropertyValuesHolder scaleX1 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0f, 1f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0f, 1f);
        PropertyValuesHolder rotation1 = PropertyValuesHolder.ofFloat("rotation", 0.0f, 360f);
        PropertyValuesHolder translationX1 = PropertyValuesHolder.ofFloat("translationX",200f);
        PropertyValuesHolder translationY1 = PropertyValuesHolder.ofFloat("translationY",200f);
        PropertyValuesHolder translationX2 = PropertyValuesHolder.ofFloat("translationX",0f);
        PropertyValuesHolder translationY2 = PropertyValuesHolder.ofFloat("translationY",0f);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(mimg, alpha1, scaleX1, scaleY, rotation1, translationX1, translationY1);
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofPropertyValuesHolder(mimg, alpha1, scaleX1, scaleY, rotation1, translationX2, translationY2);
        //可以直接执行,不过不能拼接动画,这是组合动画
        //ObjectAnimator.ofPropertyValuesHolder(mimg, alpha1, scaleX1, scaleY, rotation1, translationX1, translationY1).setDuration(3000).start();
        //实例化AnimatorSet
        AnimatorSet animatorSet = new AnimatorSet();
        //使用play方法把两个动画拼接起来
        animatorSet.play(objectAnimator1).after(objectAnimator);
        //时间
        animatorSet.setDuration(3000);
        //开始执行
        animatorSet.start();
        return ;
    }


}

布局

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.lx_shuxingdonghua.MainActivity">
<ImageView
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_gravity="center"
    android:id="@+id/img"
    android:src="@drawable/qwe"/>

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

        <Button
            android:id="@+id/xz"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="旋转" />

        <Button
            android:id="@+id/py"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="平移" />

        <Button
            android:id="@+id/sf"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="缩放" />

        <Button
            android:id="@+id/jb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="渐变" />
        <Button
            android:id="@+id/yq"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="一起执行" />
    </LinearLayout>
</RelativeLayout>

xml文件中的代码

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:propertyName="rotation"
    android:valueFrom="0.0f"
    android:valueTo="360.0f"
    android:valueType="floatType"/>
<?xml version="1.0" encoding="utf-8"?>
<!--
横纵同时放大,若只是放大一次就定义一个就可以了
-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="3000"
        android:propertyName="scaleX"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="2.0"
        android:valueType="floatType"/>

    <objectAnimator
        android:duration="3000"
        android:propertyName="scaleY"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="2.0"
        android:valueType="floatType"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"

android:duration="3000"
android:propertyName="alpha"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueFrom="1.0f"
android:valueTo="0.0"
android:valueType="floatType"
    >

</objectAnimator>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">

    <objectAnimator
        android:duration="3000"
        android:propertyName="translationX"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="200"
        android:valueType="floatType"/>

    <objectAnimator
        android:duration="3000"
        android:propertyName="translationY"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="100"
        android:valueType="floatType"/>

</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="sequentially" >

    <objectAnimator
        android:duration="3000"
        android:propertyName="rotation"
        android:valueFrom="0.0f"
        android:valueTo="360.0f"
        android:valueType="floatType"/>

    <objectAnimator
        android:duration="3000"
        android:propertyName="scaleX"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="2.0"
        android:valueType="floatType"/>

    <objectAnimator
        android:duration="3000"
        android:propertyName="scaleY"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="2.0"
        android:valueType="floatType"/>
    <objectAnimator

        android:duration="3000"
        android:propertyName="alpha"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="1.0f"
        android:valueTo="0.0"
        android:valueType="floatType"
        >

    </objectAnimator>
    <objectAnimator
        android:duration="3000"
        android:propertyName="translationX"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="200"
        android:valueType="floatType"/>

    <objectAnimator
        android:duration="3000"
        android:propertyName="translationY"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="100"
        android:valueType="floatType"/>
</set>

阅读更多
换一批

没有更多推荐了,返回首页