友盟自定义分享-生成带图片的二维码,自定义分享布局
-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
public
class
UmengShare {
private
Activity activity =
null
;
private
UMSocialService mController =
null
;
private
Dialog shareDialog =
null
;
private
Bitmap bitmapCode =
null
;
public
UmengShare(Activity activity){
this
.activity = activity;
}
public
void
showShareUI(String shareContent){
// 首先在您的Activity中添加如下成员变量
mController = UMServiceFactory.getUMSocialService(
"com.umeng.share"
,RequestType.SOCIAL);
// 设置分享内容
if
(TextUtils.isEmpty(shareContent))
mController.setShareContent(
"体验最新应用"
);
else
mController.setShareContent(shareContent);
try
{
// bitmapCode = EncodingHandler.createQRCode(Const.URL.URL_SERVER_NEW, 200);
}
catch
(WriterException e) {
bitmapCode =
null
;
}
addWXShare();
addFriendCircleShare();
//设置新浪SSO handler
mController.getConfig().setSsoHandler(
new
SinaSsoHandler());
//设置腾讯微博SSO handler
mController.getConfig().setSsoHandler(
new
TencentWBSsoHandler());
mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
mController.getConfig().setPlatformOrder(SHARE_MEDIA.TENCENT,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.SINA,SHARE_MEDIA.WEIXIN_CIRCLE);
// mController.openShare(activity, false);
shareDialog =
new
Dialog(activity, R.style.Theme_Dialog);
shareDialog.setContentView(R.layout.share_popup_layout);
shareDialog.setCanceledOnTouchOutside(
true
);
ImageView codeImage = (ImageView)shareDialog.findViewById(R.id.share_imageview);
RelativeLayout tencentShare = (RelativeLayout)shareDialog.findViewById(R.id.tencent_layout);
RelativeLayout wxShare = (RelativeLayout)shareDialog.findViewById(R.id.wx_share_layout);
RelativeLayout sinashare = (RelativeLayout)shareDialog.findViewById(R.id.sina_share_layout);
RelativeLayout circleshare = (RelativeLayout)shareDialog.findViewById(R.id.circle_share_layout);
ShareOnClickListener listener =
new
ShareOnClickListener();
tencentShare.setOnClickListener(listener);
wxShare.setOnClickListener(listener);
sinashare.setOnClickListener(listener);
circleshare.setOnClickListener(listener);
if
(bitmapCode !=
null
){
codeImage.setImageBitmap(bitmapCode);
mController.setShareImage(
new
UMImage(activity, bitmapCode));
}
else
{
codeImage.setBackgroundResource(R.drawable.share_qr_code_image);
}
shareDialog.show();
}
/**
* 微信
*/
private
void
addWXShare() {
// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
String appID =
"<span style="
font-family: Arial, Helvetica, sans-serif;
">wx967daeb35fbeac</span><span style="
font-family: Arial, Helvetica, sans-serif;
">"
;</span>
// 添加微信平台
UMWXHandler wxHandler =
new
UMWXHandler(activity, appID);
wxHandler.addToSocialSDK();
//设置微信好友分享内容
WeiXinShareContent weixinContent =
new
WeiXinShareContent();
//设置分享文字
weixinContent.setShareContent(
"体验最新应用,边玩边赚钱!马上入驻【星云互动】"
);
//设置title
weixinContent.setTitle(
"星云互动"
);
//设置分享内容跳转URL
//设置分享图片
weixinContent.setShareImage(
new
UMImage(activity, bitmapCode));
mController.setShareMedia(weixinContent);
}
/**
* 微信朋友圈
*/
private
void
addFriendCircleShare() {
String appID =
"wx06c10cd07a5ac0e9"
;
// 支持微信朋友圈
UMWXHandler wxCircleHandler =
new
UMWXHandler(activity, appID);
wxCircleHandler.setToCircle(
true
);
wxCircleHandler.addToSocialSDK();
//设置微信好友分享内容
CircleShareContent circleMedia =
new
CircleShareContent ();
//设置分享文字
circleMedia.setShareContent(
"体验最新应用,边玩边赚钱!马上入驻【星云互动】"
);
//设置title
circleMedia.setTitle(
"星云互动"
);
//设置分享内容跳转URL
//设置分享图片
circleMedia.setShareImage(
new
UMImage(activity, bitmapCode));
mController.setShareMedia(circleMedia );
}
/**
* 分享
* @param shareType
*/
private
void
shareClick(SHARE_MEDIA shareType){
mController.postShare(activity, shareType,
new
SnsPostListener() {
@Override
public
void
onStart() {
ToastUtils.showTextToast(activity,
"开始分享."
);
}
@Override
public
void
onComplete(SHARE_MEDIA arg0,
int
eCode, SocializeEntity arg2) {
if
(bitmapCode !=
null
){
bitmapCode.recycle();
bitmapCode =
null
;
}
if
(eCode ==
200
) {
ToastUtils.showTextToast(activity,
"分享成功."
);
}
/*else {
String eMsg = "";
if (eCode == -101){
eMsg = "没有授权";
}
ToastUtils.showTextToast(activity, "分享失败[" + eCode + "] " + eMsg);
}*/
}
});
}
//分享按钮点击事件
protected
class
ShareOnClickListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
if
(shareDialog !=
null
&& shareDialog.isShowing()){
shareDialog.dismiss();
}
switch
(v.getId()) {
case
R.id.tencent_layout:
shareClick(SHARE_MEDIA.TENCENT);
break
;
case
R.id.wx_share_layout:
shareClick(SHARE_MEDIA.WEIXIN);
break
;
case
R.id.sina_share_layout:
shareClick(SHARE_MEDIA.SINA);
break
;
case
R.id.circle_share_layout:
shareClick(SHARE_MEDIA.WEIXIN_CIRCLE);
break
;
}
}
}
}
2、自定义布局
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166xpublic
class
UmengShare {
private
Activity activity =
null
;
private
UMSocialService mController =
null
;
private
Dialog shareDialog =
null
;
private
Bitmap bitmapCode =
null
;
public
UmengShare(Activity activity){
this
.activity = activity;
}
public
void
showShareUI(String shareContent){
// 首先在您的Activity中添加如下成员变量
mController = UMServiceFactory.getUMSocialService(
"com.umeng.share"
,RequestType.SOCIAL);
// 设置分享内容
if
(TextUtils.isEmpty(shareContent))
mController.setShareContent(
"体验最新应用"
);
else
mController.setShareContent(shareContent);
try
{
// bitmapCode = EncodingHandler.createQRCode(Const.URL.URL_SERVER_NEW, 200);
}
catch
(WriterException e) {
bitmapCode =
null
;
}
addWXShare();
addFriendCircleShare();
//设置新浪SSO handler
mController.getConfig().setSsoHandler(
new
SinaSsoHandler());
//设置腾讯微博SSO handler
mController.getConfig().setSsoHandler(
new
TencentWBSsoHandler());
mController.getConfig().removePlatform(SHARE_MEDIA.RENREN, SHARE_MEDIA.DOUBAN);
mController.getConfig().setPlatformOrder(SHARE_MEDIA.TENCENT,SHARE_MEDIA.WEIXIN,SHARE_MEDIA.SINA,SHARE_MEDIA.WEIXIN_CIRCLE);
// mController.openShare(activity, false);
shareDialog =
new
Dialog(activity, R.style.Theme_Dialog);
shareDialog.setContentView(R.layout.share_popup_layout);
shareDialog.setCanceledOnTouchOutside(
true
);
ImageView codeImage = (ImageView)shareDialog.findViewById(R.id.share_imageview);
RelativeLayout tencentShare = (RelativeLayout)shareDialog.findViewById(R.id.tencent_layout);
RelativeLayout wxShare = (RelativeLayout)shareDialog.findViewById(R.id.wx_share_layout);
RelativeLayout sinashare = (RelativeLayout)shareDialog.findViewById(R.id.sina_share_layout);
RelativeLayout circleshare = (RelativeLayout)shareDialog.findViewById(R.id.circle_share_layout);
ShareOnClickListener listener =
new
ShareOnClickListener();
tencentShare.setOnClickListener(listener);
wxShare.setOnClickListener(listener);
sinashare.setOnClickListener(listener);
circleshare.setOnClickListener(listener);
if
(bitmapCode !=
null
){
codeImage.setImageBitmap(bitmapCode);
mController.setShareImage(
new
UMImage(activity, bitmapCode));
}
else
{
codeImage.setBackgroundResource(R.drawable.share_qr_code_image);
}
shareDialog.show();
}
/**
* 微信
*/
private
void
addWXShare() {
// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
String appID =
"<span style="
font-family: Arial, Helvetica, sans-serif;
">wx967daeb35fbeac</span><span style="
font-family: Arial, Helvetica, sans-serif;
">"
;</span>
// 添加微信平台
UMWXHandler wxHandler =
new
UMWXHandler(activity, appID);
wxHandler.addToSocialSDK();
//设置微信好友分享内容
WeiXinShareContent weixinContent =
new
WeiXinShareContent();
//设置分享文字
weixinContent.setShareContent(
"体验最新应用,边玩边赚钱!马上入驻【星云互动】"
);
//设置title
weixinContent.setTitle(
"星云互动"
);
//设置分享内容跳转URL
//设置分享图片
weixinContent.setShareImage(
new
UMImage(activity, bitmapCode));
mController.setShareMedia(weixinContent);
}
/**
* 微信朋友圈
*/
private
void
addFriendCircleShare() {
String appID =
"wx06c10cd07a5ac0e9"
;
// 支持微信朋友圈
UMWXHandler wxCircleHandler =
new
UMWXHandler(activity, appID);
wxCircleHandler.setToCircle(
true
);
wxCircleHandler.addToSocialSDK();
//设置微信好友分享内容
CircleShareContent circleMedia =
new
CircleShareContent ();
//设置分享文字
circleMedia.setShareContent(
"体验最新应用,边玩边赚钱!马上入驻【星云互动】"
);
//设置title
circleMedia.setTitle(
"星云互动"
);
//设置分享内容跳转URL
//设置分享图片
circleMedia.setShareImage(
new
UMImage(activity, bitmapCode));
mController.setShareMedia(circleMedia );
}
/**
* 分享
* @param shareType
*/
private
void
shareClick(SHARE_MEDIA shareType){
mController.postShare(activity, shareType,
new
SnsPostListener() {
@Override
public
void
onStart() {
ToastUtils.showTextToast(activity,
"开始分享."
);
}
@Override
public
void
onComplete(SHARE_MEDIA arg0,
int
eCode, SocializeEntity arg2) {
if
(bitmapCode !=
null
){
bitmapCode.recycle();
bitmapCode =
null
;
}
if
(eCode ==
200
) {
ToastUtils.showTextToast(activity,
"分享成功."
);
}
/*else {
String eMsg = "";
if (eCode == -101){
eMsg = "没有授权";
}
ToastUtils.showTextToast(activity, "分享失败[" + eCode + "] " + eMsg);
}*/
}
});
}
//分享按钮点击事件
protected
class
ShareOnClickListener
implements
OnClickListener{
@Override
public
void
onClick(View v) {
if
(shareDialog !=
null
&& shareDialog.isShowing()){
shareDialog.dismiss();
}
switch
(v.getId()) {
case
R.id.tencent_layout:
shareClick(SHARE_MEDIA.TENCENT);
break
;
case
R.id.wx_share_layout:
shareClick(SHARE_MEDIA.WEIXIN);
break
;
case
R.id.sina_share_layout:
shareClick(SHARE_MEDIA.SINA);
break
;
case
R.id.circle_share_layout:
shareClick(SHARE_MEDIA.WEIXIN_CIRCLE);
break
;
}
}
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108public
final
class
EncodingHandler {
private
static
final
int
BLACK =
0xff000000
;
private
static
final
int
WHITE =
0xffffffff
;
public
static
Bitmap createQRCode(String str,
int
widthAndHeight)
throws
WriterException {
Hashtable<EncodeHintType, String> hints =
new
Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET,
"utf-8"
);
BitMatrix matrix =
new
MultiFormatWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
int
width = matrix.getWidth();
int
height = matrix.getHeight();
int
[] pixels =
new
int
[width * height];
for
(
int
y =
0
; y < height; y++) {
for
(
int
x =
0
; x < width; x++) {
if
(matrix.get(x, y)) {
pixels[y * width + x] = BLACK;
}
else
{
// 无信息设置像素点为白色
pixels[y * width + x] = WHITE;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels,
0
, width,
0
,
0
, width, height);
return
bitmap;
}
public
static
Bitmap createQRCodeWithLogo(Context context, String str,
int
widthAndHeight)
throws
WriterException {
QRCodeWriter writer =
new
QRCodeWriter();
Hashtable<EncodeHintType, String> hints =
new
Hashtable<EncodeHintType, String>();
hints.put(EncodeHintType.CHARACTER_SET,
"utf-8"
);
BitMatrix bitMatrix =
new
QRCodeWriter().encode(str,
BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight, hints);
int
[] pixels =
new
int
[widthAndHeight * widthAndHeight];
Bitmap[] bitmaps =
new
Bitmap[
2
];
bitmaps[
1
] = BitmapFactory.decodeResource(context.getResources(),
R.drawable.qrcode_logo);
// logo图标
int
imageW = bitmaps[
1
].getWidth();
int
imageH = bitmaps[
1
].getHeight();
int
startW = widthAndHeight /
2
- imageW /
2
;
int
starH = widthAndHeight /
2
- imageH /
2
;
for
(
int
y =
0
; y < widthAndHeight; y++) {
for
(
int
x =
0
; x < widthAndHeight; x++) {
if
((x <= startW || x >= starH + imageW)
|| (y <= starH || y >= +imageH)) {
if
(bitMatrix.get(x, y)) {
pixels[y * widthAndHeight + x] = BLACK;
}
}
else
{
}
}
}
Bitmap bitmap = Bitmap.createBitmap(widthAndHeight, widthAndHeight,
Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels,
0
, widthAndHeight,
0
,
0
, widthAndHeight, widthAndHeight);
bitmaps[
0
] = bitmap;
Bitmap bm = combineBitmaps(bitmaps, startW, starH);
return
bm;
}
private
static
Bitmap combineBitmaps(Bitmap[] bitmaps,
int
w,
int
h) {
// 添加logo
Bitmap newBitmap = Bitmap.createBitmap(bitmaps[
0
].getWidth(),
bitmaps[
0
].getHeight(), Config.ARGB_8888);
Canvas cv =
new
Canvas(newBitmap);
for
(
int
i =
0
; i < bitmaps.length; i++) {
if
(i ==
0
) {
cv.drawBitmap(bitmaps[
0
],
0
,
0
,
null
);
}
else
{
cv.drawBitmap(bitmaps[i], w, h,
null
);
}
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
}
return
newBitmap;
}
public
static
Bitmap gerateLogoCode(Context context, String str,
int
widthAndHeight)
throws
WriterException{
if
(!TextUtils.isEmpty(str)){
//根据字符串生成二维码图片并显示在界面上,第二个参数为图片的大小(600*600)
Bitmap qrCodeBitmap = createQRCode(str, widthAndHeight);
//------------------添加logo部分------------------//
Bitmap logoBmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.qrcode_logo);
//二维码和logo合并
Bitmap bitmap = Bitmap.createBitmap(qrCodeBitmap.getWidth(), qrCodeBitmap
.getHeight(), qrCodeBitmap.getConfig());
Canvas canvas =
new
Canvas(bitmap);
//二维码
canvas.drawBitmap(qrCodeBitmap,
0
,
0
,
null
);
//logo绘制在二维码中央
canvas.drawBitmap(logoBmp, qrCodeBitmap.getWidth() /
2
- logoBmp.getWidth() /
2
, qrCodeBitmap.getHeight()
/
2
- logoBmp.getHeight() /
2
,
null
);
return
bitmap;
}
return
null
;
}
}
4、微信自定义分享回调
123456789101112131415161718192021222324public
class
WXEntryActivity
extends
WXCallbackActivity{
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
}
//微信发送的请求将回调到onReq方法
@Override
public
void
onReq(BaseReq req) {
super
.onReq(req);
// ToastUtils.showTextToast(this, "WXEntryActivity onReq");
System.out.println(
"WXEntryActivity onReq"
);
}
//发送到微信请求的响应结果将回调到onResp方法
@Override
public
void
onResp(BaseResp resp) {
super
.onResp(resp);
// ToastUtils.showTextToast(this, "WXEntryActivity onResp");
System.out.println(
"WXEntryActivity onResp"
);
}
}