SharePopupWindow.java
7.43 KB
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
167
168
169
170
171
172
173
package com.cnlive.goldenline.ui.widget;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.PopupWindow;
import com.cnlive.goldenline.R;
import com.cnlive.goldenline.util.ShareSdkUtil;
import com.cnlive.goldenline.util.ToastUtil;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import static com.cnlive.goldenline.util.LogUtils.LOGE;
public class SharePopupWindow extends PopupWindow implements OnClickListener {
private View mShareSina;
private View mShareWeixin;
private View mShareFriend;
private View mShareqq;
private Button btn_cancel;
private View mMenuView;
private String content, title, img;
private int type;
private Context context;
private String targetUrl;
private Configuration mConfiguration;
private int preOrientation;
@NonNull
private ViewTreeObserver.OnGlobalLayoutListener heightListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int curOrientation = mConfiguration.orientation; //获取屏幕方向
if (preOrientation != curOrientation) {//屏幕旋转时隐藏
dismiss();
}
}
};
public SharePopupWindow(@NonNull final View view_parent, @NonNull final Activity context, int type, String title, String content, String img, String targetUrl) {
super(context);
this.title = title;
this.type = type;
this.img = img;
this.content = content;
this.context = context;
this.targetUrl = targetUrl;
mConfiguration = context.getResources().getConfiguration(); //获取设置的配置信息
preOrientation = mConfiguration.orientation;
mMenuView = context.getLayoutInflater().inflate(R.layout.select_share_platform_alert_dialog, null);
if (preOrientation == 2) {
mMenuView = context.getLayoutInflater().inflate(
R.layout.select_share_platform_alert_dialog_landscape, null);
// mMenuView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
mShareSina = mMenuView.findViewById(R.id.share_sina);
mShareSina.setOnClickListener(this);
mShareWeixin = mMenuView.findViewById(R.id.share_weiXin);
mShareWeixin.setOnClickListener(this);
mShareFriend = mMenuView.findViewById(R.id.share_circle);
mShareFriend.setOnClickListener(this);
mShareqq = mMenuView.findViewById(R.id.share_qq);
mShareqq.setOnClickListener(this);
btn_cancel = (Button) mMenuView.findViewById(R.id.btn_cancel);
btn_cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (mMenuView != null ) {
mMenuView.setAnimation(AnimationUtils.loadAnimation(mMenuView.getContext(), R.anim.popup_right));
}
dismiss();
}
});
this.setContentView(mMenuView);
this.setWidth(LayoutParams.MATCH_PARENT);
this.setHeight(LayoutParams.MATCH_PARENT);
this.setFocusable(true);
this.setAnimationStyle(R.style.CustomDialogStyle);
ColorDrawable dw = new ColorDrawable(0x50000000);
this.setBackgroundDrawable(dw);
showAtLocation(view_parent, Gravity.TOP, 0, 0);
this.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
view_parent.getViewTreeObserver().removeGlobalOnLayoutListener(heightListener);
}
});
view_parent.getViewTreeObserver().addOnGlobalLayoutListener(heightListener);
mMenuView.setAnimation(AnimationUtils.loadAnimation(mMenuView.getContext(), R.anim.popup_in));
if (preOrientation == 2) {
mMenuView.setAnimation(AnimationUtils.loadAnimation(mMenuView.getContext(), R.anim.popup_right));
}
mMenuView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(@NonNull View v, @NonNull MotionEvent event) {
int height = mMenuView.findViewById(R.id.pop_layout).getTop();
int left = mMenuView.findViewById(R.id.pop_layout).getLeft();
int y = (int) event.getY();
int x = (int) event.getX();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height || (x < left)) {
v.performClick();
dismiss();
}
}
return true;
}
});
}
@Override
public void onClick(@NonNull View v) {
LOGE(" share type=" + type + ",title=" + title + " , content=" + content + ",img=" + img + " targetUrl=" + targetUrl);
switch (v.getId()) {
case R.id.share_sina:
LOGE(" share 新浪 ");
if (ShareSdkUtil.isAppAvilible(context, "com.sina.weibo") ||
ShareSdkUtil.isAppAvilible(context, "com.sina.weibotab") || ShareSdkUtil.isAppAvilible(context, "com.sina.weibog3")) {
ShareSdkUtil.sharePlatform(context, SinaWeibo.NAME, type, title, content, img, targetUrl);
} else {
ToastUtil.showToast(context.getString(R.string.toast_msg_app_not_install));
}
break;
case R.id.share_weiXin:
LOGE(" share 微信 ");
if (ShareSdkUtil.isAppAvilible(context, "com.tencent.mm")) {
ShareSdkUtil.sharePlatform(context, Wechat.NAME, type, title, content, img, targetUrl);
} else {
ToastUtil.showToast(context.getString(R.string.toast_msg_app_not_install));
}
break;
case R.id.share_circle:
LOGE(" share 朋友圈 ");
if (ShareSdkUtil.isAppAvilible(context, "com.tencent.mm")) {
ShareSdkUtil.sharePlatform(context, WechatMoments.NAME, type, title, content, img, targetUrl);
} else {
ToastUtil.showToast(context.getString(R.string.toast_msg_app_not_install));
}
break;
case R.id.share_qq:
LOGE(" share QQ ");
if (ShareSdkUtil.isAppAvilible(context, "com.tencent.mobileqq") ||
ShareSdkUtil.isAppAvilible(context, "com.tencent.mobileqqi") ||
ShareSdkUtil.isAppAvilible(context, "com.tencent.minihd.qq") ||
ShareSdkUtil.isAppAvilible(context, "com.tencent.qq.kddi")) {
ShareSdkUtil.sharePlatform(context, QZone.NAME, type, title, content, img, targetUrl);
} else {
ToastUtil.showToast(context.getString(R.string.toast_msg_app_not_install));
}
break;
}
}
}