CocosCreator-【微信SDK】Android平台-An
2018-03-26 本文已影响140人
伏波Rinnsio1xy
之前的笔记解决了android的登录,接下来的是分享,分享有5种类型的分享,分享链接到好友、分享链接到朋友圈、截图分享、文字分享到好友、文字分享到朋友圈。其中微信分享的缩略图大小不能超过32k, 大图的大小好像也不能太大,我用截图为1280x720的图,分享不出去,修改为:768x432 可以分享,这个大小基本还是可以满足需求了。
特别说明:待我把android和ios的微信的所有模块接完以后会在github建一个仓库,到时候大家就可以直接用里面的代码了。
一、Android端的java的微信分享代码:
java-分享链接:
//type:1 好友 2 朋友圈
public static boolean ShareLinkToWeChat(String link, String title, String desc, int type)
{
WXWebpageObject webpage = new WXWebpageObject();
webpage.webpageUrl = link;
WXMediaMessage msg = new WXMediaMessage(webpage);
msg.title = title;
msg.description = desc;
//图片<32k
InputStream stream = WXEntryActivity.activity.getResources().openRawResource(R.drawable.share_icon);
Bitmap bitmap = BitmapFactory.decodeStream(stream);
msg.thumbData = Util.bmpToByteArray(bitmap, true);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "Share_Linke_Type" + type;
req.message = msg;
if (type == 1)
{
req.scene = SendMessageToWX.Req.WXSceneSession;
}else
{
req.scene = SendMessageToWX.Req.WXSceneTimeline;
}
return api.sendReq(req);
}
java-分享文字:
//type:1 好友 2 朋友圈
public static boolean ShareToWeChat(String content_txt, int type)
{
WXTextObject textObj = new WXTextObject();
textObj.text = content_txt;
WXMediaMessage msg = new WXMediaMessage();
msg.mediaObject = textObj;
msg.description = content_txt;
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "Share_Text_Type" + type;
req.message = msg;
if (type == 1)
{
req.scene = SendMessageToWX.Req.WXSceneSession;
}else
{
req.scene = SendMessageToWX.Req.WXSceneTimeline;
}
return api.sendReq(req);
}
java-压缩截图:
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
java-压缩缩略图:
public static byte[] bitmap2BytesMini(Bitmap bitmap, int maxkb) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
int options = 100;
while (output.toByteArray().length > maxkb&& options != 10) {
output.reset();
bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);
options -= 10;
}
bitmap.recycle();
return output.toByteArray();
}
一、链接分享到微信好友或者朋友圈
Cocos Creator客户端js代码:
var ANDROID_CLASS_NAME = "org/cocos/unkchess/wxapi/WXEntryActivity"
var JAVA_STRING = "Ljava/lang/String;"
// 分享到好友聊天
// @share_link 分享链接
// @title 标题
// @desc 分享内容
ShareToFriend:function(share_link, title, desc){
var _type = 1 // 1:聊天分享 2:朋友圈分享
var _methond_name = "ShareLinkToWeChat"
var _methond_singnature = "(" + JAVA_STRING + JAVA_STRING + JAVA_STRING +"I)Z"
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_link, title, desc, _type)
},
// 分享到朋友圈
// @share_link 分享链接
// @title 标题
// @desc 分享内容
ShareToFriendCircle:function(share_link, title, desc){
this.Log("ShareToFriendCircle")
var _type = 2 // 1:聊天分享 2:朋友圈分享
var _methond_name = "ShareLinkToWeChat"
var _methond_singnature = "(" + JAVA_STRING + JAVA_STRING + JAVA_STRING +"I)Z"
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_link, title, desc, _type)
},
二、文字分享到微信好友或者朋友圈
// 微信文字内容分享到好友
// @text 分享的文字内容
ShareTextTotFriends:function(text){
var _type = 1
var _methond_name = "ShareToWeChat"
var _methond_singnature = "("+ JAVA_STRING +"I)Z"
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, text, _type)
},
// 微信文字内容分享到朋友圈
// @text 分享的文字内容
ShareTextTotFriendCircle:function(text){
var _type = 2
var _methond_name = "ShareToWeChat"
var _methond_singnature = "("+ JAVA_STRING +"I)Z"
jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, text, _type)
},
三、截图分享
Cocos Creator客户端截图代码:
// 微信截图分享
// @share_path 截图的路径
ShareImageToWeChat:function(share_path){
this.Log("ShareImageToWeChat")
var _methond_name = "ShareImageToWeChat"
var _methond_singnature = "("+ JAVA_STRING +")Z"
var _bShareResult = jsb.reflection.callStaticMethod(ANDROID_CLASS_NAME, _methond_name, _methond_singnature, share_path)
if(_bShareResult){
this.Log("wechat share image success")
} else {
this.Log("wechat share image failed")
}
},
// 截图分享
shareCaptureScreen: function () {
// 注意,EditBox,VideoPlayer,Webview 等控件无法被包含在截图里面
// 因为这是 OpenGL 的渲染到纹理的功能,上面提到的控件不是由引擎绘制的
if(CC_JSB) {
var renderTexture = cc.RenderTexture.create(1280,720, cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);
var self = this
// 实际截屏的代码
renderTexture.begin();
cc.director.getScene()._sgNode.visit();
renderTexture.end();
renderTexture.saveToFile("wechat_share_image_screenshot.png",cc.ImageFormat.PNG, true, function () {
// 调用微信sdk接口
var _share_path = jsb.fileUtils.getWritablePath()
self.ShareImageToWeChat(_share_path)
});
}
},
Android端截图分享java代码:
public static boolean ShareImageToWeChat(String path)
{
String full_img_path = path + "wechat_share_image_screenshot.png";
WXImageObject imageObject = new WXImageObject();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap tmp = BitmapFactory.decodeFile(full_img_path);
Bitmap thBitmap = Bitmap.createScaledBitmap(tmp, 768, 432, true);
imageObject.imageData = Util.bmpToByteArray(thBitmap, true);
WXMediaMessage msg = new WXMediaMessage(imageObject);
// 缩略图 < 32k 不然拉取不了微信客户端
Bitmap mini_bmp = Bitmap.createScaledBitmap(tmp, 160, 90, true);
msg.thumbData = Util.bitmap2BytesMini(mini_bmp, 30);
SendMessageToWX.Req req = new SendMessageToWX.Req();
req.transaction = "Share_Image_Type";
req.message = msg;
req.scene = SendMessageToWX.Req.WXSceneSession;
return api.sendReq(req);
}
以上都是纯代码,除了图片大小以为,没什么其他需要注意的地方,比较简单。下一篇是IOS端的微信分享笔记。