Skip to content

Commit

Permalink
新增 dismiss 方法及其他方案可配置化,修复 Activity bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yhaowa committed Nov 27, 2017
1 parent 36de242 commit 2ef5520
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 72 deletions.
5 changes: 1 addition & 4 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Andorid 任意界面悬浮窗,适配 4.x~7.1 及各大国产机型,无需申

6.位置不可变、Andorid 4.4 以下无法接收触摸事件

7.可灵活选择其他方案,如:所有版本都申请权限 等

集成:
===
Expand All @@ -35,7 +36,7 @@ Andorid 任意界面悬浮窗,适配 4.x~7.1 及各大国产机型,无需申

```
dependencies {
compile 'com.github.yhaolpz:FixedFloatWindow:1.0.2'
compile 'com.github.yhaolpz:FixedFloatWindow:1.0.3'
}
```

Expand All @@ -44,23 +45,31 @@ Andorid 任意界面悬浮窗,适配 4.x~7.1 及各大国产机型,无需申

```java

Button button = new Button(getApplicationContext());
button.setText("悬浮按钮");
button.setBackgroundColor(getResources().getColor(R.color.colorAccent));


FixedFloatWindow fixedFloatWindow = new FixedFloatWindow(getApplicationContext());
fixedFloatWindow.setView(button);
fixedFloatWindow.setView(view);
fixedFloatWindow.setGravity(Gravity.RIGHT | Gravity.TOP, 100, 150);

fixedFloatWindow.show();
// fixedFloatWindow.hide();
// fixedFloatWindow.dismiss();
```


效果:
===

![悬浮按钮图](https://raw.githubusercontent.com/yhaolpz/FixedFloatWindow/master/img.jpg)
![悬浮按钮图](https://raw.githubusercontent.com/yhaolpz/FixedFloatWindow/master/img.gif)


如果要选择其他方案,比如所有版本都申请权限,且在内部自动申请,只需在实例化时选择对应类型即可:

```java

FixedFloatWindow fixedFloatWindow = new FixedFloatWindow(
getApplicationContext(), FixedFloatWindow.ALL_AUTO_REQ );

```

共提供 5 种方案供大家测试、使用,其他方案见 FixedFloatWindow ,注释很详细。



Expand Down Expand Up @@ -100,7 +109,7 @@ Andorid 任意界面悬浮窗,适配 4.x~7.1 及各大国产机型,无需申

7.0 以下采用自定义 toast, 7.1及以上申请权限

本库即基于此方案
本库中默认方式即为此最终选择方案



Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.example.fixedfloatwindow;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;

/**
* 用于在内部自动申请权限
*/

public class FixedFloatActivity extends AppCompatActivity {
public class FixedFloatActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class FixedFloatPhone implements FixedFloatView {

private final Context mContext;

private int mWidth = FixedFloatWindow.WRAP_CONTENT;
private int mHeight = FixedFloatWindow.WRAP_CONTENT;
private int mWidth;
private int mHeight;

private final WindowManager mWindowManager;
private final WindowManager.LayoutParams mLayoutParams;
Expand Down Expand Up @@ -81,14 +81,14 @@ public void onFail() {
Intent intent = new Intent(mContext, FixedFloatActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);
}else{
} else {
throw new IllegalArgumentException("请将 FixedFloatWindow 设置为自动申请权限,或自行申请权限!");
}
}
}

@Override
public void hide() {
public void dismiss() {
mWindowManager.removeView(mView);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FixedFloatToast implements FixedFloatView {
private Method show;
private Method hide;

private int mWidth = FixedFloatWindow.WRAP_CONTENT;
private int mHeight = FixedFloatWindow.WRAP_CONTENT;
private int mWidth;
private int mHeight;


public FixedFloatToast(Context applicationContext) {
Expand Down Expand Up @@ -58,7 +58,7 @@ public void show() {
}


public void hide() {
public void dismiss() {
try {
hide.invoke(mTN);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

/**
* Created by yhao on 17-11-14.
*
*/

interface FixedFloatView {
Expand Down Expand Up @@ -33,8 +34,11 @@ interface FixedFloatView {
void show();



/**
* 隐藏悬浮控件
* 销毁悬浮控件
*/
void hide();
void dismiss();


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.IntDef;
import android.view.LayoutInflater;
import android.view.View;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

/**
Expand All @@ -18,58 +21,147 @@

public class FixedFloatWindow implements FixedFloatView {


/**
* 默认类型,7.1 以下采用自定义 toast 方式跳过申请权限,7.1 及以上内部自动申请权限 , 推荐
*/
public static final int AUTO_REQ = 0;


/**
* 7.1 以下采用自定义 toast 方式跳过权限申请;7.1 及以上需用户自己申请权限,否则报错
*/
public static final int ME_REQ = 1;


/**
* 所有版本都申请权限,内部自动申请
*/
public static final int ALL_AUTO_REQ = 2;


/**
* 所有版本都申请权限,需要用户自己申请权限,否则报错
*/
public static final int ALL_ME_REQ = 3;


/**
* 所有版本都采用自定义 toast 方式跳过权限申请,不兼容 7.1 及以上版本,不推荐
*/
public static final int NO_REQ = 4;


/**
* 悬浮窗控件尺寸
*/
public static final int MATCH_PARENT = -1;
public static final int WRAP_CONTENT = -2;

private FixedFloatView mFixedFloatView;
private boolean isShow = false;
static PermissionListener mPermissionListener;


public FixedFloatWindow(Context applicationContext) {
this(applicationContext, true);
this(applicationContext, AUTO_REQ);
}


/**
* @param applicationContext Application 上下文
* @param autoReqPermission true将在内部自动请求权限;false需要用户自己申请权限,默认true
* @param type 申请权限类型,见注释
*/
public FixedFloatWindow(Context applicationContext, boolean autoReqPermission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
mFixedFloatView = new FixedFloatPhone(applicationContext, autoReqPermission);
} else {
mFixedFloatView = new FixedFloatToast(applicationContext);
public FixedFloatWindow(Context applicationContext, @TYPE int type) {
switch (type) {
case AUTO_REQ:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
mFixedFloatView = new FixedFloatPhone(applicationContext, true);
} else {
mFixedFloatView = new FixedFloatToast(applicationContext);
}
break;
case ME_REQ:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
mFixedFloatView = new FixedFloatPhone(applicationContext, false);
} else {
mFixedFloatView = new FixedFloatToast(applicationContext);
}
break;
case ALL_AUTO_REQ:
mFixedFloatView = new FixedFloatPhone(applicationContext, true);
break;
case ALL_ME_REQ:
mFixedFloatView = new FixedFloatPhone(applicationContext, false);
break;
case NO_REQ:
mFixedFloatView = new FixedFloatToast(applicationContext);
break;
}
}


/**
* 设置要悬浮的视图
*
* @param view 要悬浮的视图
* @param width 悬浮视图宽
* @param height 悬浮视图高
*/
@Override
public void setView(View view, int width, int height) {
mFixedFloatView.setView(view, width, height);
mView = view;
}

/**
* 设置要悬浮的视图
*
* @param view 要悬浮的视图
*/
@Override
public void setView(View view) {
mFixedFloatView.setView(view);
this.setView(view, WRAP_CONTENT, WRAP_CONTENT);
}

/**
* 设置悬浮窗显示位置
*
* @param gravity Gravity.TOP 等
* @param xOffset 偏移距离
* @param yOffset 偏移距离
*/
@Override
public void setGravity(int gravity, int xOffset, int yOffset) {
mFixedFloatView.setGravity(gravity, xOffset, yOffset);

}

/**
* 显示悬浮窗
*/
@Override
public void show() {
if (isShow) return;
mFixedFloatView.show();
isShow = true;
if (once) {
mFixedFloatView.show();
once = false;
} else {
if (isShow) return;
mView.setVisibility(View.VISIBLE);
isShow = true;
}
}

@Override
/**
* 隐藏悬浮窗
*/
public void hide() {
if (!isShow) return;
mFixedFloatView.hide();
if (once || !isShow) return;
mView.setVisibility(View.INVISIBLE);
isShow = false;
}

/**
* 销毁悬浮窗,调用后不可再通过 show 方法显示,一般情况下不需要调用
*/
@Override
public void dismiss() {
mFixedFloatView.dismiss();
isShow = false;
}

Expand Down Expand Up @@ -103,4 +195,17 @@ public static boolean hasPermission(Context context) {
}
return result;
}


private FixedFloatView mFixedFloatView;
static PermissionListener mPermissionListener;
private boolean isShow;
private boolean once = true;
private View mView;

@IntDef({AUTO_REQ, ME_REQ, ALL_AUTO_REQ, ALL_ME_REQ, NO_REQ})
@Retention(RetentionPolicy.SOURCE)
private @interface TYPE {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.fixedfloatwindow;

/**
* Created by yhao on 2017/11/27.
* https://github.com/yhaolpz
*/

public enum FloatType {
}
Binary file added img.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed img.jpg
Binary file not shown.
Loading

0 comments on commit 2ef5520

Please sign in to comment.