Android中属性动画1----ObjectAnimator的基本使用(平移,缩放,渐变,旋转)

来源:互联网 发布:阿里云 邮件超时 编辑:程序博客网 时间:2024/06/05 14:46

 属性动画ObjectAnimator和补间动画的区别是,补间动画只是表面上实现了平移,旋转,渐变,缩放,实际上属性值不变;

 属性动画实现平移,旋转,渐变,缩放后,属性值变了

主要代码:

package com.zhh.android;import android.animation.AnimatorSet;import android.animation.ObjectAnimator;import android.animation.PropertyValuesHolder;import android.animation.ValueAnimator;import android.app.Activity;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.support.v7.app.WindowDecorActionBar;import android.util.Log;import android.view.View;import android.view.animation.AnimationSet;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;/** * ObjectAnimator * 平移,缩放,渐变,旋转基本使用 * 参考视频 */public class MainActivity extends Activity {    private ImageView imageView;    private Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        myOnclick();    }    /**     * 初始化控件     */    private void initView() {        imageView = (ImageView)findViewById(R.id.imageView);        button = (Button)findViewById(R.id.button);    }    /**     * 点击事件     */    private void myOnclick() {//      按钮点击事件        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                setAnimate4();            }        });//      图片点击事件        imageView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {              startActivity(new Intent(MainActivity.this,Main2Activity.class));            }        });    }    /**     * 属性动画     * 平移     */    private void setAnimate1(){//      imageView中凡是有get,set方法的属性,都可以通过属性动画操作//      创建属性动画对象,并设置移动的方向和偏移量//      translationX是imageview的平移属性        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);//      设置移动时间        objectAnimator.setDuration(1000);//      开始动画        objectAnimator.start();    }    /**     * 属性动画     * 旋转     */    private void setAnimate2(){//      创建属性动画对象,并设置移动的方向和偏移量//      rotation是imageView的旋转属性        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);//      设置移动时间        objectAnimator.setDuration(1000);//      开始动画        objectAnimator.start();    }    /**     * 属性动画     * 渐变     */    private void setAnimate3(){//      创建属性动画对象,并设置移动的方向和偏移量//      rotation是imageView的旋转属性        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha", 0f, 1f);//      设置移动时间        objectAnimator.setDuration(1000);//      开始动画        objectAnimator.start();    }    /**     * 属性动画     * 缩放     */    private void setAnimate4(){        // 将一个TextView沿垂直方向先从原大小(1f)放大到5倍大小(5f),然后再变回原大小。        ObjectAnimator anim = ObjectAnimator.ofFloat(imageView, "scaleY", 1f, 5f, 1f);        anim.setDuration(5000);        // 正式开始启动执行动画        anim.start();    }    /**     * 属性动画     * 多种效果,同时使用     * 一般用这种     */    private void setAnimate5(){        ObjectAnimator objectAnimatorX = ObjectAnimator.ofFloat(imageView, "translationX", 0f, 200f);        ObjectAnimator objectAnimatorY = ObjectAnimator.ofFloat(imageView, "translationY", 0f, 200f);        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", 0f, 360f);        AnimatorSet animatorSet = new AnimatorSet();//      同时播放//        animatorSet.playTogether(objectAnimatorX,objectAnimatorY,objectAnimator);//      依次播放//        animatorSet.playSequentially(objectAnimatorX,objectAnimatorY,objectAnimator);//      控制顺序,先平移再旋转//      水平方向上平移和竖直方向平移同时进行        animatorSet.play(objectAnimatorX).with(objectAnimatorY);//      平移完成之后再旋转        animatorSet.play(objectAnimator).after(objectAnimatorX);        animatorSet.setDuration(1000);        animatorSet.start();    }    /**     * 属性动画     * 多种效果,同时使用     * 不常用     */    private void setAnimate6(){//      旋转        PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);//      x轴方向平移        PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("translationX", 0f, 200f);//      y轴方向平移        PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("translationY", 0f, 200f);        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(imageView, propertyValuesHolder1, propertyValuesHolder2, propertyValuesHolder3);//      设置间隔时间        objectAnimator.setDuration(1000);//      开始动画        objectAnimator.start();    }}
activity_main.xml中

<?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.zhh.android.MainActivity"    >    <ImageView        android:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher"        />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerInParent="true"        android:text="动画"        /></RelativeLayout>
参考视频:

http://www.imooc.com/learn/263

源码下载:
http://download.csdn.net/download/zhaihaohao1/10126123


阅读全文
0 0
原创粉丝点击