COCOS2DX-3.0截取当前屏幕并在ANDROID上分享

截取当前屏幕:(废话不多说直接上代码C++)

Size wSize=Director::sharedDirector()->getVisibleSize();
RenderTexture* render=RenderTexture::create(wSize.width, wSize.height);
render->begin();
Director::sharedDirector()->getRunningScene()->visit();
render->end();
render->saveToFile(name, kCCImageFormatJPEG);

ANDROID分享:(C++)

std::string path=FileUtils::sharedFileUtils()->getWritablePath()+name;
JniMethodInfo minfo;
if (JniHelper::getStaticMethodInfo(minfo, "xxx.xx.xx", "xxx", "(Ljava/lang/String;)V")) {
	minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, minfo.env->NewStringUTF(path.c_str()));
}

ANDROID分享:(java)

runOnUiThread(new Runnable() {
	public void run() {
		// 显示
		final ProgressDialog dialog = ProgressDialog.show(app, "分享", "分享制作中...");
		// 
		new Thread(new Runnable() {
			public void run() {
				do {
					// 检测分享文件是否存在
					File file=new File(path);
					if (file.exists()) {
						break;
					}
				} while (true);
						
				// 分享
				new Handler(XXXActivity.getMainLooper()).post(new Runnable() {
					public void run() {
						// 隐藏
						dialog.dismiss();
								
						// android 分享
						Intent intent = new Intent();
						intent.setType("text/*");
						intent.setType("image/*");
						intent.setAction(Intent.ACTION_SEND);
						intent.putExtra(Intent.EXTRA_TEXT, "xxxxx");
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						intent.putExtra(Intent.EXTRA_SUBJECT, "xxxxx");
						intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(path)));
						XXXActivity.startActivity(Intent.createChooser(intent, "分享"));
					}
				});
			}
		}).start();
	}
});

PS:

1、以上就是COCOS2DX-3.0截屏及ANDROID分享的代码;

2、因在截屏调用saveToFile后,在ANDROID分享时不一定就创建了截屏文件,所以要检查一下文件是否存在。

3、又为了要检查文件是否存在并不是上一次的截屏文件在截屏前应删除截屏文件,这里省略了这一步。

Exception:

1、在下面代码应注意(Can't create handler inside thread that has not called Looper.prepare())异常

		new Handler(XXXActivity.getMainLooper()).post(new Runnable() {



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章