package com.frank.ffmpeg.floating; import android.animation.TimeInterpolator; import android.content.Context; import android.support.annotation.LayoutRes; import android.support.annotation.MainThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import java.util.HashMap; import java.util.Map; /** * Created by yhao on 2017/12/22. * https://github.com/yhaolpz */ public class FloatWindow { private FloatWindow() { } private static final String mDefaultTag = "default_float_window_tag"; private static Map mFloatWindowMap; public static IFloatWindow get() { return get(mDefaultTag); } public static IFloatWindow get(@NonNull String tag) { return mFloatWindowMap == null ? null : mFloatWindowMap.get(tag); } @MainThread public static WindowBuilder with(@NonNull Context applicationContext) { return new WindowBuilder(applicationContext); } public static class WindowBuilder { Context mApplicationContext; View mView; private int mLayoutId; int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT; int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT; int gravity = Gravity.TOP | Gravity.START; int xOffset; int yOffset; boolean mShow = true; Class[] mActivities; int mMoveType = MoveType.fixed; long mDuration = 300; TimeInterpolator mInterpolator; private String mTag = mDefaultTag; WindowBuilder(Context applicationContext) { mApplicationContext = applicationContext; } public WindowBuilder setView(@NonNull View view) { mView = view; return this; } public WindowBuilder setView(@LayoutRes int layoutId) { mLayoutId = layoutId; return this; } public WindowBuilder setWidth(int width) { mWidth = width; return this; } public WindowBuilder setHeight(int height) { mHeight = height; return this; } public WindowBuilder setWidth(@Screen.screenType int screenType, float ratio) { mWidth = (int) ((screenType == Screen.width ? Util.getScreenWidth(mApplicationContext) : Util.getScreenHeight(mApplicationContext)) * ratio); return this; } public WindowBuilder setHeight(@Screen.screenType int screenType, float ratio) { mHeight = (int) ((screenType == Screen.width ? Util.getScreenWidth(mApplicationContext) : Util.getScreenHeight(mApplicationContext)) * ratio); return this; } public WindowBuilder setX(int x) { xOffset = x; return this; } public WindowBuilder setY(int y) { yOffset = y; return this; } public WindowBuilder setX(@Screen.screenType int screenType, float ratio) { xOffset = (int) ((screenType == Screen.width ? Util.getScreenWidth(mApplicationContext) : Util.getScreenHeight(mApplicationContext)) * ratio); return this; } public WindowBuilder setY(@Screen.screenType int screenType, float ratio) { yOffset = (int) ((screenType == Screen.width ? Util.getScreenWidth(mApplicationContext) : Util.getScreenHeight(mApplicationContext)) * ratio); return this; } /** * 设置 Activity 过滤器,用于指定在哪些界面显示悬浮窗,默认全部界面都显示 * * @param show  过滤类型,子类类型也会生效 * @param activities  过滤界面 */ public WindowBuilder setFilter(boolean show, @NonNull Class... activities) { mShow = show; mActivities = activities; return this; } public WindowBuilder setMoveType(@MoveType.MOVE_TYPE int moveType) { mMoveType = moveType; return this; } public WindowBuilder setMoveStyle(long duration, @Nullable TimeInterpolator interpolator) { mDuration = duration; mInterpolator = interpolator; return this; } public WindowBuilder setTag(@NonNull String tag) { mTag = tag; return this; } public void build() { if (mFloatWindowMap == null) { mFloatWindowMap = new HashMap<>(); } if (mFloatWindowMap.containsKey(mTag)) { throw new IllegalArgumentException("FloatWindow of this tag has been added, Please set a new tag for the new FloatWindow"); } if (mView == null && mLayoutId == 0) { throw new IllegalArgumentException("View has not been set!"); } if (mView == null) { mView = Util.inflate(mApplicationContext, mLayoutId); } IFloatWindow floatWindowImpl = new IFloatWindowImpl(this); mFloatWindowMap.put(mTag, floatWindowImpl); } } }