import java.io.IOException;
import java.io.InputStream;
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.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.color.Color;
import android.util.DisplayMetrics;
/**
* 绘制一个精灵
*/
public class DrawSpriteActivity extends SimpleBaseGameActivity
{
private static int winWidth = 854;
private static int winHeight = 480;
//纹理
private ITexture mTexture;
//纹理范围
private ITextureRegion mTextureRegin;
@Override
public EngineOptions onCreateEngineOptions()
{
setScreenDisplay();
Camera camera = new Camera(0, 0, winWidth, winHeight);
EngineOptions engineOptions = new EngineOptions(
true,
ScreenOrientation.LANDSCAPE_FIXED,
new RatioResolutionPolicy(winWidth, winHeight), camera);
return engineOptions;
}
/**
* 7、关于Texture:
* Texture是AndEngine所提供的纹理用类,但Texture本身(在AndEngine中)并没有提供加载图片的方法,
* 必须通过TextureRegionFactory类
* (更准确的说,依赖它内部封装的TextureRegion、BuildableTexture等类)与之合作才可以加载纹理
* 。除此之外,AndEngine要求所加载纹理(图片)大小必须为2的整数次幂。
*
* 8、关于TextureRegion:
* TextureRegion的父类是抽象类BaseTextureRegion,主要功能也被封装在BaseTextureRegion类当中,
* AndEngine提供了TextureRegionFactory这个工厂类用以简化构建TextureRegion的流程
* 。单就TextureRegion来讲,它的作用似乎就是让系统知道如何剪切一个纹理,并返回一个这样的纹理给你。
*
* 然而,事实上AndEngine中只有TextureRegion才更接近于通常意义上的Texture。或者说,只有TextureRegion +
* Texture时
* ,我们才能较为完整的使用AndEngine纹理功能。严肃的讲,AndEngine中的Texture有很多功能必须靠TextureRegion最终完成
* ,比如AndEngine中的Sprite必须加载TextureRegion才能使用Texture,而不是直接调用Texture,
* TMXTiledMap中读取指定瓦片返回的也是TextureRegion
* ,而非直接的Texture(进行画面渲染时AndEngine内部会调用TextureRegion中的Texture引用
* ,但也只允许如此调用);应该说,AndEngine中见Texture几乎必见TextureRegion,二者无法分离,缺一不可。
*/
@Override
protected void onCreateResources()
{
//Texture“纹理”、TextureRegion“纹理范围”、Sprite“精灵”。
try
{
mTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener()
{
@Override
public InputStream open() throws IOException
{
return getAssets().open("images/face_box.png");
}
});
//加载纹理到硬件
mTexture.load();
//映射纹理
mTextureRegin = TextureRegionFactory.extractFromTexture(mTexture);
} catch (IOException e)
{
e.printStackTrace();
}
}
@Override
protected Scene onCreateScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
scene.setBackground(new Background(Color.GREEN));
float pX = (winWidth-mTextureRegin.getWidth())/2;
float pY = (winHeight-mTextureRegin.getHeight())/2;
//创建一个精灵
Sprite sprite = new Sprite(pX, pY, mTextureRegin, this.getVertexBufferObjectManager());
//添加到场景
scene.attachChild(sprite);
return scene;
}
/**
* 设置屏幕大小
*/
private void setScreenDisplay()
{
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
winWidth = outMetrics.widthPixels;
winHeight = outMetrics.heightPixels;
}
}