Cocos2dx-android Integrated for Avatar Switching (JNI use)

Source: Internet
Author: User

This article was reproduced in: http://www.cnblogs.com/zisou/p/cocos2dxJQ.html

COCOS2DX in the game development of Android platform often encounter some mixed needs, such as:

For example, some community games need to replace the player's "Avatar" operation, in fact, call the Android servers service inside the local image, as well as SELECT, crop the picture, cut to the server to upload the image function, so as to achieve the operation of the user avatar function;

Then use Jnihelper to call Andriod Java inside the class and methods to achieve a mixed to meet our needs to operate the Android system: Get the native MAC address, get the local name, and even to operate the Android background service complex operation;

To start with, we need to write a good Android activity class, the main function of this class is to call the Android local image, select, crop, crop play to the server to upload pictures; First look at my directory structure:

The following is the entrance of our game main cocos2dxactivity, above a userinfoactivity.java is we really need to call the class of the Avatar function;

He contains a userinfo.xml layer which is the knowledge of Android development;

The interface content of the Userinfo.xml view file is this:

Specific inside the code I do not post, here is not to say how to engage in Android development, the entire project needs to file pictures, I will upload, everyone to pick up;

The above Userinfoactivity.java class has fully realized the above functions, only need to import the class in the project line;

OK, the function class is implemented, then how to make C + + call Java? , the principle is to use Jnihelper in C + + class to invoke a method inside the Java class;

Now I want to call me in my C + + class entrance cocos2dxactivity (zha_jin_ Hua.java) inside of a method, this method is mainly to perform Android page jump, jump to my function class Userinfoactivity.java;

The Zha_jin_hua.java class method is as follows:

    public static Object gotoactivity ()    {        Intent in = new Intent (Zha_jin_hua.getcontext (),                Userinfoactivity.class);        Zha_jin_hua.getcontext (). StartActivity (in);                return null;    }

This is a static class, the return type is object, but I do not need to return the value, because I do the page jump, if my requirement is to get the Android module code value, you can fill in the corresponding return type, you can do Java and C + + value of each other;

The header files in the C + + class need to be introduced:

#if (Cc_target_platform = = cc_platform_android) #include <jni.h> #include "platform/android/jni/jnihelper.h" # Include <android/log.h> #if 1#define  log_tag    "jnihelper" #define  logd (...)  __android_log_print (android_log_debug,log_tag,__va_args__) #else # define  logd (...) #endif #endif

In a C + + class, a specific invocation requires the following execution:

void Personlayer::onheadtouched () {    cclog ("player clicks on Avatar for Avatar");    Basesoundcontrol::P layeffectmusic (Effect_click);    #if (Cc_target_platform = = cc_platform_android)    jnimethodinfo minfo;    Jobject jobj;        The Getstaticmethodinfo method is to call a static class, or you can not call the static class Getmethodinfo        bool B = Jnihelper::getstaticmethodinfo (Minfo,                                          " Com.dcgame.zhajinhua.zha_jin_hua ",//class path                                          " Gotoactivity ",//static method name                                          " () ljava/lang/object; "); /parentheses are the parameters, followed by the return value.        if (!b) {            cclog ("Jnihelper::getmethodinfo error ...");        } else{            Cclog ("Ready to Invoke Method ...");            Jobj = Minfo.env->callstaticobjectmethod (Minfo.classid, Minfo.methodid);        }    #endif}

So you can call Java code, to execute the Gotoactivity method, this method is static, so use the getstaticmethodinfo, if not the getmethodinfo;

Complete the above operation is not finished, because we call the Android system avatar, need to use the camera function, so also open this function permission;

Add the following code to the Androidmanifest.xml:

    <uses-permission android:name= "Android.permission.CAMERA"/>    <uses-feature android:name= " Android.hardware.camera "/>    <uses-feature android:name=" Android.hardware.camera.autofocus "/>    <uses-permission android:name= "Android.permission.READ_LOGS" >    

There is also a class that is configured for activity:

<activity android:name= "Com.xxx.xxxxx.UserInfoActivity" ></activity>

OK, the configuration is complete, can be implemented in the Android avatar operation, the effect is as follows:

Related image resources, source code:

Baidu Cloud

Cocos2dx-android Integrated for Avatar Switching (JNI use)

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.