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

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

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

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

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

普通的Android控件,例如TextView、Button和CheckBox等,它们都是将自己的UI绘制在宿主窗口的绘图表面之上,这意味着它们的UI是在应用程序的主线程中进行绘制的。由于应用程序的主线程除了要绘制UI之外,还需要及时地响应用户输入,否则的话,系统就会认为应用程序没有响应了,因此就会弹出一个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;
//继承Cocos2dxActivity
public 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

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...