• android 学习随笔二十六(动画:属性动画)


    • 属性动画,属性动画是真正改变对象的某个属性的值

    * 补间动画,只是一个动画效果,组件其实还在原来的位置上,xy没有改变
    1、位移
    * 第一个参数target指定要显示动画的组件
    * 第二个参数propertyName指定要改变组件的哪个属性
    * 第三个参数values是可变参数,就是赋予属性的新的值
    * 传入0,代表x起始坐标:当前x + 0
    * 传入100,代表x终点坐标:当前x + 100

    //具有get、set方法的成员变量就称为属性
    ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;

    package com.itheima.propertyanimator;
    
    import android.R.animator;
    import android.os.Bundle;
    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.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.animation.TranslateAnimation;
    import android.widget.ImageView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        private ImageView iv;
        private ObjectAnimator oa1;
        private ObjectAnimator oa2;
        private ObjectAnimator oa3;
        private ObjectAnimator oa4;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            iv = (ImageView) findViewById(R.id.iv);
            iv.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "点我上miss", 0).show();
                }
            });
        }
    
        public void translate(View v){
    //        TranslateAnimation ta = new TranslateAnimation(-100, 100, 0, 0);
    //        ta.setDuration(2000);
    //        ta.setFillAfter(true);
    //        iv.startAnimation(ta);
            //创建属性动画师
            //arg0:要操作的对象
            //arg1:要修改的属性的名字
            
            oa1 = ObjectAnimator.ofFloat(iv, "translationX", 0, 70, 30, 100);
            oa1.setDuration(2000);
            oa1.setRepeatCount(1);
            oa1.setRepeatMode(ValueAnimator.REVERSE);
            oa1.start();
        }
        
        public void scale(View v){
            oa2 = ObjectAnimator.ofFloat(iv, "scaleX", 0.2f, 2, 1, 2.5f);
            oa2.setDuration(2000);
            oa2.setRepeatCount(1);
            oa2.setRepeatMode(ValueAnimator.REVERSE);
            oa2.start();
        }
        
        public void alpha(View v){
            oa3 = ObjectAnimator.ofFloat(iv, "alpha", 0.2f, 1);
            oa3.setDuration(2000);
            oa3.setRepeatCount(1);
            oa3.setRepeatMode(ValueAnimator.REVERSE);
            oa3.start();
        }
        
        public void rotate(View v){
            oa4 = ObjectAnimator.ofFloat(iv, "rotation", 0, 360, 180, 720);
            oa4.setDuration(2000);
            oa4.setRepeatCount(1);
            oa4.setRepeatMode(ValueAnimator.REVERSE);
            oa4.start();
        }
        
        public void fly(View v){
            //创建动画师集合
            AnimatorSet set = new AnimatorSet();
            //排队飞
    //        set.playSequentially(oa1, oa2, oa3, oa4);
            //一起飞
            set.playTogether(oa1, oa2, oa3, oa4);
            //设置属性动画师操作的对象
            set.setTarget(iv);
            set.start();
        }
        
        public void xml(View v){
            //使用动画师填充器把xml资源文件填充成属性动画对象
            Animator animator = AnimatorInflater.loadAnimator(this, R.animator.property_animation);
            animator.setTarget(iv);
            animator.start();
        }
    
    }
    MainActivity
    <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_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="播放xml定义的动画"
        android:onClick="xml"
        android:layout_below="@id/ll"
        />
        <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>
    activity_main

     animatorproperty_animation.xml

    <?xml version="1.0" encoding="utf-8"?>
    <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
        android:propertyName="translationY"
        android:duration="2000"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:valueFrom="0"
        android:valueTo="100"
        
        >
    
    </objectAnimator>
    property_animation

    2、缩放:
    * 第三个参数指定缩放的比例
    * 0.1是从原本高度的十分之一开始
    * 2是到原本高度的2倍结束

    ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
    3、透明:
    * 透明度,0是完全透明,1是完全不透明

    ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
    4、旋转
    * rotation指定是顺时针旋转
    * 20是起始角度
    * 270是结束角度

    ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
    * 属性指定为rotationX是竖直翻转
    * 属性指定为rotationY是水平翻转

    ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);

    5、可变参数
    * 第三个参数可变参数可以传入多个参数,可以实现往回位移(旋转、缩放、透明)

    ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;

    6、所有动画一起飞

    //创建动画师集合
    AnimatorSet set = new AnimatorSet();
    //设置要播放动画的组件
    set.setTarget(bt);
    //所有动画有先后顺序的播放
    //set.playSequentially(oa, oa2, oa3, oa4);
    //所有动画一起播放
    set.playTogether(oa, oa2, oa3, oa4);
    set.start();

  • 相关阅读:
    sublime tex创建可服用的片段
    php unset
    使用VS.NET来制作安装程序
    手机暗码大全.(挺好玩的..)
    【转】双目运算符 "??"
    linuxcp命令
    ios——在iPhone程序中打开其它程序
    IOS地图项目01网络编程很难么???
    C# BitArray 实例
    Windows Phone 8使用初体验
  • 原文地址:https://www.cnblogs.com/ecollab/p/5969732.html
Copyright © 2020-2023  润新知