quick-cocos2dx 截屏分享到 微信、微博等社交网络

最近做游戏要用截屏分享的功能,于是在quick群中问群友怎么实现。感谢网友@当往事遇上风 给我的截屏代码。

思路:

1、先用qiuck截图保存本地

2、用luaj调用android的分享api 分享到社交网络

下面是代码

 

function ShowView:shareButtonPressed()
self:screen()
local path = self:screen()
if device.platform == "android" then
-- Java 类的名称
local className = "com/szfore/xazc/Xazc"
-- 调用 Java 方法
luaj.callStaticMethod(className, "Share", '', '()V')
elseif device.platform == "ios" then
-- print('分享到ios')

end
end

 

 

--截屏代码 有一个咔嚓的动画
function ShowScene:screen()
local path = device.writablePath


local size = CCDirector:sharedDirector():getWinSize()
local screen = CCRenderTexture:create(size.width, size.height, 0)
local temp = CCDirector:sharedDirector():getRunningScene()
screen:begin()
temp:visit()
screen:endToLua()
local pathsave = path.."/share.jpg"

if screen:saveToFile('share.jpg', 0) == true then
print(pathsave)
end
local colorLayer1 = display.newColorLayer(ccc4(0, 0, 0, 125)):addTo(self)
colorLayer1:setAnchorPoint(ccp(0, 0))
colorLayer1:setPosition(ccp(0, display.height))


local colorLayer2 = display.newColorLayer(ccc4(0, 0, 0, 125)):addTo(self)
colorLayer2:setAnchorPoint(ccp(0, 0))
colorLayer2:setPosition(ccp(0, - display.height))


transition.moveTo(colorLayer1, {y = display.cy, time = 0.5})
self:performWithDelay(function ()
transition.moveTo(colorLayer1, {y = display.height, time = 0.3})
end, 0.5)


transition.moveTo(colorLayer2, {y = -display.cy, time = 0.5})
self:performWithDelay(function ()
transition.moveTo(colorLayer2, {y = -display.height, time = 0.3})
end, 0.5)


end

 

 

以上是quick中截屏并且保存到本地的代码 如果是android的系统的话 路径就是 /data/data/com.szfore.demo/files/share.jpg

这里只实现了android的分享, ios开发没弄过 不过 ios的分享应该是一样的原理 (好像ios的要用调用第三方的sdk才能进行分享没有android这样直接掉调用的api)

 

public class Demo extends Cocos2dxActivity {
static private Demo s_instance;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
s_instance = this;
}

static {
System.loadLibrary("game");
}

//分享到社交圈方法
public static void Share() {

new Thread(new Runnable(){
public void run() {

String filePath = "file:////data/data/" + s_instance.getApplicationInfo().packageName+ "/files/share.jpg";
Intent intent = new Intent("android.intent.action.SEND");
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
intent.putExtra(Intent.EXTRA_TEXT, "终于可以了!!!");
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse(filePath));
Log.i("debug", filePath);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
s_instance.startActivity(Intent.createChooser(intent, "分享"));
}
}).start();
}

}

以上是android中的分享代码 

 

 

就上面的代码就可以实现截屏分享到社交网络拉 

posted on 2014-08-11 10:38  &大飞  阅读(381)  评论(0)    收藏  举报

导航