AndEngine之DEMO学习(二)RectangleExample

package org.andengine.examples;

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 android.widget.Toast;


public class RectangleExample extends SimpleBaseGameActivity {


 private static final int CAMERA_WIDTH = 720;
 private static final int CAMERA_HEIGHT = 480;


 @Override
 public EngineOptions onCreateEngineOptions() {
  final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

  return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
 }

 @Override
 public void onCreateResources() {

 }

 @Override
 public Scene onCreateScene() {
  this.mEngine.registerUpdateHandler(new FPSLogger());

  final Scene scene = new Scene();
  final ScreenCapture screenCapture = new ScreenCapture();
  scene.attachChild(screenCapture);
  scene.setOnSceneTouchListener(new IOnSceneTouchListener() {
   @Override
   public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
    if(pSceneTouchEvent.isActionDown()) {
     screenCapture.capture(180, 60, 360, 360, FileUtils.getAbsolutePathOnExternalStorage(RectangleExample.this, "Screen_" + System.currentTimeMillis() + ".png"), new IScreenCaptureCallback() {
      @Override
      public void onScreenCaptured(final String pFilePath) {
       RectangleExample.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
         Toast.makeText(RectangleExample.this, "Screenshot: " + pFilePath + " taken!", Toast.LENGTH_SHORT).show();
        }
       });
      }

      @Override
      public void onScreenCaptureFailed(final String pFilePath, final Exception pException) {
       RectangleExample.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
         Toast.makeText(RectangleExample.this, "FAILED capturing Screenshot: " + pFilePath + " !", Toast.LENGTH_SHORT).show();
        }
       });
      }
     });
    }
    return true;
   }
  });

  scene.setBackground(new Background(0, 0, 0));

  /* Create the rectangles. */
  final Rectangle rect1 = this.makeColoredRectangle(-180, -180, 1, 0, 0);
  final Rectangle rect2 = this.makeColoredRectangle(0, -180, 0, 1, 0);
  final Rectangle rect3 = this.makeColoredRectangle(0, 0, 0, 0, 1);
  final Rectangle rect4 = this.makeColoredRectangle(-180, 0, 1, 1, 0);

  final Entity rectangleGroup = new Entity(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2);

  rectangleGroup.attachChild(rect1);
  rectangleGroup.attachChild(rect2);
  rectangleGroup.attachChild(rect3);
  rectangleGroup.attachChild(rect4);

  scene.attachChild(rectangleGroup);

  return scene;
 }

 private Rectangle makeColoredRectangle(final float pX, final float pY, final float pRed, final float pGreen, final float pBlue) {
  final Rectangle coloredRect = new Rectangle(pX, pY, 180, 180, this.getVertexBufferObjectManager());
  coloredRect.setColor(pRed, pGreen, pBlue);
  return coloredRect;
 }
}

这是AndEngine提供的第二个程序,在屏幕上画出四个颜色不同的矩形,相对上一个画线的程序还增加了一个截屏的功能。

截屏的实现比较简单,screenCapture作为实体加入Engine中,目的是在调用capture函数是,使用对象中持有的ScreenGrabber工具进行截图,并保存图片。因为代码中没有创建目录,需要修改一下ScreenCapture中保存图片的代码:

private static void saveCapture(final Bitmap pBitmap, final String pFilePath) throws FileNotFoundException {
		FileOutputStream out = null;
		try {
			File pFile = new File(pFilePath);
			if (!pFile.getParentFile().exists())
			{
				if (!pFile.getParentFile().mkdirs())
				{
					throw new FileNotFoundException("Dir creat fail. ");
				}
			}
			out = new FileOutputStream(pFile);
			pBitmap.compress(CompressFormat.PNG, 100, out);
		} catch (final FileNotFoundException e) {
			StreamUtils.flushCloseStream(out);
			Debug.e("Error saving file to: " + pFilePath, e);
			throw e;
		}
	}

点击屏幕显示截图成功,但是不要高兴太早,你会发现截屏的图片是一片漆黑,想了一下实现原理,通过OpenGL将屏幕读取出来,代码为:

GLES20.glReadPixels(pGrabX, 0, pGrabWidth, pGrabY + pGrabHeight, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, sourceBuffer);

他执行的时机应该是所有图像已经绘制完成的时候,所有在RectangleExample中将截屏的Entity—scene.attachChild(screenCapture);放在最后,程序正常运行。

再看一下我们这回的主角Rectangle,他不但有Line中Shape所具有的功能,还实现IAreaShape接口:

public interface IAreaShape extends IShape {
	//拥有了宽和高
	public float getWidth();
	public float getHeight();

	//拥有了缩放后的宽和高
	public float getWidthScaled();
	public float getHeightScaled();

	public void setHeight(final float pHeight);
	public void setWidth(final float pWidth);
	public void setSize(final float pWidth, final float pHeight);
}

在缓存方面使用了HighPerformanceRectangleVertexBufferObject(高性能矩形顶点缓存对象),缓存数据mBufferData中包含了四个顶点坐标和颜色数据。与Line一样使用的都是GLES20.GL_TRIANGLE_STRIP的连接方法,扩展阅读“OpenGL ES 绘图基础”。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值