如果使用截屏,需要修改andengine源码中的org.andengine.entity.util.ScreenCapture类中的saveCapture方法:
修改后代码:
private static void saveCapture(final Bitmap pBitmap, final String pFilePath) throws FileNotFoundException {
FileOutputStream out = null;
try {
File file = new File(pFilePath);
if (!file.getParentFile().exists())
{
if(!file.getParentFile().mkdirs()){ //创建文件
throw new FileNotFoundException("create dir fail.");
}
}
out = new FileOutputStream(pFilePath);
pBitmap.compress(CompressFormat.PNG, 100, out);
} catch (final FileNotFoundException e) {
StreamUtils.flushCloseStream(out);
Debug.e("Error saving file to: " + pFilePath, e);
throw e;
}
}
然后重新打包,然后加到自己的项目中使用。
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.Entity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.entity.util.ScreenCapture;
import org.andengine.entity.util.ScreenCapture.IScreenCaptureCallback;
import org.andengine.input.touch.TouchEvent;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.FileUtils;
import org.andengine.util.color.Color;
import android.util.DisplayMetrics;
import android.widget.Toast;
/**
* 创建有颜色的矩形
*/
public class RectangleActivity extends SimpleBaseGameActivity
{
private static int winWidth = 854;
private static int winHeight = 480;
//每个矩形的大小
private static final int RECTANGLE_WIDTH = 180;
private static final int RECTANGLE_HEIGHT = 180;
@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;
}
@Override
protected void onCreateResources()
{
}
/**
* 创建场景
*/
@Override
protected Scene onCreateScene()
{
this.mEngine.registerUpdateHandler(new FPSLogger());
Scene scene = new Scene();
scene.setBackground(new Background(Color.BLACK));
Rectangle rectangle1 = makeColorRectangle(0, 0, RECTANGLE_WIDTH, RECTANGLE_HEIGHT, Color.RED);
Rectangle rectangle2 = makeColorRectangle(RECTANGLE_WIDTH, 0, RECTANGLE_WIDTH, RECTANGLE_HEIGHT, Color.YELLOW);
Rectangle rectangle3 = makeColorRectangle(0, RECTANGLE_WIDTH, RECTANGLE_WIDTH, RECTANGLE_HEIGHT, Color.BLUE);
Rectangle rectangle4 = makeColorRectangle(RECTANGLE_WIDTH, RECTANGLE_HEIGHT, RECTANGLE_WIDTH, RECTANGLE_HEIGHT, Color.WHITE);
//居中显示
Entity entity = new Entity((winWidth-2*RECTANGLE_WIDTH)/2, (winHeight-2*RECTANGLE_HEIGHT)/2);
entity.attachChild(rectangle1);
entity.attachChild(rectangle2);
entity.attachChild(rectangle3);
entity.attachChild(rectangle4);
scene.attachChild(entity);
//截屏(该代码要在截图场景的下面)
final ScreenCapture screenCapture = new ScreenCapture();
//加入截屏功能
scene.attachChild(screenCapture);
//场景触摸事件
scene.setOnSceneTouchListener(new IOnSceneTouchListener()
{
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
{
if (pSceneTouchEvent.isActionDown())
{
screenCapture.capture(
winWidth, winHeight,
FileUtils.getAbsolutePathOnExternalStorage(RectangleActivity.this,"Screen_" + System.currentTimeMillis() + ".png"),
new IScreenCaptureCallback()
{
/**
* 截图成功
*/
@Override
public void onScreenCaptured(final String pFilePath)
{
RectangleActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
Toast.makeText(RectangleActivity.this, "截图成功:"+pFilePath, 3000).show();
}
});
}
/**
* 截图失败
*/
@Override
public void onScreenCaptureFailed(final String pFilePath, final Exception pException)
{
RectangleActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
Toast.makeText(RectangleActivity.this, "截图失败:"+pFilePath, 3000).show();
System.err.println("失败:"+pException.getMessage());
}
});
}
});
}
return true;
}
});
return scene;
}
/**
* 创建有颜色的矩形
* @return
*/
private Rectangle makeColorRectangle(float pX, float pY, float pWidth, float pHeight,Color pColor)
{
Rectangle rectangle = new Rectangle(pX, pY, pWidth, pHeight, getVertexBufferObjectManager());
rectangle.setColor(pColor);
return rectangle;
}
/**
* 设置屏幕大小
*/
private void setScreenDisplay()
{
DisplayMetrics outMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
winWidth = outMetrics.widthPixels;
winHeight = outMetrics.heightPixels;
}
}