您的位置:首页 > 理论基础 > 计算机网络

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

2014-03-28 23:40 531 查看
最近做游戏要用截屏分享的功能,于是在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)

<span style="white-space:pre">	</span>//分享到社交圈方法  
    public static void share(final String prms) {  
         new Thread(new Runnable(){  
             public void run() {  

                 //String filePath = "/data/data/" + mActivity.getApplicationInfo().packageName+ "/files/share.png";
                 //String filePath =  "file:" + mActivity.getFilesDir()+ "/share.png";
                 //String filePath = "/mnt//sdcard/share.png";  
            <span style="white-space:pre">	</span> //String newPath = copyImage(prms);
            <span style="white-space:pre">	</span> File f = new File(prms);  
            <span style="white-space:pre">	</span> f.setExecutable(true,false);
                 f.setReadable(true,false);
                 f.setWritable(true,false);

                 Intent intent = new Intent("android.intent.action.SEND");     
                 intent.setType("image/*");        
                 intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); 
                 intent.putExtra(Intent.EXTRA_TEXT, prms);  
                 if (f != null && f.exists() && f.isFile()) {    
                <span style="white-space:pre">	</span> Log.e("path ","exists");
                <span style="white-space:pre">	</span> intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(f)); 
                 } 
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
                 mActivity.startActivity(Intent.createChooser(intent, "分享"));   
             }  
         }).start();  
  } 
以上是android中的分享代码 

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

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息