公司新立了个浏览器项目,所有界面和功能基本上时仿UC做的,我负责的工具箱模块中有全屏功能,实现实时切换全屏状态不难,难的是如何实现所有界面统一切换全屏功能,忽然想起前几天研究换肤功能时看到过的项目,项目中实现日间/夜间模式切换功能所用的逻辑正式我需要的,没错,在借鉴(Copy)代码后完美实现了自己项目的全屏切换功能,特此记录分享。
1.新建ScreenStateChangeObserver接口,即全屏状态改变观察者接口
/**
* ====================================
* 作 者:Ares(颜崔)
* 地 址:https://github.com/Ares-yc
* 描 述:屏幕状态改变观察者接口
* 版 本:1.0.0
* 创建时间:2017/8/23/023.
* 修改时间:2017/8/23/023.
* ====================================
*/
public interface ScreenStateChangeObserver {
/**
* 加载当前屏幕状态(全屏或者非全屏)
* */
void loadingCurrentScreenState();
/**
* 更新屏幕状态(全屏或者非全屏)
* */
void notifyByScreenStateChanged();
}
2.创建自定义的BrowserApplication,以便于全局管理,记得要改清单文件哦!
/**
* ====================================
* 作 者:Ares(颜崔)
* 地 址:https://github.com/Ares-yc
* 描 述:
* 版 本:1.0.0
* 创建时间:2017/8/17/017.
* 修改时间:2017/8/17/017.
* ====================================
*/
public class BrowserApplication extends Application {
private static Context mContext;
/** 屏幕全屏切换监听栈 */
private static List<ScreenStateChangeObserver> mScreenStateChangeObserverStack;
@Override
public void onCreate() {
super.onCreate();
mContext = this.getApplicationContext();
}
public static Context getContext(){
return mContext;
}
/**
* 获得observer堆栈
* */
private static List<ScreenStateChangeObserver> obtainScreenStateChangeObserverStack() {
if (mScreenStateChangeObserverStack == null) mScreenStateChangeObserverStack = new ArrayList<>();
return mScreenStateChangeObserverStack;
}
/**
* 向堆栈中添加observer
* */
public static void registerObserver(ScreenStateChangeObserver observer) {
if (observer == null || obtainScreenStateChangeObserverStack().contains(observer)) return ;
obtainScreenStateChangeObserverStack().add(observer);
}
/**
* 从堆栈中移除observer
* */
public static void unregisterObserver(ScreenStateChangeObserver observer) {
if (observer == null || !(obtainScreenStateChangeObserverStack().contains(observer))) return ;
obtainScreenStateChangeObserverStack().remove(observer);
}
/**
* 向堆栈中所有对象发送更新屏幕切换全屏的指令
* */
public static void notifyByThemeChanged() {
List<ScreenStateChangeObserver> observers = obtainScreenStateChangeObserverStack();
for (ScreenStateChangeObserver observer : observers) {
observer.notifyByScreenStateChanged(); //通知栈内所有实现类更新全屏状态
}
boolean isFulllScreen = SPUtil.getInstance(mContext).getBoolean(Constant.IS_FULL_SCREEN,false);
SPUtil.getInstance(mContext).putBoolean(Constant.IS_FULL_SCREEN,!isFulllScreen);
}
}
3.创建基类BaseActivity,实现ScreenStateChangeObserver接口,任何想要实现全屏切换的Activity只需继承BaseActivity即可
/**
* ====================================
* 作 者:Ares(颜崔)
* 地 址:https://github.com/Ares-yc
* 描 述:Activity的抽象基类
* 版 本:a1.0.0
* 创建时间:2017/8/14/014.
* 修改时间:2017/8/14/014.
* ====================================
*/
public abstract class BaseActivity extends AppCompatActivity implements ScreenStateChangeObserver{
//可在子类中直接使用,例如:在MainActivity中可替代MainActivity.this使用
protected Context mContext;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(setLayoutId());
BrowserApplication.registerObserver(this);
loadingCurrentScreenState();
ButterKnife.bind(this);
this.mContext = this;
initView();
initData();
}
//通过继承关系,获取子类设置的布局Id
protected abstract int setLayoutId();
//初始化必要的控件
protected abstract void initView();
//初始化数据
protected abstract void initData();
//加载当前屏幕状态(全屏或者非全屏)
@Override
public void loadingCurrentScreenState() {
SetBoxUtil.loadingCurrentScreenState(this);
}
//更新屏幕状态(全屏或者非全屏)
@Override
public void notifyByScreenStateChanged() {
SetBoxUtil.notifyByScreenStateChanged(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
BrowserApplication.unregisterObserver(this);
}
}
4.附上SetBoxUtil代码
/**
* ====================================
* 作 者:Ares(颜崔)
* 地 址:https://github.com/Ares-yc
* 描 述:工具箱方法方法实现类
* 版 本:1.0.0
* 创建时间:2017/8/23/023.
* 修改时间:2017/8/23/023.
* ====================================
*/
public class SetBoxUtil {
//加载当前全屏状态
public static void loadingCurrentScreenState(Activity mContext){
boolean isFulllScreen = SPUtil.getInstance(mContext).getBoolean(Constant.IS_FULL_SCREEN,false);
if (isFulllScreen) {
WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
mContext.getWindow().setAttributes(params);
} else {
WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
mContext.getWindow().setAttributes(params);
}
}
//更新全屏状态
public static void notifyByScreenStateChanged(Activity mContext){
boolean isFulllScreen = SPUtil.getInstance(mContext).getBoolean(Constant.IS_FULL_SCREEN,false);
if (!isFulllScreen) {
WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
params.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
mContext.getWindow().setAttributes(params);
} else {
WindowManager.LayoutParams params = mContext.getWindow().getAttributes();
params.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
mContext.getWindow().setAttributes(params);
}
}
}