Andengine之点击时实现图片的缩放和扩大
来源:互联网 发布:淘宝c店需要品牌授权吗 编辑:程序博客网 时间:2024/05/16 03:09
1.实现图片的缩放要使用的Cream对象是SmoothCamera类型的。也就是说Camera camera = newSmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 0, 0, 0.1f)
2.图片放在文件夹下(raw),图片的类型可以是pvr,bin扩展名。在openGL中以上图片所占用的内存资源比较少。
在Andengine中有三个demo,下面直接来代码,
一:
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 camera = new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 0, 0, 0.1f) { @Override
//根据mZoomState的状态值来决定是放大还是缩小,该mZoomState的值由下面的代码给以赋值 public void onUpdate(final float pSecondsElapsed) { switch (PVRGZTextureExample.this.mZoomState) { case IN: this.setZoomFactor(this.getZoomFactor() + 0.1f * pSecondsElapsed); break; case OUT: this.setZoomFactor(this.getZoomFactor() - 0.1f * pSecondsElapsed); break; } super.onUpdate(pSecondsElapsed); } };
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 {
//读取图片文件的数据 return PVRGZTextureExample.this.getResources().openRawResource(R.raw.house_pvrgz_argb_8888); } }; this.mTexture.load();
//从已有的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 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()));
//触摸屏幕时,根据触摸的位置来决定是mZoomState的取值
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 // ===========================================================}
- Andengine之点击时实现图片的缩放和扩大
- 点击实现图片旋转缩放
- Android扩大图片的点击区域
- iOS 之UIImageView实现图片的移动和缩放
- openCV实现图片的时钟和中心圆形扩大效果
- 仿微信朋友圈图片点击浏览和关闭时的图片缩放的过渡动画。
- AndEngine实现屏幕截取和图片保存
- UIImageView实现图片的移动和缩放
- Android-实现图片的拖动和缩放
- nginx实现图片的缩放和裁剪
- Android实现图片的缩放和拖动
- 实现图片的缩放
- Android对图片局部扩大的实现
- 关于扩大图片点击热区的问题
- 实现xlistview+xutils展示数据之后点击item跳转页面显示图片缩放和放大
- AndEngine实现Sprite更换图片
- AndEngine实现Sprite更换图片
- 《js实现图片缓慢缩放的效果》之学习总结
- MID开发笔记
- 802.11n 中HT20 HT40是什么意思
- Struts2初学 struts.xml详解
- ICMP_t
- 漫谈 Clustering (3): Gaussian Mixture Model
- Andengine之点击时实现图片的缩放和扩大
- 如何确认WiFi 11N工作模式
- 线索二叉树
- 二叉树的存储结构及其遍历
- java rmic
- 分享 那些经典电影的经典台词
- IUSR和IIS_IUSRS和区别
- WebKit中CSS处理流程(二)
- ks8851网卡驱动在omap4460上的移植,发现开发板能发送数据,但是接受不到数据