截取當前屏幕:(廢話不多說直接上代碼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() {