友盟自定义分享-生成带图片的二维码,自定义分享布局

 

http://www.2cto.com/weixin/201503/383512.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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.gerateLogoCode(activity,"http://www.baidu.com/", 200);
//          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.setTargetUrl("http://www.scloudm.com/");
        //设置分享图片
        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.setTargetUrl("http://www.scloudm.com/");
        //设置分享图片
        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、自定义布局

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
xpublic 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.gerateLogoCode(activity,"http://www.baidu.com/", 200);
//          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.setTargetUrl("http://www.scloudm.com/");
        //设置分享图片
        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.setTargetUrl("http://www.scloudm.com/");
        //设置分享图片
        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;
            }
        }
    }
     
     
}
3、生成二维码图片

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
public 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、微信自定义分享回调

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public 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");
    }
     
}

 

 

posted @ 2015-07-01 17:31  新感觉  阅读(552)  评论(0编辑  收藏  举报