Hack 32 使用Cocos2dx 增强应用 实现下雪效果

Hack 32 使用Cocos2dx 增强应用 实现下雪效果,第1张

概述    Android提供了不少方法向用户展示信息,但是有些不是高效的,如果我们的应用需要添加3D动画,我们可能使用OpenGL,这需要添加一个复杂层,有些人可能不会用,我们可以用游戏框架Cocos2dx来使我们的应用增色,下面演示用其增加一个界面 Cocos2dx是一个c++库,资料自己可以查一下。     Cocos2dx用OpenGL来绘制,为了画图形,需要用到SurfaceView, 看一

AndroID提供了不少方法向用户展示信息,但是有些不是高效的,如果我们的应用需要添加3D动画,我们可能使用OpenGL,这需要添加一个复杂层,有些人可能不会用,我们可以用游戏框架Cocos2dx来使我们的应用增色,下面演示用其增加一个界面

Cocos2dx是一个c++库,资料自己可以查一下。

Cocos2dx用OpenGL来绘制,为了画图形,需要用到SurfaceVIEw, 看一下SurfaceVIEw的注释:

SurfaceVIEw是vIEw的子类,提供了一个专用的绘图表面嵌入在视图层次,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面。由于拥有独立的绘图表面,因此SurfaceVIEw的UI就可以在一个独立的线程中进行行绘制。又由于不占用主线程资源,SurfaceVIEw一方面可以实现复杂而高效的UI,另一方面又不会导致用户输入得不到及时响应。

普通的AndroID控件,例如TextVIEw、button和CheckBox等,它们都是将自己的UI绘制在宿主窗口的绘图表面之上,这意味着它们的UI是在应用程序的主线程中进行绘制的。由于应用程序的主线程除了要绘制UI之外,还需要及时地响应用户输入,否则的话,系统就会认为应用程序没有响应了,因此就会d出一个ANR对话框出来。对于一些游戏画面,或者摄像头预览、视频播放来说,它们的UI都比较复杂,而且要求能够进行高效的绘制,因此,它们的UI就不适合在应用程序的主线程中进行绘制。这时候就必须要给那些需要复杂而高效UI的视图生成一个独立的绘图表面,以及使用一个独立的线程来绘制这些视图的UI。

一句话:SurfaceVIEw单独开启一个线程,不运行在主线程当中,

SurfaceVIEw 可以和其他图层叠加,可在其它层上面或下面

效果图:


demo中有两层,第一层是显示文字,下层显示雪花

xml布局:

<?xml version="1.0" enCoding="utf-8"?><!--  copyright (c) 2012 Manning  See the file license.txt for copying permission.--><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent" >    <TextVIEw        androID:ID="@+ID/winter_text"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_alignParenttop="true"        androID:layout_margintop="5dp"        androID:gravity="center"        androID:text="Hello Winter!"        androID:textSize="30sp" />    <VIEw        androID:ID="@+ID/separator"        androID:layout_wIDth="fill_parent"        androID:layout_height="5dp"        androID:layout_below="@ID/winter_text"        androID:background="#FFFFFF" />    <TextVIEw        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_centerInParent="true"        androID:layout_margintop="5dp"        androID:gravity="center"        androID:text="It's sNowing!"        androID:textSize="30sp" />    <FrameLayout        androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent"        androID:layout_below="@ID/separator">        <org.cocos2dx.lib.Cocos2dxEditText            androID:ID="@+ID/game_edittext"            androID:layout_wIDth="fill_parent"            androID:layout_height="wrap_content"            androID:background="@null" />        <org.cocos2dx.lib.Cocos2dxGLSurfaceVIEw            androID:ID="@+ID/game_gl_surfacevIEw"            androID:layout_wIDth="fill_parent"            androID:layout_height="fill_parent"/>    </FrameLayout></relativeLayout><?xml version="1.0" enCoding="utf-8"?><!--  copyright (c) 2012 Manning  See the file license.txt for copying permission.--><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent" >    <TextVIEw        androID:ID="@+ID/winter_text"        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_alignParenttop="true"        androID:layout_margintop="5dp"        androID:gravity="center"        androID:text="Hello Winter!"        androID:textSize="30sp" />    <VIEw        androID:ID="@+ID/separator"        androID:layout_wIDth="fill_parent"        androID:layout_height="5dp"        androID:layout_below="@ID/winter_text"        androID:background="#FFFFFF" />    <TextVIEw        androID:layout_wIDth="fill_parent"        androID:layout_height="wrap_content"        androID:layout_centerInParent="true"        androID:layout_margintop="5dp"        androID:gravity="center"        androID:text="It's sNowing!"        androID:textSize="30sp" />    <FrameLayout        androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent"        androID:layout_below="@ID/separator">        <org.cocos2dx.lib.Cocos2dxEditText            androID:ID="@+ID/game_edittext"            androID:layout_wIDth="fill_parent"            androID:layout_height="wrap_content"            androID:background="@null" />        <org.cocos2dx.lib.Cocos2dxGLSurfaceVIEw            androID:ID="@+ID/game_gl_surfacevIEw"            androID:layout_wIDth="fill_parent"            androID:layout_height="fill_parent"/>    </FrameLayout></relativeLayout>


Java代码:

package com.manning.androIDHacks.Hack032;import org.cocos2dx.lib.Cocos2dxActivity;import org.cocos2dx.lib.Cocos2dxEditText;import org.cocos2dx.lib.Cocos2dxGLSurfaceVIEw;import org.cocos2dx.lib.Cocos2dxRenderer;import androID.app.ActivityManager;import androID.content.Context;import androID.content.pm.ConfigurationInfo;import androID.os.Bundle;import androID.util.Log;//继承Cocos2dxActivitypublic class MainActivity extends Cocos2dxActivity {  protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    if (detectopenGLES20()) {      // get the packagename,it's used to set the resource path      String packagename = getApplication().getPackagename();      super.setPackagename(packagename);      setContentVIEw(R.layout.game_demo);      mGLVIEw = (Cocos2dxGLSurfaceVIEw) findVIEwByID(R.ID.game_gl_surfacevIEw);      Cocos2dxEditText edittext = (Cocos2dxEditText) findVIEwByID(R.ID.game_edittext);      mGLVIEw.setEGLContextClIEntVersion(2);      mGLVIEw.setCocos2dxRenderer(new Cocos2dxRenderer());      mGLVIEw.setTextFIEld(edittext);    } else {      Log.d("activity","don't support gles2.0");      finish();    }  }  @OverrIDe  protected voID onPause() {    super.onPause();    mGLVIEw.onPause();  }  @OverrIDe  protected voID onResume() {    super.onResume();    mGLVIEw.onResume();  }  private boolean detectopenGLES20() {    ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);    ConfigurationInfo info = am.getDeviceConfigurationInfo();    return (info.reqGlEsversion >= 0x20000);  }  static {    System.loadlibrary("game");  }}

源码地址:

androID下雪效果 - 下载频道 - CSDN.NET http://download.csdn.net/detail/xiaobijia/8073251

总结

以上是内存溢出为你收集整理的Hack 32 使用Cocos2dx 增强应用 实现下雪效果全部内容,希望文章能够帮你解决Hack 32 使用Cocos2dx 增强应用 实现下雪效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1005264.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存