AndEngine学习笔记(三) RectangleExample

本文介绍使用AndEngine绘制矩形的示例代码。通过RectangleExample类展示了如何创建不同颜色的矩形并设置其位置和大小。同时介绍了纹理过滤选项如NEAREST和BILINEAR的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

项目地址:http://code.google.com/p/andengine/

 

 

 

RectangleExample源码:

 

 

 

 

package org.anddev.andengine.examples;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.primitive.Rectangle;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.util.FPSLogger;

/**
 * @author Nicolas Gramlich
 * @since 11:54:51 - 03.04.2010
 */
public class RectangleExample extends BaseExample {
	// ===========================================================
	// Constants
	// ===========================================================

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

	// ===========================================================
	// Fields
	// ===========================================================

	private Camera mCamera;

	// ===========================================================
	// Constructors
	// ===========================================================

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================

	@Override
	public Engine onLoadEngine() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
	}

	@Override
	public void onLoadResources() {

	}

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

		final Scene scene = new Scene(1);
		scene.setBackground(new ColorBackground(0, 0, 0));

		final Rectangle rect1 = new Rectangle(180, 60, 180, 180);
		rect1.setColor(1, 0, 0);
		final Rectangle rect2 = new Rectangle(360, 60, 180, 180);
		rect2.setColor(0, 1, 0);
		final Rectangle rect3 = new Rectangle(180, 240, 180, 180);
		rect3.setColor(0, 0, 1);
		final Rectangle rect4 = new Rectangle(360, 240, 180, 180);
		rect4.setColor(1, 1, 0);

		scene.getTopLayer().addEntity(rect1);
		scene.getTopLayer().addEntity(rect2);
		scene.getTopLayer().addEntity(rect3);
		scene.getTopLayer().addEntity(rect4);

		return scene;
	}

	@Override
	public void onLoadComplete() {

	}

	// ===========================================================
	// Methods
	// ===========================================================

	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}

 

RectangleExample的内容和LineExample几乎相同,画Line和画Rectangle的方法和类似的图形画法在以后的文章里面总结一次,暂时不作过多记录。

 

NEAREST: Faster then BILINEAR as only the nearest pixel on the Texture is used (Sprites tend to look pixelated when, physical resolution is higher or lower than texture resolution. Up/Down-scaling)
BILINEAR: Slower than NEAREST, but looks better, as not only one pixel from the texture is fetched but the 4 nearest.

The difference between those and the REPEATING TextureOptions is the way OpenGL treats TextureCoordinates that are out of the physical bounds of the Texture (usually ranging from 0.0 to 1.0).
Where REPEATING paints the Texture twice when the coordinates go from 0.0 to 2.0, NON-REPEATING will stretch the outermost pixel of the Texture what looks pretty ugly.

So the REPEATING TextureOptions are used only in very special cases.

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值