cocos js在带虚拟按键的手机上的动态调整
在Cocos2dxRenderer.java中增加native方法
private static native void setFrameSize(final int width, final int height,final int resolution_w, final int resolution_h);
然后在Cocos2dxRenderer.java中修改onSurfaceChanged方法
private int Default_Width=1080;
@Override
public void onSurfaceChanged(final GL10 GL10, final int width, final int height) {
Log.d("Renderer onSurfaceChang","width:"+width+" height:"+height);
Cocos2dxRenderer.nativeOnSurfaceChanged(width, height);
double scale = (double)Default_Width/width;
int resolution_w = Default_Width;
int resolution_h = (int) (height*scale);
Cocos2dxRenderer.setFrameSize(width, height,resolution_w,resolution_h);
}
其中Default_Width为设计屏幕的大小
在javaactivity-android.cpp中修改
JNIEXPORT void Java_org_cocos2dx_lib_Cocos2dxRenderer_setFrameSize(JNIEnv* env, jobject thiz, jint w, jint h,jint resolution_w, jint resolution_h)
{
auto director = cocos2d::Director::getInstance();
auto glview = director->getOpenGLView();
if (glview)
{
glview->setFrameSize(w, h);
glview->setDesignResolutionSize(resolution_w, resolution_h, ResolutionPolicy::NO_BORDER);
}
}
原来修改setFrameSize时也能在部分手机能解决虚拟按钮显示和消失是cocos中的点击事件偏移的问题 但是如果分辨率不是我们目标的分辨率,切后台会黑屏。
在c++中打日志发现原始的setDesignResolutionSize的宽度仍然为我们目标分辨率的宽度。随之按比例修改了屏幕的高度
网友评论