Scale and expand the image when you click andengine.

Source: Internet
Author: User

1. The cream object to be used for image scaling is of the smoothcamera type. That is, camera = new
Smoothcamera (0, 0, camera_width, camera_height, 0, 0, 0.1f)

2. Place the image in a folder (raw). The image type can be PVR or bin extension. In OpenGL, fragments consume less memory resources.

There are three demos in andengine. The following code is used directly,

I:

Package org. andengine. examples;

Import java. Io. ioexception; import java. Io. inputstream;

Import Org. andengine. engine. camera. camera; import Org. andengine. engine. camera. smoothcamera; import Org. andengine. engine. options. engineoptions; import Org. andengine. engine. options. screenorientation; import Org. andengine. engine. options. resolutionpolicy. ratioresolutionpolicy; import Org. andengine. entity. scene. ionscenetouchlistener; 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. examples. ADT. zoomstate; import Org. andengine. input. touch. touchevent; import Org. andengine. openGL. texture. itexture; import Org. andengine. openGL. texture. textureoptions; import Org. andengine. openGL. texture. compressed. PVR. pvrgztexture; import Org. andengine. openGL. texture. compressed. PVR. pvrtexture. pvrtextureformat; import Org. andengine. openGL. texture. region. itextureregion; import Org. andengine. openGL. texture. region. textureregionfactory; import Org. andengine. UI. activity. simplebasegameactivity; import Org. andengine. util. debug. debug;

Import Android. widget. Toast;

/*** (C) 2010 Nicolas gramlich * (c) 2011 Zynga ** @ author Nicolas gramlich * @ since 13:54:51-13.07.2011 */public class pvrgztextureexample extends simplebasegameactivity {// ======== ========================================================== ====/constants // ========================================== ======================================

Private Static final int camera_width = 720; Private Static final int camera_height = 480;

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

Private itexture mtexture; private itextureregion mhousetextureregion;

Private zoomstate mzoomstate = zoomstate. None;

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

// ================================================ ==================================/// Getter & setter // ================== ========================================================== ========

// ================================================ ====================================/// Methods for/from superclass/interfaces // ========== ========================================================== ==============

@ Override public engineoptions oncreateengineoptions () {toast. maketext (this, "click the top half of the screen to zoom in or the bottom half to zoom out! ", Toast. length_long );

Final camera = new smoothcamera (0, 0, camera_width, camera_height, 0, 0, 0.1f) {@ override

// Depending on the status value of mzoomstate, determine whether to zoom in or out. The value of this mzoomstate is assigned a public void onupdate (final float implements condselapsed) {Switch (pvrgztextureexample. this. mzoomstate) {case in: This. setzoomfactor (this. getzoomfactor () + 0.1f * parse condselapsed); break; case out: This. setzoomfactor (this. getzoomfactor ()-0.1f * parse condselapsed); break;} super. onupdate (synchronized condselapsed );}};

Return new engineoptions (true, screenorientation. landscape_fixed, new ratioresolutionpolicy (camera_width, camera_height), camera );}

@ Override public void oncreateresources () {try {This. mtexture = new pvrgztexture (this. gettexturemanager (), pvrtextureformat. rgba_8888, textureoptions. bilinear) {@ override protected inputstream ongetinputstream () throws ioexception {

// Read the image file data return pvrgztextureexample. This. getresources (). openrawresource (R. Raw. house_pvrgz_argb_8888) ;}; this. mtexture. Load ();

// Extract the image from the existing texture

This. mhousetextureregion = textureregionfactory. extractfromtexture (this. mtexture, 0, 0,512,512);} catch (final throwable e) {Debug. E (E );}}

@ Override public scene oncreatescene () {This. mengine. registerupdatehandler (New fpslogger ());

Final scene = new scene (); scene. setbackground (new background (0.09804f, 0.6274f, 0.8784f ));

Final float centerx = (camera_width-This. mhousetextureregion. getwidth ()/2; Final float centery = (camera_height-This. mhousetextureregion. getheight ()/2;

Scene. attachchild (New sprite (centerx, centery, this. mhousetextureregion, this. getvertexbufferobjectmanager ()));

// When you touch the screen, the value of mzoomstate is determined based on the touch position.

Scene. setonscenetouchlistener (New ionscenetouchlistener () {@ override public Boolean onscenetouchevent (final scene pscene, final touchevent pscenetouchevent) {If (pscenetouchevent. isactiondown () | pscenetouchevent. isactionmove () {If (pscenetouchevent. gety () <camera_height/2) {pvrgztextureexample. this. mzoomstate = zoomstate. in;} else {pvrgztextureexample. this. mzoomstate = zoomstate. out ;}} else {pvrgztextureexample. this. mzoomstate = zoomstate. none;} return true ;}});

Return scene ;}

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

// ================================================ ======================================/Inner and anonymous classes // ================ ========================================================== =======}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.