java-universal-tween-engine,一个动画系统库

http://code.google.com/p/java-universal-tween-engine/

可以用来创建平滑的移动系统,比如循环,移动,旋转等。由于项目使用纯java写成,所以支持swt,swing,android,opengles等。Tween缓冲大家应该都不陌生,说白了就是从一起始位置逐渐移动到目标位置的过程,这个过程可以是加速移动,也可以是减速移动,这些不同的缓动方式就是Tween的各种ease。

源码在最后,将其解压后复制到src就可以了。

用法,先定义一个需要运动的物体:
public class Particule {
	private float x, y;
    
    public float getX() {
            return x;
    }
    
    public float getY() {
            return y;
    }
    
    public void setX(float x) {
            this.x = x;
    }
    
    public void setY(float y) {
            this.y = y;
    }
}


然后,实现Tweenable接口,用于表明运动的返回值和变化值
import aurelienribon.tweenengine.Tweenable;

public class TweenableParticule implements Tweenable {
    // The following lines define the different possible tween types.
    // It's up to you to define what you need :-)

    public static final int X = 1;
    public static final int Y = 2;
    public static final int XY = 3;

    // Composition pattern

    private Particule target;

    // Constructor

    public TweenableParticule(Particule particule) {
            this.target = particule;
    }

    // Tweenable implementation

    @Override
    public int getTweenValues(int tweenType, float[] returnValues) {
            switch (tweenType) {
                    case X: returnValues[0] = target.getX(); return 1;
                    case Y: returnValues[0] = target.getY(); return 1;
                    case XY:
                            returnValues[0] = target.getX();
                            returnValues[1] = target.getY();
                            return 2;
                    default: assert false; return 0;
            }
    }
    
    @Override
    public void onTweenUpdated(int tweenType, float[] newValues) {
            switch (tweenType) {
                    case X: target.setX(newValues[0]); break;
                    case Y: target.setY(newValues[1]); break;
                    case XY:
                            target.setX(newValues[0]);
                            target.setY(newValues[1]);
                            break;
                    default: assert false; break;
            }
    }
}

最后,在Activity中运动:
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenGroup;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.Tweenable;
import aurelienribon.tweenengine.equations.*;

import com.ql.test.Particule;
import com.ql.test.TweenableParticule;

/**
 * http://code.google.com/p/java-universal-tween-engine/
 * @author admin
 *
 */
public class Screen4 extends Activity {
	Particule particule;
	TweenManager manager;
     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.screen_4);
//		Button btn_11=(Button)findViewById(R.id.btn_11);
        setContentView(new ScreenView(this));
		// Let's say we are working with an Android target. We need to pool our objects.

		Tween.setPoolEnabled(true);

		// We also need a manager to handle every tween.

		manager = new TweenManager();

		// We can now create as many interpolations as we need !
		particule=new Particule();
		particule.setX(100);
		particule.setY(100);
		
		Tweenable tweenParticle=new TweenableParticule(particule);
		
		Tween tween = Tween.to(tweenParticle, TweenableParticule.XY, 10000, Cubic.OUT).target(400, 500);
		manager.add(tween.start());

		tween = Tween.to(tweenParticle, TweenableParticule.XY, 10000, Bounce.OUT).target(100, 500).delay(10000);
		manager.add(tween.start());
		
//		TweenGroup tweenGroup=new TweenGroup().pack(
//				Tween.set(tweenParticle, TweenableParticule.XY),
//				Tween.to(tweenParticle, TweenableParticule.XY, 10000, Sine.OUT),
//				Tween.from(tweenParticle, TweenableParticule.XY, 10000, Sine.OUT),
//				Tween.to(tweenParticle, TweenableParticule.XY, 10000, Bounce.OUT)
//				);
//		manager.add(tweenGroup.sequence().repeat(2,5000).start());
		
//		TweenGroup tweenGroup= new TweenGroup().pack(
//			    Tween.to(tweenParticle, TweenableParticule.XY, 500, Quad.INOUT).target(200),
//			    Tween.to(tweenParticle, TweenableParticule.XY, 500, Quad.INOUT).target(100),
//			    Tween.to(tweenParticle, TweenableParticule.XY, 500, Quad.INOUT).target(200).delay(1000)
//			).sequence().start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        
    }

    @Override
    protected void onPause() {
        super.onPause();
        manager.clear();
    }

    class ScreenView extends View{

    	Paint paint;
		public ScreenView(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			paint=new Paint();
			paint.setAntiAlias(true);
			paint.setColor(Color.RED);
			paint.setStyle(Paint.Style.FILL_AND_STROKE);
			invalidate();
		}

		@Override
		protected void onDraw(Canvas canvas) {
			// TODO Auto-generated method stub
			//super.onDraw(canvas);
			canvas.drawCircle(particule.getX(), particule.getY(), 20, paint);
			manager.update();
			invalidate();
		}
    	
		
    }
}

你可能感兴趣的:(java,android,swing,Google,OS)