import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.primitive.Line;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.color.Color;
import org.andengine.util.modifier.ease.EaseBackIn;
import org.andengine.util.modifier.ease.EaseBackInOut;
import org.andengine.util.modifier.ease.EaseBackOut;
import org.andengine.util.modifier.ease.EaseBounceIn;
import org.andengine.util.modifier.ease.EaseBounceInOut;
import org.andengine.util.modifier.ease.EaseBounceOut;
import org.andengine.util.modifier.ease.EaseCircularIn;
import org.andengine.util.modifier.ease.EaseCircularInOut;
import org.andengine.util.modifier.ease.EaseCircularOut;
import org.andengine.util.modifier.ease.EaseCubicIn;
import org.andengine.util.modifier.ease.EaseCubicInOut;
import org.andengine.util.modifier.ease.EaseCubicOut;
import org.andengine.util.modifier.ease.EaseElasticIn;
import org.andengine.util.modifier.ease.EaseElasticInOut;
import org.andengine.util.modifier.ease.EaseElasticOut;
import org.andengine.util.modifier.ease.EaseExponentialIn;
import org.andengine.util.modifier.ease.EaseExponentialInOut;
import org.andengine.util.modifier.ease.EaseExponentialOut;
import org.andengine.util.modifier.ease.EaseLinear;
import org.andengine.util.modifier.ease.EaseQuadIn;
import org.andengine.util.modifier.ease.EaseQuadInOut;
import org.andengine.util.modifier.ease.EaseQuadOut;
import org.andengine.util.modifier.ease.EaseQuartIn;
import org.andengine.util.modifier.ease.EaseQuartInOut;
import org.andengine.util.modifier.ease.EaseQuartOut;
import org.andengine.util.modifier.ease.EaseQuintIn;
import org.andengine.util.modifier.ease.EaseQuintInOut;
import org.andengine.util.modifier.ease.EaseQuintOut;
import org.andengine.util.modifier.ease.EaseSineIn;
import org.andengine.util.modifier.ease.EaseSineInOut;
import org.andengine.util.modifier.ease.EaseSineOut;
import org.andengine.util.modifier.ease.EaseStrongIn;
import org.andengine.util.modifier.ease.EaseStrongInOut;
import org.andengine.util.modifier.ease.EaseStrongOut;
import org.andengine.util.modifier.ease.IEaseFunction;
import android.graphics.Typeface;
import android.util.DisplayMetrics;
/**
* 各个IEaseFunction的效果演示
*/
public class EaseFunctionActivity extends SimpleBaseGameActivity
{
private static int winWidth = 854;
private static int winHeight = 480;
private BuildableBitmapTextureAtlas mBuildableBitmapTextureAtlas;
private TextureRegion mNextTextureRegion,mBadgeTextureRegion;
private Sprite[] mBadgeSprite = new Sprite[3];
private int mEaseFunctionsIndex = 0;
private Text[] mEaseFunctionNameTexts = new Text[3];
//当前是第几组IEaseFunction
private Text mIndexText;
//字体
private Font mFont,mIndexFont;
private static IEaseFunction[][] mEaseFunctions = new IEaseFunction[][]
{
new IEaseFunction[]
{
EaseLinear.getInstance(),
EaseLinear.getInstance(),
EaseLinear.getInstance()
},
new IEaseFunction[]
{
EaseBackIn.getInstance(),
EaseBackOut.getInstance(),
EaseBackInOut.getInstance()
},
new IEaseFunction[]
{
EaseBounceIn.getInstance(),
EaseBounceOut.getInstance(),
EaseBounceInOut.getInstance()
},
new IEaseFunction[]
{
EaseCircularIn.getInstance(),
EaseCircularOut.getInstance(),
EaseCircularInOut.getInstance()
},
new IEaseFunction[]
{
EaseCubicIn.getInstance(),
EaseCubicOut.getInstance(),
EaseCubicInOut.getInstance()
},
new IEaseFunction[]
{
EaseElasticIn.getInstance(),
EaseElasticOut.getInstance(),
EaseElasticInOut.getInstance()
},
new IEaseFunction[]
{
EaseExponentialIn.getInstance(),
EaseExponentialOut.getInstance(),
EaseExponentialInOut.getInstance()
},
new IEaseFunction[]
{
EaseQuadIn.getInstance(),
EaseQuadOut.getInstance(),
EaseQuadInOut.getInstance()
},
new IEaseFunction[]
{
EaseQuartIn.getInstance(),
EaseQuartOut.getInstance(),
EaseQuartInOut.getInstance()
},
new IEaseFunction[]
{
EaseQuintIn.getInstance(),
EaseQuintOut.getInstance(),
EaseQuintInOut.getInstance()
},
new IEaseFunction[]
{
EaseSineIn.getInstance(),
EaseSineOut.getInstance(),
EaseSineInOut.getInstance()
},
new IEaseFunction[]
{
EaseStrongIn.getInstance(),
EaseStrongOut.getInstance(),
EaseStrongInOut.getInstance()
}
};
/**
* 设置屏幕大小
*/
private void setScreenDisplay()
{
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
winWidth = displayMetrics.widthPixels;
winHeight = displayMetrics.heightPixels;
}
/**
* (1)
*/
@Override
public EngineOptions onCreateEngineOptions()
{
System.out.println("onCreateEngineOptions()");
setScreenDisplay();
Camera camera = new Camera(0,0,winWidth,winHeight);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(winWidth, winHeight), camera);
}
/**
* (2)
*/
@Override
protected void onCreateResources()
{
System.out.println("onCreateResources()");
//最好不同颜色字体存放在不同的纹理区域中
//创建字体
BitmapTextureAtlas mFontBitmapTextureAtlas1 = new BitmapTextureAtlas(getTextureManager(), 512, 512, TextureOptions.DEFAULT);
mFont = new Font(getFontManager(), mFontBitmapTextureAtlas1, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.WHITE);
//创建字体
BitmapTextureAtlas mFontBitmapTextureAtlas2 = new BitmapTextureAtlas(getTextureManager(), 512, 512, TextureOptions.DEFAULT);
mIndexFont = new Font(getFontManager(), mFontBitmapTextureAtlas2, Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC), 35, false, Color.RED);
//字体要单独加载(mFontBitmapTextureAtlas.load()好像不行)
mFont.load();
mIndexFont.load();
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/");
mBuildableBitmapTextureAtlas = new BuildableBitmapTextureAtlas(getTextureManager(), 512, 512, TextureOptions.DEFAULT);
mNextTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBuildableBitmapTextureAtlas, this, "next.png");
mBadgeTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBuildableBitmapTextureAtlas, this, "badge.png");
try
{
mBuildableBitmapTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0,1,1));
} catch (TextureAtlasBuilderException e)
{
e.printStackTrace();
}
mBuildableBitmapTextureAtlas.load();
}
/**
* (3)
*/
@Override
protected Scene onCreateScene()
{
System.out.println("onCreateScene()");
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
/*
* 平视显示器
* 摄像机的HUD相当于在你的视野上添加一个图层,这个图层用处很多,比如相机上的十字线,汽车模拟游戏里面的仪表盘。
* 在AndEngine中实际是在Camera中添加一个叫HUD的屏幕实体
*/
// HUD hud = new HUD(); //TODO 使用HUD的意义是什么?是不是与runOnUpdateThread的操作有关?
Sprite nextSprite = new Sprite(winWidth-mNextTextureRegion.getWidth()-100, 0, mNextTextureRegion, getVertexBufferObjectManager())
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
{
if (pSceneTouchEvent.isActionDown())
{
next();
}
return true;
}
};
Sprite previousSprite = new Sprite(100, 0, mNextTextureRegion, getVertexBufferObjectManager())
{
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY)
{
if (pSceneTouchEvent.isActionDown())
{
previous();
}
return true;
}
};
//设置previousSprite横向翻转
previousSprite.setFlippedHorizontal(true);
scene.attachChild(nextSprite);
scene.attachChild(previousSprite);
scene.registerTouchArea(nextSprite);
scene.registerTouchArea(previousSprite);
mBadgeSprite[0] = new Sprite(0, mNextTextureRegion.getHeight()+40, mBadgeTextureRegion, getVertexBufferObjectManager());
mBadgeSprite[1] = new Sprite(0, mNextTextureRegion.getHeight()+mBadgeTextureRegion.getHeight()+80, mBadgeTextureRegion, getVertexBufferObjectManager());
mBadgeSprite[2] = new Sprite(0, mNextTextureRegion.getHeight()+2*mBadgeTextureRegion.getHeight()+120, mBadgeTextureRegion, getVertexBufferObjectManager());
//创建文字(一般使用这个构造器避免ArrayIndexOutOfBoundsException异常)
mEaseFunctionNameTexts[0] = new Text(0, mNextTextureRegion.getHeight()+mBadgeTextureRegion.getHeight()+40, mFont, "",30, getVertexBufferObjectManager());
mEaseFunctionNameTexts[1] = new Text(0, mNextTextureRegion.getHeight()+2*mBadgeTextureRegion.getHeight()+80, mFont, "",30, getVertexBufferObjectManager());
mEaseFunctionNameTexts[2] = new Text(0, mNextTextureRegion.getHeight()+3*mBadgeTextureRegion.getHeight()+120, mFont, "",30, getVertexBufferObjectManager());
mIndexText = new Text(winWidth/2-100, 40, mIndexFont, "",30, getVertexBufferObjectManager());
scene.attachChild(mBadgeSprite[0]);
scene.attachChild(mBadgeSprite[1]);
scene.attachChild(mBadgeSprite[2]);
//把文字添加到场景
scene.attachChild(mIndexText);
scene.attachChild(mEaseFunctionNameTexts[0]);
scene.attachChild(mEaseFunctionNameTexts[1]);
scene.attachChild(mEaseFunctionNameTexts[2]);
//画3条直线
scene.attachChild(new Line(0, mNextTextureRegion.getHeight()+mBadgeTextureRegion.getHeight()+73,
winWidth, mNextTextureRegion.getHeight()+mBadgeTextureRegion.getHeight()+73, 2, getVertexBufferObjectManager()));
scene.attachChild(new Line(0, mNextTextureRegion.getHeight()+2*mBadgeTextureRegion.getHeight()+113,
winWidth, mNextTextureRegion.getHeight()+2*mBadgeTextureRegion.getHeight()+113, 2, getVertexBufferObjectManager()));
scene.attachChild(new Line(0, mNextTextureRegion.getHeight()+3*mBadgeTextureRegion.getHeight()+153,
winWidth, mNextTextureRegion.getHeight()+3*mBadgeTextureRegion.getHeight()+153, 2, getVertexBufferObjectManager()));
return scene;
}
private void next()
{
mEaseFunctionsIndex++;
mEaseFunctionsIndex %= mEaseFunctions.length;
reAnimate();
}
private void previous()
{
mEaseFunctionsIndex--;
if (mEaseFunctionsIndex < 0) mEaseFunctionsIndex += mEaseFunctions.length;
reAnimate();
}
/**
* (4)
*/
@Override
public synchronized void onGameCreated()
{
reAnimate();
}
/**
* 徽记坐标归位,并重新设置EaseFunction定义移动修改器。
*/
private void reAnimate()
{
//实现onUpdateSpriteBatch函数来动态更新精灵
this.runOnUpdateThread(new Runnable()
{
@Override
public void run()
{
System.out.println("mEaseFunctionsIndex:"+mEaseFunctionsIndex);
mIndexText.setText("第"+mEaseFunctionsIndex+"组");
for (int i = 0; i < 3; i++)
{
mBadgeSprite[i].setPosition(0, mBadgeSprite[i].getY());
//移除上一次的实体修改器
mBadgeSprite[i].clearEntityModifiers();
//设置文本
mEaseFunctionNameTexts[i].setText(mEaseFunctions[mEaseFunctionsIndex][i].getClass().getSimpleName());
MoveModifier moveModifier = new MoveModifier
(
3,
0,
winWidth-mBadgeSprite[i].getWidth(),
mBadgeSprite[i].getY(),
mBadgeSprite[i].getY(),
mEaseFunctions[mEaseFunctionsIndex][i]
);
mBadgeSprite[i].registerEntityModifier(moveModifier);
}
}
});
}
}