安卓(属性动画)

Property Animation(属性动画)

效果图

在这里插入图片描述

布局文件

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="bw.com.Unit2.PropertyActivity">

    <Button
        android:id="@+id/alpha_btn_property"
        android:text="alpha动画(透明)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/scale_btn_property"
        android:text="rotate动画(拉伸)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/rotate_btn_property"
        android:text="rotate动画(旋转)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/translate_btn_property"
        android:text="translate动画(位移)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/color_btn_property"
        android:text="color动画(改变颜色)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/set_btn_property"
        android:text="set动画(集合)"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    
    <TextView
        android:id="@+id/property_tv"
        android:text="1702C"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        />

</LinearLayout>

Java代码

// An highlighted block
package bw.com.Unit2;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import bw.com.Unit1.R;

@ContentView(R.layout.activity_property)
public class PropertyActivity extends AppCompatActivity {

    @ViewInject(R.id.property_tv)
    private TextView textView;

    private int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        x.view().inject(this);

    }

    @Event({R.id.alpha_btn_property,R.id.scale_btn_property,R.id.rotate_btn_property,R.id.translate_btn_property,
            R.id.property_tv,R.id.color_btn_property,R.id.set_btn_property})
    private void click(View view){
        switch (view.getId()){
            case R.id.alpha_btn_property:
                alphaProperty();
                break;
            case R.id.scale_btn_property:
                scaleProperty();
                break;
            case R.id.rotate_btn_property:
                rotateProperty();
                break;
            case R.id.translate_btn_property:
                transcaleProperty();
                break;
            case R.id.property_tv:
                Toast.makeText(this, "我被点击了!", Toast.LENGTH_SHORT).show();
                break;
            case R.id.color_btn_property:
                colorProperty();
                break;
            case R.id.set_btn_property:
                setProperty();
                break;
        }
    }
    //属性动画的集合
    private void setProperty() {

//        ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(textView,"backgroundColor", Color.BLUE,Color.RED);
//        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView,"translationY",0,100);
//        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(textView,"scaleX",1f,3f);
//        ObjectAnimator objectAnimator4 = ObjectAnimator.ofFloat(textView,"rotation",0,360);
//        ObjectAnimator objectAnimator5 = ObjectAnimator.ofFloat(textView,"alpha",1,0);
//
//        //属性动画集合
//        AnimatorSet animatorSet = new AnimatorSet();
//
//        animatorSet.setDuration(5000);
//        //play 开始执行属性动画  with和平移的同时执行  before在play之前的动画执行  after是在play之后
//        animatorSet.play(objectAnimator1).with(objectAnimator2).after(objectAnimator3).after(objectAnimator4).after(objectAnimator5);
//        animatorSet.start();

        //属性存储器:保存属性动画的属性
        PropertyValuesHolder propertyValuesHolderC = PropertyValuesHolder.ofInt("backgroundColor", Color.BLUE,Color.RED);
        PropertyValuesHolder propertyValuesHolderT = PropertyValuesHolder.ofFloat("translationY", 0,200);
        PropertyValuesHolder propertyValuesHolderS = PropertyValuesHolder.ofFloat("scaleX", 1f,3f);
        PropertyValuesHolder propertyValuesHolderR = PropertyValuesHolder.ofFloat("rotation", 0,360);
        PropertyValuesHolder propertyValuesHolderA = PropertyValuesHolder.ofFloat("alpha", 1,0);

        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(textView,propertyValuesHolderC,propertyValuesHolderT
                ,propertyValuesHolderS,propertyValuesHolderR,propertyValuesHolderA
        );

        objectAnimator.setDuration(5000);

        objectAnimator.start();

//        //拓展
//        final ValueAnimator valueAnimator = ValueAnimator.ofObject(new MyTypeEvaluator(),1f,20f);
//        //值动画添加更新的监听
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                //获取动画操作属性的值
//                Float value = (Float) valueAnimator.getAnimatedValue();
//                //浮点的每一次  都打印
//                Log.e("###",value+"");
//            }
//        });
//
//        valueAnimator.start();
    }

    //设置背景颜色的属性 动画
    private void colorProperty() {

        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(textView,"backgroundColor", Color.BLUE,Color.RED);

        objectAnimator.setDuration(5000);

//        objectAnimator.setRepeatCount(3);

        objectAnimator.start();

    }

    //位移动画
    private void transcaleProperty() {

        //获取控件相对于y轴位移
        float y = textView.getTranslationY();

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationY",y,y+100);

        objectAnimator.setDuration(3000);

        objectAnimator.start();

    }

    //缩放属性动画
    private void scaleProperty() {


        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"scaleY",1f,3f);
        //执行时间
        objectAnimator.setDuration(3000);

        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //恢复原样
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"scaleX",3f,1f);
                objectAnimator.setDuration(3000);
                objectAnimator.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        objectAnimator.start();
    }

    //旋转属性动画
    private void rotateProperty() {

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"rotation",0,360);

        objectAnimator.setDuration(3000);

        objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
        //插值器
        objectAnimator.setInterpolator(new AnticipateInterpolator());

        objectAnimator.start();

    }

    //加载XML文件  实现属性动画
    private void alphaProperty() {
        //创建属性动画
        ValueAnimator valueAnimator = (ValueAnimator) AnimatorInflater.loadAnimator(PropertyActivity.this,R.animator.alpha);
        //动画属性关联view
        valueAnimator.setTarget(textView);

        //执行时间  毫秒
        valueAnimator.setDuration(3000);
        //重复次数
//        valueAnimator.setRepeatCount(3);

        //设置监听
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                Toast.makeText(PropertyActivity.this, "开始", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"alpha",0,1);
                objectAnimator.setDuration(1000);
                objectAnimator.start();
                Toast.makeText(PropertyActivity.this, "结束", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                count++;
                Toast.makeText(PropertyActivity.this, "执行第"+count+"次", Toast.LENGTH_SHORT).show();
            }
        });

        //启动属性动画
        valueAnimator.start();

    }
}

R.animator.alpha文件

// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="Alpha"
    android:valueFrom="1"
    android:valueTo="0"
    android:fillBefore="true"
    >

</objectAnimator>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值