码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-13 20:22:56      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

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、自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="340dp"
    android:layout_height="340dp"
    android:layout_gravity="center"
    android:background="@color/white" >

    <TextView
        android:id="@+id/tuijian_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_layout"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:text="分享给好友"
        android:textColor="@color/blue_shallow"
        android:textSize="@dimen/font_body_20" />

    <View
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_below="@id/tuijian_textview"
        android:layout_marginTop="10dp"
        android:background="@color/blue_shallow" />

    <ImageView
        android:id="@+id/share_imageview"
        android:layout_width="170dp"
        android:layout_height="170dp"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/line1"
        android:layout_centerHorizontal="true"
        android:scaleType="fitXY" />

	<TextView
	    android:id="@+id/load_saomiao_name"
	    android:layout_width="wrap_content"
	    android:layout_height="26dp"
	    android:layout_below="@+id/share_imageview"
	    android:layout_centerHorizontal="true"
	    android:layout_marginBottom="4dp"
	    android:background="@color/white"
	    android:gravity="center"
	    android:singleLine="true"
	    android:text="扫描二维码,立即下载"
	    android:textColor="@android:color/darker_gray"
	    android:textSize="14sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/share_scrollView"
        android:layout_marginTop="10dp"
        android:background="@color/gray_line" />

    <ScrollView
        android:id="@+id/share_scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@android:color/transparent" >

        <LinearLayout
            android:id="@+id/share_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:orientation="horizontal" >
            
            <RelativeLayout
                android:id="@+id/tencent_layout"
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:layout_weight="1"
                android:background="@drawable/umeng_socialize_shareboard_item_background" >

                <ImageView
                    android:id="@+id/tencent_share_image"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/umeng_socialize_tx_on" />

                <TextView
                    android:id="@+id/tencent_share_name"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="@android:color/transparent"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="腾讯微博"
                    android:textSize="12sp"
                    android:textColor="@color/blue_shallow" />
            </RelativeLayout>
			<RelativeLayout
			    android:id="@+id/wx_share_layout"
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:layout_weight="1"
                android:background="@drawable/umeng_socialize_shareboard_item_background" >

                <ImageView
                    android:id="@+id/wx_share_image"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/umeng_socialize_wechat" />

                <TextView
                    android:id="@+id/wx_share_name"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="@android:color/transparent"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="微信"
                    android:textSize="12sp"
                    android:textColor="@color/blue_shallow" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/sina_share_layout"
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:layout_weight="1"
                android:background="@drawable/umeng_socialize_shareboard_item_background" >

                <ImageView
                    android:id="@+id/sina_share_image"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/umeng_socialize_sina_on" />

                <TextView
                    android:id="@+id/sina_share_name"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="@android:color/transparent"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="新浪微博"
                    android:textSize="12sp"
                    android:textColor="@color/blue_shallow" />
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/circle_share_layout"
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:layout_weight="1"
                android:background="@drawable/umeng_socialize_shareboard_item_background" >

                <ImageView
                    android:id="@+id/circle_share_image"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="10dp"
                    android:scaleType="fitXY"
                    android:background="@drawable/umeng_socialize_wxcircle" />

                <TextView
                    android:id="@+id/circle_share_name"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="4dp"
                    android:layout_marginLeft="2dp"
                    android:layout_marginRight="2dp"
                    android:background="@android:color/transparent"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="朋友圈"
                    android:textSize="12sp"
                    android:textColor="@color/blue_shallow" />
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

3、生成二维码图片

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、微信自定义分享回调

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");
	}
	
}


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

标签:

原文地址:http://www.cnblogs.com/lbangel/p/4335871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!