編輯:關於Android編程
復制代碼 代碼如下:
//觀察者,需要用到觀察者模式的類需實現此接口
public interface Observer {
void update(Object... objs);
}
//被觀察者(一個抽象類,方便擴展)
public abstract class Observable {
public final ArrayList<Class<?>> obserList = new ArrayList<Class<?>>();
/** Attach Observer (通過實例注冊觀察者)
* <b>Notice:</b> ob can't be null ,or it will throw NullPointerException
* */
public <T> void registerObserver(T ob) {
if (ob == null) throw new NullPointerException();
this.registerObserver(ob.getClass());
}
/**
* Attach Observer(通過Class注冊觀察者)
* @param cls
*/
public void registerObserver(Class<?> cls) {
if (cls == null) throw new NullPointerException();
synchronized(obserList) {
if (!obserList.contains(cls)) {
obserList.add(cls);
}
}
}
/** Unattach Observer (注銷觀察者)
* <b>Notice:</b>
* <b>It reverses with attachObserver() method</b>
* */
public <T> void unRegisterObserver(T ob) {
if (ob == null) throw new NullPointerException();
this.unRegisterObserver(ob.getClass());
}
/** Unattach Observer(注銷觀察者,有時候在未獲取到實例使用)
* <b>Notice:</b>
* <b>It reverses with attachObserver() method</b>
* */
public void unRegisterObserver(Class<?> cls) {
if(cls == null) throw new NullPointerException();
synchronized(obserList){
Iterator<Class<?>> iterator = obserList.iterator();
while(iterator.hasNext()) {
if(iterator.next().getName().equals(cls.getName())){
iterator.remove();
break;
}
}
}
}
/** detach all observers */
public void unRegisterAll() {
synchronized(obserList) {
obserList.clear();
}
}
/** Ruturn the size of observers */
public int countObservers() {
synchronized(obserList) {
return obserList.size();
}
}
/**
* notify all observer (通知所有觀察者,在子類中實現)
* @param objs
*/
public abstract void notifyObservers(Object... objs);
/**
* notify one certain observer (通知某一個確定的觀察者)
* @param cls
* @param objs
*/
public abstract void notifyObserver(Class<?> cls, Object... objs);
/**
* notify one certain observer
* @param cls
* @param objs
*/
public abstract <T> void notifyObserver(T t, Object... objs);
}
//目標被觀察者
public class ConcreteObservable extends Observable {
private static ConcreteObservable instance = null;
private ConcreteObservable() {}
public static synchronized ConcreteObservable getInstance() {
if (instance == null) {
instance = new ConcreteObservable();
}
return instance;
}
@Override
public <T> void notifyObserver(T t, Object... objs) {
// TODO Auto-generated method stub
if (t == null) throw new NullPointerException();
this.notifyObserver(t.getClass(), objs);
}
@Override
public void notifyObservers(Object... objs) {
// TODO Auto-generated method stub
for (Class<?> cls : obserList) {
this.notifyObserver(cls, objs);
}
}
@Override //通過java反射機制實現調用
public void notifyObserver(Class<?> cls, Object... objs) {
// TODO Auto-generated method stub
if (cls == null) throw new NullPointerException();
Method[] methods = cls.getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals("update")) {
try {
method.invoke(cls, objs);
break;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//使用 (實現Observer接口)
public class Text extends Activity implements Observer {
public void onCreate(...) {
ConcreteObservable.getInstance().registerObserver(Text.class);
....
}
//實現接口處理
public void update(Object... objs) {
// 做操作,比如更新數據,更新UI等
}
}
GLSurfaceView提供了下列特性:1> 管理一個surface,這個surface就是一塊特殊的內存,能直接排版到android的視圖view上。2>
先給大家展示下效果圖:不知道大家對效果圖感覺怎麼樣,個人覺還不錯,感興趣的朋友可以參考下實現代碼哦。public class ToggleButton extends V
上一篇介紹了Animation動畫其一:Tween補間動畫。 這篇文章接下來介紹Animation另一種動畫形式:Frame逐幀動畫。 Frame動畫是一系列圖片按照一定
1、開發Android硬件抽象層代碼 ~/android-2.3_r1/hardware/libhardware ----include