分享模块,下载合集微调
This commit is contained in:
291
app/src/main/java/com/gh/common/util/ShareUtils.java
Normal file
291
app/src/main/java/com/gh/common/util/ShareUtils.java
Normal file
@ -0,0 +1,291 @@
|
||||
package com.gh.common.util;
|
||||
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.support.v7.widget.GridLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.Gravity;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.PopupWindow;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.gamecenter.R;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import cn.sharesdk.framework.Platform;
|
||||
import cn.sharesdk.framework.PlatformActionListener;
|
||||
import cn.sharesdk.framework.ShareSDK;
|
||||
import cn.sharesdk.sina.weibo.SinaWeibo;
|
||||
import cn.sharesdk.system.text.ShortMessage;
|
||||
import cn.sharesdk.tencent.qq.QQ;
|
||||
import cn.sharesdk.tencent.qzone.QZone;
|
||||
import cn.sharesdk.wechat.friends.Wechat;
|
||||
import cn.sharesdk.wechat.moments.WechatMoments;
|
||||
|
||||
/**
|
||||
* Created by khy on 2016/9/4.
|
||||
*/
|
||||
public class ShareUtils {
|
||||
private static ShareUtils instance;
|
||||
|
||||
private String shareUrl;
|
||||
private String shareTitle;
|
||||
private String shareIcon;
|
||||
|
||||
private int maxHeight;
|
||||
private int[] arrLogo = {R.drawable.share_wechat_logo, R.drawable.share_wechatmoments_logo, R.drawable.share_qq_logo
|
||||
, R.drawable.share_qzone_logo, R.drawable.share_sinaweibo_logo, R.drawable.share_shortmessage_logo
|
||||
, R.drawable.share_copyfont_logo, R.drawable.share_cancel_logo};
|
||||
private String[] arrLabel = {"微信好友", "朋友圈", "QQ好友", "QQ空间", "新浪微博", "短信", "复制链接", "取消"};
|
||||
|
||||
private PopupWindow popupWindow;
|
||||
|
||||
private Context context;
|
||||
|
||||
public static ShareUtils getInstance(Context context) {
|
||||
if (instance == null) {
|
||||
instance = new ShareUtils();
|
||||
}
|
||||
instance.context = context;
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void showShareWindows(View view, String url, String title, String icon){
|
||||
this.shareIcon = icon;
|
||||
this.shareTitle = title;
|
||||
this.shareUrl = url;
|
||||
|
||||
maxHeight = context.getResources().getDisplayMetrics().heightPixels;
|
||||
|
||||
RelativeLayout contentView = new RelativeLayout(context);
|
||||
contentView.setBackgroundColor(0x4c000000);
|
||||
contentView.setFocusable(true);
|
||||
contentView.setFocusableInTouchMode(true);
|
||||
|
||||
RecyclerView shareRecyclerView = new RecyclerView(context);
|
||||
shareRecyclerView.setPadding(DisplayUtils.dip2px(context, 4), 0, DisplayUtils.dip2px(context, 4), 0);
|
||||
shareRecyclerView.setBackgroundColor(Color.WHITE);
|
||||
|
||||
//RecyclerView禁止滑动
|
||||
GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 4){
|
||||
@Override
|
||||
public boolean canScrollVertically() {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
shareRecyclerView.setLayoutManager(gridLayoutManager);
|
||||
shareRecyclerView.setAdapter(new ShareRecyclerViewAdapter());
|
||||
|
||||
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
|
||||
, maxHeight/3 - DisplayUtils.dip2px(context, 10));
|
||||
rlParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
|
||||
contentView.addView(shareRecyclerView,rlParams);
|
||||
|
||||
popupWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT
|
||||
, LinearLayout.LayoutParams.MATCH_PARENT, true);
|
||||
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
|
||||
popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
|
||||
|
||||
contentView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
popupWindow.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
contentView.setOnKeyListener(new View.OnKeyListener() {
|
||||
@Override
|
||||
public boolean onKey(View v, int keyCode, KeyEvent event) {
|
||||
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK
|
||||
&& event.getRepeatCount() == 0 && popupWindow != null
|
||||
&& popupWindow.isShowing()) {
|
||||
popupWindow.dismiss();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class ShareRecyclerViewAdapter extends RecyclerView.Adapter<ShareRecyclerViewAdapter.ViewHolder>{
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
LinearLayout linearLayout = new LinearLayout(context);
|
||||
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, maxHeight/6 - DisplayUtils.dip2px(context, 5)));
|
||||
linearLayout.setOrientation(LinearLayout.VERTICAL);
|
||||
linearLayout.setGravity(Gravity.CENTER);
|
||||
linearLayout.setBackgroundResource(R.drawable.cardview_item_style);
|
||||
|
||||
ImageView shareLogo = new ImageView(context);
|
||||
shareLogo.setLayoutParams(new ViewGroup.LayoutParams(DisplayUtils.dip2px(context,45), DisplayUtils.dip2px(context,45)));
|
||||
|
||||
TextView shareLabel = new TextView(context);
|
||||
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
|
||||
layoutParams.setMargins(0, DisplayUtils.dip2px(context,10), 0, 0);
|
||||
shareLabel.setLayoutParams(layoutParams);
|
||||
shareLabel.setGravity(Gravity.CENTER);
|
||||
shareLabel.setTextColor(Color.parseColor("#3a3a3a"));
|
||||
shareLabel.setTextSize(13);
|
||||
|
||||
linearLayout.addView(shareLogo);
|
||||
linearLayout.addView(shareLabel);
|
||||
|
||||
return new ViewHolder(linearLayout);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, final int position) {
|
||||
holder.shareLogo.setImageResource(arrLogo[position]);
|
||||
holder.shareLabel.setText(arrLabel[position]);
|
||||
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
switch (position){
|
||||
case 0:
|
||||
wechatSahre(shareUrl, shareTitle, shareIcon);
|
||||
break;
|
||||
case 1:
|
||||
wechatMomentsSahre(shareUrl, shareTitle, shareIcon);
|
||||
break;
|
||||
case 2:
|
||||
qqSahre(shareUrl, shareTitle, shareIcon);
|
||||
break;
|
||||
case 3:
|
||||
qZoneSahre(shareUrl, shareTitle, shareIcon);
|
||||
break;
|
||||
case 4:
|
||||
sinaWeiboSahre(shareUrl, shareTitle, shareIcon);
|
||||
break;
|
||||
case 5:
|
||||
shortMessageSahre(shareUrl, shareTitle, shareIcon);
|
||||
break;
|
||||
case 6:
|
||||
copyFont(shareUrl);
|
||||
break;
|
||||
case 7:
|
||||
popupWindow.dismiss();
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return 8;
|
||||
}
|
||||
|
||||
public class ViewHolder extends RecyclerView.ViewHolder{
|
||||
LinearLayout linearLayout;
|
||||
TextView shareLabel;
|
||||
ImageView shareLogo;
|
||||
|
||||
public ViewHolder(View itemView) {
|
||||
super(itemView);
|
||||
linearLayout = (LinearLayout) itemView;
|
||||
shareLogo = (ImageView) linearLayout.getChildAt(0);
|
||||
shareLabel = (TextView) linearLayout.getChildAt(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//QQ分享
|
||||
private void qqSahre(String url, String title, String icon){
|
||||
QQ.ShareParams qqParams = new QQ.ShareParams();
|
||||
qqParams.setTitle(title);
|
||||
qqParams.setText(title + " " + url);
|
||||
qqParams.setImageUrl(icon);
|
||||
qqParams.setTitleUrl(url);
|
||||
qqParams.setSite("光环助手");
|
||||
sharePlatform(qqParams,QQ.NAME);
|
||||
}
|
||||
|
||||
//微信好友分享
|
||||
private void wechatSahre(String url, String title, String icon){
|
||||
Wechat.ShareParams wechatParams = new Wechat.ShareParams();
|
||||
wechatParams.setText(title + " " + url);
|
||||
wechatParams.setImageUrl(icon);
|
||||
wechatParams.setTitle(title);
|
||||
wechatParams.setUrl(url);
|
||||
sharePlatform(wechatParams,Wechat.NAME);
|
||||
}
|
||||
|
||||
//QQ空间分享
|
||||
private void qZoneSahre(String url, String title, String icon){
|
||||
QZone.ShareParams qZoneParams = new QZone.ShareParams();
|
||||
qZoneParams.setTitle(title);
|
||||
qZoneParams.setText(title + " " + url);
|
||||
qZoneParams.setImageUrl(icon);
|
||||
qZoneParams.setTitleUrl(url);
|
||||
qZoneParams.setComment("精彩尽在" + url);
|
||||
qZoneParams.setSite(context.getString(R.string.app_name));
|
||||
qZoneParams.setSiteUrl(url);
|
||||
sharePlatform(qZoneParams,QZone.NAME);
|
||||
}
|
||||
|
||||
//微信朋友圈分享
|
||||
private void wechatMomentsSahre(String url, String title, String icon){
|
||||
WechatMoments.ShareParams wechatMomentsParams = new WechatMoments.ShareParams();
|
||||
wechatMomentsParams.setText(title + " " + url);
|
||||
wechatMomentsParams.setTitle(title);
|
||||
wechatMomentsParams.setImageUrl(icon);
|
||||
wechatMomentsParams.setUrl(url);
|
||||
sharePlatform(wechatMomentsParams, WechatMoments.NAME);
|
||||
}
|
||||
|
||||
//新浪微博分享
|
||||
private void sinaWeiboSahre(String url, String title, String icon){
|
||||
SinaWeibo.ShareParams sinaWeiboParams = new SinaWeibo.ShareParams();
|
||||
sinaWeiboParams.setTitle(title);
|
||||
sinaWeiboParams.setImageUrl(icon);
|
||||
sinaWeiboParams.setUrl(url);
|
||||
sinaWeiboParams.setText(title + " " + url);
|
||||
sharePlatform(sinaWeiboParams, SinaWeibo.NAME);
|
||||
}
|
||||
|
||||
//短信分享
|
||||
private void shortMessageSahre(String url, String title, String icon){
|
||||
ShortMessage.ShareParams shortMessageParams = new ShortMessage.ShareParams();
|
||||
shortMessageParams.setText(title + " " + url);
|
||||
shortMessageParams.setUrl(url);
|
||||
shortMessageParams.setImageUrl(icon);
|
||||
sharePlatform(shortMessageParams, ShortMessage.NAME);
|
||||
}
|
||||
|
||||
//分享平台回调
|
||||
private void sharePlatform(Platform.ShareParams params, String name) {
|
||||
Platform platform = ShareSDK.getPlatform(name);
|
||||
platform.setPlatformActionListener(new PlatformActionListener() {
|
||||
@Override
|
||||
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
|
||||
Utils.log("分享成功");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Platform platform, int i, Throwable throwable) {
|
||||
Utils.log("分享失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancel(Platform platform, int i) {
|
||||
Utils.log("取消分享");
|
||||
}
|
||||
});
|
||||
platform.share(params);
|
||||
}
|
||||
|
||||
//复制文字链接
|
||||
private void copyFont(String copyContent) {
|
||||
ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
cmb.setText(copyContent);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user