看到很多开源库都使用了这个动画框架,就自己试了一下,果然很强大。把测试代码贴上,方便以后使用

package com.test.animation;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout; import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.animation.PropertyValuesHolder;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator; public class AnimationActivity extends Activity {
Button button; private void initView() {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL);
button = new Button(this);
button.setText("target");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(button, params);
setContentView(layout);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
// testAnim00();
// testAnim0();
// testAnim1();
// testAnim2();
testAnim3();
// testAnim4();
// testAnim5();
// testAnim6();
// testAnim7();
// startActivity(new Intent(this,AnimationContextMenuActivity.class));
} private void testAnim00() {
Animator animator1 = AnimatorUtils.rotationCloseToRight(button).setDuration(2000);
Animator animator2 = AnimatorUtils.rotationOpenFromRight(button).setDuration(2000);
Animator animator3 = AnimatorUtils.rotationCloseVertical(button).setDuration(2000);
Animator animator4 = AnimatorUtils.rotationOpenVertical(button).setDuration(2000);
Animator animator5 = AnimatorUtils.alphaDisappear(button).setDuration(2000);
Animator animator6 = AnimatorUtils.alphaAppear(button).setDuration(2000);
Animator animator7 = AnimatorUtils.translationRight(button, 100).setDuration(2000);
Animator animator8 = AnimatorUtils.translationLeft(button, 100).setDuration(2000);
final AnimatorSet set = new AnimatorSet();
// set.play(animator1).before(animator2)/*.after(animator3).after(animator4).after(animator5).after(animator6).after(animator7).after(animator8)*/;
// set.play(animator2).before(animator3);
// set.play(animator3).before(animator4);
// set.play(animator4).before(animator5);
// set.play(animator5).before(animator6);
// set.play(animator6).before(animator7);
// set.play(animator7).before(animator8);
/*===============//after before只能有两个,注意 before/after的顺序 或者用下面的===========================*/
set.playSequentially(animator1,animator2,animator3,animator4,animator5,animator6,animator7,animator8);
set.start();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
set.start();
}
});
} private void testAnim0() {
Animator anim1 = ObjectAnimator.ofFloat(button, "rotationX", 90, 0).setDuration(2000);
Animator anim2 = ObjectAnimator.ofFloat(button, "rotationX", 0, 90).setDuration(2000);
final AnimatorSet set = new AnimatorSet();
set.play(anim1).after(anim2);
set.start();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
set.start();
}
});
} private void testAnim1() {
ObjectAnimator.ofFloat(button, "translationY", 100).setDuration(1000).start();
} private void testAnim2() {
Animator animator = ObjectAnimator.ofFloat(button, "translationX", 100).setDuration(1000);
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) { } @Override
public void onAnimationEnd(Animator animator) {
ObjectAnimator.ofFloat(button, "translationX", 0).setDuration(1000).start();
} @Override
public void onAnimationCancel(Animator animator) { } @Override
public void onAnimationRepeat(Animator animator) { }
});
animator.start();
} private void testAnim3() {
ViewPropertyAnimator.animate(button).translationY(100).start();
final ValueAnimator animator = ValueAnimator.ofFloat(0, 360).setDuration(2000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// ViewHelper.setPivotX(button,0);
// ViewHelper.setRotationX(button, (Float) animation.getAnimatedValue());
ViewHelper.setPivotY(button, 0);
ViewHelper.setRotationY(button, (Float) animation.getAnimatedValue()); //配套使用
}
});
animator.start();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator.start();
}
});
} private void testAnim4() {
//左移动100,右移动100,alpha 变为0.2 跟5效果一样
AnimatorSet set = new AnimatorSet();
set.playTogether(ObjectAnimator.ofFloat(button, "translationX", 100),
ObjectAnimator.ofFloat(button, "translationY", 100),
ObjectAnimator.ofFloat(button, "alpha", 1, 0.2f));
set.setDuration(2000).start();
} private void testAnim5() {
//左移动100,右移动100,alpha 变为0.2
ObjectAnimator.ofPropertyValuesHolder(button, PropertyValuesHolder.ofFloat("translationX", -100),
PropertyValuesHolder.ofFloat("translationY", 100),
PropertyValuesHolder.ofFloat("alpha", 1, 0.3f)).setDuration(2000).start(); } private void testAnim6() {
// ViewPropertyAnimator.animate(button).translationX(100).translationY(100).setDuration(2000).start();
// ViewPropertyAnimator.animate(button).translationY(100).rotationYBy(720).setDuration(2000).start();
ViewPropertyAnimator.animate(button).rotationYBy(360).x(300).y(500).setDuration(2000).start();//旋转角度为360的倍数,x(..)y(..)后面为动画后位置
} //===========================================================================demo=========================================================
private void testAnim7() {
final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(Gravity.CENTER_HORIZONTAL); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 0; i < 5; i++) {
Button btn = new Button(this);
btn.setText("btn" + i);
btn.setTag(i);
layout.addView(btn, params);
}
Button startBtn = new Button(this);
startBtn.setText("start");
startBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isAdimateOpen) {
isAdimateOpen = true;
doAnimateOpen(layout.getChildAt(0), 0, 5, 300);
doAnimateOpen(layout.getChildAt(1), 1, 5, 300);
doAnimateOpen(layout.getChildAt(2), 2, 5, 300);
doAnimateOpen(layout.getChildAt(3), 3, 5, 300);
doAnimateOpen(layout.getChildAt(4), 4, 5, 300);
} else {
isAdimateOpen = false;
doAnimateClose(layout.getChildAt(0), 0, 5, 300);
doAnimateClose(layout.getChildAt(1), 1, 5, 300);
doAnimateClose(layout.getChildAt(2), 2, 5, 300);
doAnimateClose(layout.getChildAt(3), 3, 5, 300);
doAnimateClose(layout.getChildAt(4), 4, 5, 300);
}
}
});
layout.addView(startBtn, params);
setContentView(layout);
} boolean isAdimateOpen = false; /**
* 打开菜单的动画
*
* @param view 执行动画的view
* @param index view在动画序列中的顺序
* @param total 动画序列的个数
* @param radius 动画半径
*/
private void doAnimateOpen(View view, int index, int total, int radius) {
if (view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
}
double degree = Math.PI * index / ((total - 1) * 2);
int translationX = (int) (radius * Math.cos(degree));
int translationY = (int) (radius * Math.sin(degree));
AnimatorSet set = new AnimatorSet();
//包含平移、缩放和透明度动画
set.playTogether(
ObjectAnimator.ofFloat(view, "translationX", 0, translationX),
ObjectAnimator.ofFloat(view, "translationY", 0, translationY),
ObjectAnimator.ofFloat(view, "scaleX", 1f, 1.5f),
ObjectAnimator.ofFloat(view, "scaleY", 1f, 1.5f),
ObjectAnimator.ofFloat(view, "alpha", 1f, 1.5f));
//动画周期为500ms
set.setDuration(1000).start();
} /**
* 关闭菜单的动画
*
* @param view 执行动画的view
* @param index view在动画序列中的顺序
* @param total 动画序列的个数
* @param radius 动画半径
*/
private void doAnimateClose(final View view, int index, int total,
int radius) {
if (view.getVisibility() != View.VISIBLE) {
view.setVisibility(View.VISIBLE);
}
double degree = Math.PI * index / ((total - 1) * 2);
int translationX = (int) (radius * Math.cos(degree));
int translationY = (int) (radius * Math.sin(degree));
AnimatorSet set = new AnimatorSet();
//包含平移、缩放和透明度动画
set.playTogether(
ObjectAnimator.ofFloat(view, "translationX", translationX, 0),
ObjectAnimator.ofFloat(view, "translationY", translationY, 0),
ObjectAnimator.ofFloat(view, "scaleX", 1.5f, 1f),
ObjectAnimator.ofFloat(view, "scaleY", 1.5f, 1f),
ObjectAnimator.ofFloat(view, "scaleY", 1.5f, 1f));
set.setDuration(1000).start();
}
}

  

NineOldAndroid开源库简单使用demo的更多相关文章

  1. 开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发

    [原][开源框架]Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位... 时间 2015-01-05 10:08:18 我是程序猿,我为自己代言 原文  http: ...

  2. 【开源框架】Android之史上最全最简单最有用的第三方开源库收集整理,有助于快速开发,欢迎各位网友补充完善

    链接地址:http://www.tuicool.com/articles/jyA3MrU 时间 2015-01-05 10:08:18  我是程序猿,我为自己代言 原文  http://blog.cs ...

  3. Android之史上最全最简单最有用的第三方开源库收集整理

    Android开源库 自己一直很喜欢Android开发,就如博客签名一样, 我是程序猿,我为自己代言 . 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者, ...

  4. 【踩坑速记】开源日历控件,顺便全面解析开源库打包发布到Bintray/Jcenter全过程(新),让开源更简单~

    一.写在前面 自使用android studio开始,就被它独特的依赖方式:compile 'com.android.support:appcompat-v7:25.0.1'所深深吸引,自从有了它,麻 ...

  5. GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    若有任何疑问可通过邮件或微博联系我 项目名称 项目简介 1. react-native 这个是 Facebook 在 React.js Conf 2015 大会上推出的基于 JavaScript 的开 ...

  6. GitHub开源库排名一百的简单介绍,值得收藏!

    GitHub Android Libraries Top 100 简介 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub ...

  7. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

  8. Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作

    我们做listview左右滑动操作时,一般中情况下,都是像QQ那样,左滑弹出操作菜单(删除.编辑),然后选择菜单操作: 这样的效果不可谓不好,算是非常经典. 另外,有少数的APP,尤其是任务管理类的A ...

  9. 开源库Magicodes.WeChat.SDK总体介绍

    目录 1    概要    1 2    主要特点    2 3    架构图    8 3.1    构造器——WeChatSDKBuilder    8 3.2    函数管理器——WeChatF ...

随机推荐

  1. hadoop运行原理之Job运行(一) JobTracker启动及初始化

    这部分的计划是这样的,首先解释JobTracker的启动过程和作业从JobClient提交到JobTracker上:然后分析TaskTracker和heartbeat:最后将整个流程debug一遍来加 ...

  2. Jenkins+Jmeter+Ant 接口持续集成(转)

    来源:https://testerhome.com/topics/5186 为什么要用Jmeter做接口测试 当选择这套方案的时候,很多人会问,为什么选择Jmeter做Case管理?为什么不自己写框架 ...

  3. Android 每次访问网络时,都需要判断是否有网络

    /** * 在执行网络操作之前判断网络是否链接可用 * * @return true 可用 false 不可用 */ private boolean isOnline() { Connectivity ...

  4. 4、时间同步ntp服务的安装于配置(作为客户端的配置)

    yum安装ntpd服务   .yum -y install ntp ntpdate (安装时间同步ntp服务) . vi /etc/ntp.conf (修改ntpd服务的配置文件)   3.修改配置文 ...

  5. hadoop map-red的执行过程

    hadoop的 map-red就是一个并行计算平台,我们在使用这个平台的时候,要做的事情就是提交自己定制的任务(job,主要定制map类,reduce类,combine类等类),然后设置job的各种参 ...

  6. mysql触发器使用

    触发器 简要 1.触发器基本概念 2.触发器语法及实战例子 3.before和after区别 一.触发器基本概念 1.一触即发 2.作用: 监视某种情况并触发某种操作 3.观察场景 一个电子商城: 商 ...

  7. UE4 C++ 使用FTimeLine/FTime 实例 Actor moving faster than Timeline

    https://answers.unrealengine.com/questions/313698/timeline-issues.html   https://docs.unrealengine.c ...

  8. 【转】Styling And Animating SVGs With CSS

    原文转自:http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/?utm_source=CSS- ...

  9. 导出带图形的数据excel表

    public static string StatisticsSR(string parmStr) { try { StatisticsSRInfo parm = JsonConvert.Deseri ...

  10. 十分钟了解分布式计算:Petuum

    Petuum是一个机器学习专用分布式计算框架,本文介绍其架构,并基于文章 More Effective Distributed ML via a Stale Synchronous Parallel ...