PopupWindow實現任意位置動畫顯隱
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout; /**
* Author: zls.
* Created on 2016/4/22 21:02.
*/
public class PopupWindow { private static Handler handler;
private static xWindow popupWindow;
private static long DURATION = 0; public static void showAtLocation(Context context, View v, int x, int y){
showAtLocation(context, v, x, y, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
} public static void showAtLocation(Context context, View v, int x, int y, int[] anim){
int enterAnim = 0, exitAnim = 0;
if(anim.length > 0) enterAnim = anim[0];
if(anim.length > 1) exitAnim = anim[1];
showAtLocation(context, v, x, y, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, enterAnim, exitAnim);
} public static void showAtLocation(Context context, View v, int x, int y, int width, int height){
showAtLocation(context, v, x, y, width, height, 0, 0);
} public static void showAtLocation(Context context, View v, int x, int y, int width, int height, int enterAnim, int exitAnim){
if(context == null || !(context instanceof Activity) || null == v) return;
close();
initView(context, v, width, height, enterAnim, exitAnim);
popupWindow.showAtLocation(v, 0, x, y);
} public static void showAsDropDown(Context context, View anchor, final View v){
showAsDropDown(context, anchor, v, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
} public static void showAsDropDown(Context context, View anchor, View v, int[] anim){
int enterAnim = 0, exitAnim = 0;
if(anim.length > 0) enterAnim = anim[0];
if(anim.length > 1) exitAnim = anim[1];
showAsDropDown(context, anchor, v, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, enterAnim, exitAnim);
} public static void showAsDropDown(Context context, View anchor, View v, int width, int height){
showAsDropDown(context, anchor, v, width, height, 0, 0);
} public static void showAsDropDown(Context context, View anchor, View v, int width, int height, int enterAnim, int exitAnim){
if(context == null || !(context instanceof Activity) || null == v) return;
close();
initView(context, v, width, height, enterAnim, exitAnim);
popupWindow.showAsDropDown(anchor);
} private static void initView(Context context, final View v, int width, int height, int enterAnim, int exitAnim){
FrameLayout frame = new FrameLayout(context);
frame.addView(v);
popupWindow = new xWindow(frame, width, height);
popupWindow.setFocusable(true);
popupWindow.setTouchable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000"))); DURATION = 0;
if(exitAnim > 0) {
final Animation exitAnimation = AnimationUtils.loadAnimation(context, exitAnim);
DURATION = exitAnimation.getDuration();
popupWindow.setOnPreDismissListener(new Runnable() {
@Override
public void run() {
v.startAnimation(exitAnimation);
}
});
}
if(enterAnim > 0) {
Animation animation = AnimationUtils.loadAnimation(context, enterAnim);
v.startAnimation(animation);
}
handler = new Handler(context.getMainLooper());
} public static void close(){
if(popupWindow != null){
popupWindow.dismiss();
popupWindow = null;
handler = null;
}
} private static class xWindow extends android.widget.PopupWindow{ private Runnable onPreDismissListener; public xWindow(View v, int w, int h){
super(v, w, h);
} @Override
public void dismiss() {
if(null != onPreDismissListener){
onPreDismissListener.run();
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
xWindow.super.dismiss();
}
}, DURATION);
} public void setOnPreDismissListener(Runnable onPreDismissListener) {
this.onPreDismissListener = onPreDismissListener;
}
} } PS:第一篇博文,正在加班,沒辦法花很多時間來寫了,畢竟也比較簡單。因為在網上找不到相關的文章,所以只好通過自己的辦法去實現了,希望可以幫到一些剛入門的朋友。