浅谈属性动画的简单使用(一)
来源:互联网 发布:手机淘宝怎么改运费 编辑:程序博客网 时间:2024/05/16 03:35
今天,我们来谈下属性动画的使用,大家都知道在android中有两大动画:一种是补间动画,一种则是属性动画.使用过属性动画的人应该就知道它是非常强大,比补间动画更加强大.使用过它的人应该是更加爱不释手.因为属性动画,高度封装,扩展性灵活,使用简单.补间动画能做的,属性动画都能做,还有一个很大特点就是补间动画和属性动画最大的区别就是:补间动画不具备任何交互性,而属性动画具备与用户的交互性.至于这个区别后面我会用一个简单的例子来证明.
1,属性动画和补间动画的最大的区别在于:属性动画具备与用户交互性,而补间动画不具备与用户交互性的特点,补间动画一般只是为了展示一个简单的动画,而用户不具备对该动画操作性.为什么呢??大家都很清楚,补间动画的原理就是通过不断重新绘制自己从而达到一个动画的效果,实际上在不断重绘过程中,会有很多不同的View产生,最后的View不再是原来的那个View了,所以你对原来View一些交互性操作,对于最后的View不起作用,而属性动画则不同,它是从实际和本质上去改变动画的属性,而不是重绘自己达到某种动画的效果,那么也就是原来的View还是原来的View,只是改变其动画的属性从而达到实现某种动画的效果,自然原来的View的一些交互性的操作,到最后的时候那些交互性肯定依然保留.
简单的例子来证明补间动画不具备用户的交互性,而属性动画具备用户的交互性:我们给一个ImageView添加一个点击事件,然后弹出一个Toast,分别用补间动画和属性动画来实现这个ImageView在x轴方向上平移动画.
package com.mikyou.simpletest;import android.animation.ObjectAnimator;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener{private Button mButton;private ImageView mImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mButton=(Button) findViewById(R.id.id_button);mImageView=(ImageView) findViewById(R.id.id_imageview);mButton.setOnClickListener(this);mImageView.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.id_button: startAnim();break;case R.id.id_imageview:Toast.makeText(MainActivity.this, "您点击了ImageView", 0).show();break;default:break;}}private void startAnim() {/*//1,使用补间动画TranslateAnimation mAnimation=new TranslateAnimation(0, 300, 0, 0);mAnimation.setDuration(1000);mAnimation.setFillAfter(true);mImageView.startAnimation(mAnimation);*///2,使用属性动画ObjectAnimator animator=ObjectAnimator.ofFloat(mImageView, "translationX", 0f,300f);animator.setDuration(1000);animator.start();}}
通过以上的小例子,应该对属性动画的原理有了简单的了解了吧,那接下来让我们一起来看看属性动画的基本使用:
第一个重要的类:ObjectAnimator
1、ObjectAnimator
* 属性动画ObjectAnimator
ofFloat方法:
第一个参数:操作的对象
第二个参数:操作的属性(只要这个属性具有set,get方法那么就可以使用)
补充:常用的属性:
translationX,translationY
rotation,rotationX,rotationY
scaleX,scaleY
X,Y(具体移动到某个点)
alpha
第三个参数:运动的起点
第四个参数:运动的终点
常用的方法和类
ValueAnimator
ObjectAnimator
AnimatorUpdateListener
初步的ObjectAnimator使用:
//多个动画结合在一起,动画是同时执行的,start后每个动画都是一个异步的过程
/* ObjectAnimator.ofFloat(mImageView, "translationY", 0f,400f).setDuration(1000).start();
ObjectAnimator.ofFloat(mImageView, "translationX", 0f,400f).setDuration(1000).start();
ObjectAnimator.ofFloat(mImageView, "rotation", 0f,1800f).setDuration(1000).start();
ObjectAnimator.ofFloat(mImageView, "scaleX", 1f,2f).setDuration(1000).start();
ObjectAnimator.ofFloat(mImageView, "scaleY", 1f,2f).setDuration(1000).start();*/
进一步的ObjectAnimator的使用,PropertyValuesHolder效率更高:
/*PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation", 0,360f);
PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX", 0,300f);
PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY", 0,400f);
ObjectAnimator.ofPropertyValuesHolder(mImageView, p1,p2,p3).setDuration(1000).start();*/
最后的ObjectAnimator的使用,AnimatorSet的使用灵活:
animatorSet的API提供强大控制动画的功能
* set.playTogether(animator1,animator2,animator3);//一起开始动画
* set.playSequentially(animator1,animator2,animator3);//按顺序依次播放
*set.play(animator1).with(animator2);
*set.play(animator3).after(animator2);//先让它同时在X,Y轴方向上平移,平移完后就开始旋转
***************************************************************************************************
ObjectAnimator animator1=ObjectAnimator.ofFloat(mImageView, "translationY", 0f,400f);
ObjectAnimator animator2=ObjectAnimator.ofFloat(mImageView, "translationX", 0f,400f);
ObjectAnimator animator3=ObjectAnimator.ofFloat(mImageView, "rotation", 0f,360f);
AnimatorSet set=new AnimatorSet();
//set.playTogether(animator1,animator2,animator3);//一起开始动画
//set.playSequentially(animator1,animator2,animator3);//按顺序依次播放
/* set.play(animator1).with(animator2);
set.play(animator3).after(animator2);*///先让它同时在X,Y轴方向上平移,平移完后就开始旋转
set.setDuration(1000);
set.start();
2、属性动画的监听事件:
/**
* @author mikyou
* 设置动画的监听事件
* addListener
* */
//重写指定的回调方法,通过AnimatorListenerAdapter类来指定自己想实现的方法
mAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
Toast.makeText(MainActivity.this, "animation End", 0).show();
}
});
//重写所有的回调方法
/*mAnimator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Toast.makeText(MainActivity.this, "animation End", 0).show();
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
});*/
到这里属性的简单的使用,还有一些具体用法会在接下来的案例中穿叉讲解.
- 浅谈属性动画的简单使用(一)
- 属性动画的简单使用
- 属性动画的简单使用
- 浅谈属性动画简单使用之实现卫星菜单(二)
- 浅谈属性动画简单使用之实现爱的贝塞尔曲线浪漫告白效果(三)
- 动画概述 一,创建一个简单的属性动画
- CABasicAnimation(基础属性动画)的简单使用
- 属性动画的简单使用和总结
- Android 属性动画的简单使用
- 使用属性动画实现一个简单的加载动画
- 简单的属性动画
- android动画的简单使用(一)
- Animator 属性动画简单使用
- 几个简单动画属性使用
- 属性动画的使用
- 属性动画的使用
- 属性动画的使用
- 属性动画的使用
- 免费开源!仿微信仿陌陌类APP源代码整项目开源,包括ADT项..
- 腾讯前端面试
- 2016/3/27 - 记
- 正则表达式和Jquery
- EasyUI表单验证
- 浅谈属性动画的简单使用(一)
- 2016sdau课程练习专题一 1008 problemI
- iOS UIDocumentInteractionController 第三方打开详解
- Java开发微信公众号-接口测试帐号接口配置及Java源代码
- 用C语言写的一个小的银行管理程序
- 阿里2016实习offer五面经验与总结
- 【反省】还在犹豫什么?
- The imported target "Qt5::Core" references the file
- 第5周-项目1-三角形类锥形(1)