//需要引入sharesdk和在微網誌微信QQ提供的第三方平台上申請賬号,請自行查閱sharesdk官方文檔
//用于分享的資料,自行根據需求進行設定
public class ShareModel
{
private String address;
private String img;
private String title;
private String txt;
public String getTitle() {
return title;
}
public ShareModel(String address, String img, String title, String txt)
{
this.address = address;
this.img = img;
this.txt = txt;
this.title = title;
}
public String getAddress() {
return address;
}
public String getImg() {
return img;
}
public String getTxt() {
return txt;
}
}
public class ShareUtil
{
private static Context mContext;
private static String shareToastTxt;
public void doShare(Context context, final ShareModel ShareModel)
{
this.mContext = context;
final ShareCallBack shareCallBack = new ShareCallBack();
final ShareDialog ShareDialog = new ShareDialog(context);
ShareDialog.setCancelButtonOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
ShareDialog.dismiss();
}
});
ShareDialog.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
//設定分享内容
Platform.ShareParams sp = new Platform.ShareParams();
sp.setShareType(Platform.SHARE_WEBPAGE);
sp.setText(ShareModel.getTxt());
//微信郵箱和QQ空間使用
sp.setTitle(ShareModel.getTitle()); //分享文本
sp.setTitleUrl(ShareModel.getAddress());
//網絡圖檔rul
sp.setImageUrl(ShareModel.getImg());
//僅在QQ空間使用
sp.setTitleUrl(ShareModel.getAddress());
//僅在微信使用
sp.setUrl(ShareModel.getAddress());
//僅在QQ空間使用
sp.setSite(ShareModel.getTitle());
sp.setSiteUrl(ShareModel.getAddress());
if (item.get("ItemText").equals("微網誌"))
{
Platform sinaWeibo = ShareSDK.getPlatform(SinaWeibo.NAME);
shareToastTxt = "微網誌";
sinaWeibo.setPlatformActionListener(shareCallBack); // 設定分享事件回調
// 執行分享
sinaWeibo.share(sp);
}
else if (item.get("ItemText").equals("微信好友"))
{
Platform wechat = ShareSDK.getPlatform(Wechat.NAME);
shareToastTxt = "微信";
wechat.setPlatformActionListener(shareCallBack); // 設定分享事件回調
// 執行分享
wechat.share(sp);
}
else if (item.get("ItemText").equals("朋友圈"))
{
Platform wechatMoments = ShareSDK.getPlatform(WechatMoments.NAME);
shareToastTxt = "朋友圈";
wechatMoments.setPlatformActionListener(shareCallBack); // 設定分享事件回調
// 執行分享
wechatMoments.share(sp);
}
else if (item.get("ItemText").equals("QQ好友"))
{
Platform qq = ShareSDK.getPlatform(QQ.NAME);
shareToastTxt = "QQ";
qq.setPlatformActionListener(shareCallBack); // 設定分享事件回調
// 執行分享
qq.share(sp);
}
else if (item.get("ItemText").equals("QQ空間"))
{
Platform qzone = ShareSDK.getPlatform(QZone.NAME);
shareToastTxt = "空間";
qzone.setPlatformActionListener(shareCallBack); // 設定分享事件回調
// 執行分享
qzone.share(sp);
}
else if (item.get("ItemText").equals("郵箱"))
{
//設定分享内容
Platform.ShareParams sp1 = new Platform.ShareParams();
sp1.setShareType(Platform.SHARE_TEXT);
sp1.setTitle(ShareModel.getTitle());
sp1.setText(ShareModel.getTxt()+" "+ ShareModel.getAddress());
Platform email = ShareSDK.getPlatform(Email.NAME);
shareToastTxt = "郵箱";
email.setPlatformActionListener(shareCallBack); // 設定分享事件回調
// 執行分享
email.share(sp1);
}
else if(item.get("ItemText").equals("複制連結"))
{
ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
// 将文本内容放到系統剪貼闆裡。
cm.setText(ShareModel.getAddress());
Toast.makeText(mContext, "複制成功", Toast.LENGTH_LONG).show();
}
ShareDialog.dismiss();
}
});
}
//分享回調
private static class ShareCallBack implements PlatformActionListener
{
@Override
public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap)
{
Toast.makeText(mContext,shareToastTxt+"分享成功",Toast.LENGTH_SHORT).show();
}
@Override
public void onError(Platform platform, int i, Throwable throwable)
{
Toast.makeText(mContext,shareToastTxt+"分享失敗",Toast.LENGTH_SHORT).show();
Log.e("share_error",throwable.toString());
}
@Override
public void onCancel(Platform platform, int i)
{
Toast.makeText(mContext,shareToastTxt+"分享取消",Toast.LENGTH_SHORT).show();
}
}
}
public class ShareDialog
{
private Dialog dialog;
private GridView gridView;
private RelativeLayout cancelButton;
private SimpleAdapter saImageItems;
//此處是分享dialog用到的七個圖檔,存放順序與下方name相對應的順序需要保持一緻,請自行根據美工提供的圖檔進行替換
private int[] image = {R.drawable.share_sina, R.drawable.share_wx, R.drawable.share_friend, R.drawable.share_qq,R.drawable.share_qzne,R.drawable.share_email,R.drawable.share_copyline};
private String[] name = {"微網誌", "微信好友", "朋友圈", "QQ好友","QQ空間","郵箱","複制連結"};
public ChShareDialog(Context context)
{
dialog = new Dialog(context,R.style.MyAnimDialog);
dialog.show();
dialog.setCanceledOnTouchOutside(true);
Window window = dialog.getWindow();
window.setContentView(R.layout.ch_share_dialog);
if (window != null)
{
window.getDecorView().setPadding(0, WidgetUtil.dip2px(context,5), 0,0);
WindowManager.LayoutParams attr = window.getAttributes();
if (attr != null)
{
attr.height = ViewGroup.LayoutParams.WRAP_CONTENT;
attr.width = ViewGroup.LayoutParams.MATCH_PARENT;
//設定dialog 在布局中的位置
attr.gravity = Gravity.BOTTOM;
window.setAttributes(attr);
}
}
gridView = (GridView) window.findViewById(R.id.share_gridView);
cancelButton = (RelativeLayout) window.findViewById(R.id.share_cancel);
List<HashMap<String, Object>> shareList = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < image.length; i++)
{
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", image[i]);//添加圖像資源的ID
map.put("ItemText", name[i]);//按序号做ItemText
shareList.add(map);
}
saImageItems = new SimpleAdapter(context, shareList, R.layout.ch_share_item, new String[]{"ItemImage", "ItemText"}, new int[]{R.id.imageView1, R.id.textView1});
gridView.setAdapter(saImageItems);
}
public void setCancelButtonOnClickListener(OnClickListener Listener)
{
cancelButton.setOnClickListener(Listener);
}
public void setOnItemClickListener(OnItemClickListener listener)
{
gridView.setOnItemClickListener(listener);
}
/**
* 關閉對話框
*/
public void dismiss()
{
dialog.dismiss();
}
}
//用于分辨率轉化,dp與px的互相轉化
public class WidgetUtil
{
/**
* 根據手機的分辨率從 dp 的機關 轉成為 px(像素)
*/
public static int dip2px(Context context, float dpValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* 根據手機的分辨率從 px(像素) 的機關 轉成為 dp
*/
public static int px2dip(Context context, float pxValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
//分享dialog用到的動畫效果,請複制粘貼至styles檔案内
<style name="MyAnimDialog" parent="android:Theme.Dialog">
<!-- 背景顔色及透明程度 -->
<item name="android:windowBackground">@android:color/transparent</item>
<!-- 是否半透明 -->
<item name="android:windowIsTranslucent">false</item>
<!-- 是否沒有标題 -->
<item name="android:windowNoTitle">true</item>
<!-- 是否浮現在activity之上 -->
<item name="android:windowIsFloating">true</item>
<!-- 是否背景模糊 -->
<item name="android:backgroundDimEnabled">true</item>
<!-- 設定背景模糊的透明度-->
<item name="android:backgroundDimAmount">0.5</item>
<!-- 動畫 -->
<item name="android:windowAnimationStyle">@style/dialog_animation</item>
</style>
<!-- 對話框顯示和退出動畫 -->
<style name="dialog_animation">
<item name="android:windowEnterAnimation">@anim/dialog_enter</item>
<item name="android:windowExitAnimation">@anim/dialog_exit</item>
</style>
// dialog_exit.xml 放入anim檔案夾下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fillAfter="true"
android:fromYDelta="0%"
android:toYDelta="100%p"/>
</set>
// dialog_enter.xml 放入anim檔案夾下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fillAfter="true"
android:fromYDelta="100%p"
android:toYDelta="0%"/>
</set>
// share_dialog.xml shareDialog的布局檔案,請自行放入layout檔案夾
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ffffff"
android:layout_gravity="bottom"
android:orientation="vertical">
<GridView
android:paddingTop="20dp"
android:id="@+id/share_gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@color/white"
android:gravity="center"
android:horizontalSpacing="1dp"
android:listSelector="@android:color/transparent"
android:numColumns="4"
android:verticalSpacing="10dp"></GridView>
<RelativeLayout
android:id="@+id/share_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/share_gridView"
android:gravity="center"
android:padding="5dp"
android:background="@color/white"
android:visibility="visible">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:text="取消"
android:textColor="@color/black"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
使用方法:
ShareModel shareModel = new ShareModel();
shareModel.set...;//根據需要設定四個參數
ShareUtil.doShare(context,shareModel); //到此就OK啦,放到你按鍵響應事件下即可