NineOldAndroids动画,如游戏币增减效果

自Android 3.0以上的版本,SDK新增了一个android.animation包,里面的类都是跟动画效果实现相关的,通过Honeycomb API,能够实现非常复杂的动画效果。但如果开发者想在3.0以下的版本中也能使用到这套API,那么Nine Old Androids就会是你最好的选择,该API和Honeycomb API完全一样,只是改变了你使用com.nineoldandroids.XXX的入口。

该项目包含两个工程,一个是Library,即为动画效果的实现库,另一个则是Sample,是对如何使用该API的演示。开发者可以直接登陆Google Play下载安装Nine Old Androids Sample,查看演示。

 

github地址:https://github.com/JakeWharton/NineOldAndroids

官网:http://nineoldandroids.com/ 

 

 

这里借助这个工具包,可以实现游戏吃了什么后,金币增加的上升消失的提示动画,代码非常简单:

(1)主界面:

package com.example.nineoldandroidstest;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.Animator.AnimatorListener;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

public class MainActivity extends Activity {

	private TextView textView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		textView = (TextView) findViewById(R.id.test_tv);
		Button button = (Button) findViewById(R.id.test_btn);
		
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dismissAnimation(textView);
			}
		});
	}

	
	/**
	 * 使用示例http://nineoldandroids.com/:
	 * AnimatorSet set = new AnimatorSet();
		set.playTogether(
		    ObjectAnimator.ofFloat(myView, "rotationX", 0, 360),
		    ObjectAnimator.ofFloat(myView, "rotationY", 0, 180),
		    ObjectAnimator.ofFloat(myView, "rotation", 0, -90),
		    ObjectAnimator.ofFloat(myView, "translationX", 0, 90),
		    ObjectAnimator.ofFloat(myView, "translationY", 0, 90),
		    ObjectAnimator.ofFloat(myView, "scaleX", 1, 1.5f),
		    ObjectAnimator.ofFloat(myView, "scaleY", 1, 0.5f),
		    ObjectAnimator.ofFloat(myView, "alpha", 1, 0.25f, 1)
		);
		set.setDuration(5 * 1000).start();
		
	 * 
	 */
	/**
	 * 消失动画
	 * @param v
	 */
    @SuppressLint("NewApi")
	public void dismissAnimation(View v){
    	AnimatorSet mHeartAnimator;
		ObjectAnimator localObjectAnimator1 = ObjectAnimator.ofFloat(v,
				"translationY", new float[] { -200.0F });
		ObjectAnimator localObjectAnimator2 = ObjectAnimator.ofFloat(v,
				"alpha", new float[] { 0.0F });
		mHeartAnimator = new AnimatorSet();
		mHeartAnimator.playTogether(new Animator[] { localObjectAnimator1,
				localObjectAnimator2 });
		mHeartAnimator.setDuration(270L);
		mHeartAnimator.setInterpolator(new LinearInterpolator());
		mHeartAnimator.start();
		mHeartAnimator.addListener(new AnimatorListener() {
			
			public void onAnimationStart(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationEnd(Animator arg0) {
				textView.clearAnimation();
			}
			
			public void onAnimationCancel(Animator arg0) {
				// TODO Auto-generated method stub
				
			}
		});
    }
	
	
}

 

 

(2)布局activity_main.xml

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    
    
    <TextView
        android:id="@+id/test_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/hello_world"
        android:background="@drawable/shape_text" />

    
    <Button 
        android:id="@+id/test_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_centerHorizontal="true"
        android:text="测试动画"
        />
    
</RelativeLayout>

 

实现效果如下:

点击按钮文字上升消失

 

 

可以使用该包,实现如path的弧形菜单效果:

Android动画进阶—使用开源动画库nineoldandroids

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值