一个焦点抢占策略的代码示例
包含以下功能:
requestMapFocus;
abandonMapFocus;
registerMapFocusChangeListener;
onMapFocusChanged(int previousMap, int focusMap);
主体代码:
package com.test;
import java.util.LinkedList;
public class MapFocusManager {
private OnMapFocusChangeListener mMapFocusChangeListener;
private LinkedList<Integer> mFocusedMapList = new LinkedList<>();
public synchronized boolean requestMapFocus(int mapType) {
if (mapType < 0) {
// Log.w("TAG", "illegal mapType:" + mapType);
return false;
}
int currentFocusMap = -1;
int index = mFocusedMapList.indexOf(mapType);
if (index == 0) {
// already at first, do nothing.
return true;
} else if (index > 0) {
// already exit mFocusedMapList, but not at first.
currentFocusMap = mFocusedMapList.get(0);
mFocusedMapList.remove(index);
mFocusedMapList.addFirst(mapType);
mMapFocusChangeListener.onMapFocusChanged(currentFocusMap, mapType);
} else {
// not exist in mFocusedMapList;
if (0 == mFocusedMapList.size()) {
currentFocusMap = -1;
} else {
currentFocusMap = mFocusedMapList.get(0);
}
mFocusedMapList.addFirst(mapType);
mMapFocusChangeListener.onMapFocusChanged(currentFocusMap, mapType);
}
return true;
}
public synchronized boolean abandonMapFocus(int mapType) {
int index = mFocusedMapList.indexOf(mapType);
if (index == 0) {
// at first, remove it!
mFocusedMapList.removeFirst();
int newFocusMap = -1;
if (0 == mFocusedMapList.size()) {
newFocusMap = -1;
} else {
newFocusMap = mFocusedMapList.getFirst();
}
mMapFocusChangeListener.onMapFocusChanged(mapType, newFocusMap);
return true;
} else if (index > 0) {
// already exist in mFocusedMapList, but not first, just remove it, focus not changed.
mFocusedMapList.remove(index);
} else {
// not exist in mFocusedMapList;
return false;
}
return true;
}
public synchronized void registerMapFocusChangeListener(OnMapFocusChangeListener listener) {
mMapFocusChangeListener = listener;
}
}
监听器代码:
package com.test;
public interface OnMapFocusChangeListener {
void onMapFocusChanged(int previousMap, int focusMap);
}
测试程序和输出
@Test
public void test_demo() {
MapFocusManager mapFocusManager = new MapFocusManager();
OnMapFocusChangeListener listener = new OnMapFocusChangeListener() {
@Override
public void onMapFocusChanged(int previousMap, int focusMap) {
System.out.println("onMapFocusChanged, previousMap:" + previousMap
+ ", focusMap:" + focusMap);
}
};
mapFocusManager.registerMapFocusChangeListener(listener);
System.out.println("start abandon 123");
mapFocusManager.abandonMapFocus(1);
mapFocusManager.abandonMapFocus(2);
mapFocusManager.abandonMapFocus(3);
System.out.println("start request 1");
mapFocusManager.requestMapFocus(1);
System.out.println("start abandon 1");
mapFocusManager.abandonMapFocus(1);
System.out.println("start request 123");
mapFocusManager.requestMapFocus(1);
mapFocusManager.requestMapFocus(2);
mapFocusManager.requestMapFocus(3);
System.out.println("start abandon 2");
mapFocusManager.abandonMapFocus(2);
System.out.println("start abandon 3");
mapFocusManager.abandonMapFocus(3);
System.out.println("start abandon 1");
mapFocusManager.abandonMapFocus(1);
System.out.println("start abandon 1");
mapFocusManager.abandonMapFocus(1);
System.out.println("start abandon 1");
mapFocusManager.abandonMapFocus(1);
}
start abandon 123
start request 1
onMapFocusChanged, previousMap:-1, focusMap:1
start abandon 1
onMapFocusChanged, previousMap:1, focusMap:-1
start request 123
onMapFocusChanged, previousMap:-1, focusMap:1
onMapFocusChanged, previousMap:1, focusMap:2
onMapFocusChanged, previousMap:2, focusMap:3
start abandon 2
start abandon 3
onMapFocusChanged, previousMap:3, focusMap:1
start abandon 1
onMapFocusChanged, previousMap:1, focusMap:-1
start abandon 1
start abandon 1
Process finished with exit code 0
经测试,功能符合期望;
有以下问题,需要完善:
1,listener=null状态未判断;
2,mFocusedMapList越界情况未考虑;