Gallery浏览图片除开编辑界面,主要有三个界面:即起始页,相册簿集合页面(AlbumSetPage);点击某个相册进入该相册的所有图片页面(AlbumPage)和再点击某张具体图片之后的图片浏览页面(PhotoPage)。
我们从起始页面开始,点击某个相册:
一个相册是一个SlotView对象,在onCreate()中初始化了SlotView。并且对SlotView进行了监听:
mSlotView.setListener(new SlotView.SimpleListener() {
@Override
public void onDown(int index) {
AlbumSetPage.this.onDown(index);
}
@Override
public void onUp(boolean followedByLongPress) {
AlbumSetPage.this.onUp(followedByLongPress);
}
@Override
public void onSingleTapUp(int slotIndex) {
AlbumSetPage.this.onSingleTapUp(slotIndex);
}
@Override
public void onLongTap(int slotIndex) {
AlbumSetPage.this.onLongTap(slotIndex);
}
});
public void onSingleTapUp(int slotIndex) {
if (!mIsActive) return;
...
mHandler.sendMessage(mHandler.obtainMessage(MSG_PICK_ALBUM, slotIndex, 0));
}
}
mHandler = new SynchronizedHandler(mActivity.getGLRoot()) {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case MSG_PICK_ALBUM: {
pickAlbum(message.arg1);
break;
}
default: throw new AssertionError(message.what);
}
}
};
private void pickAlbum(int slotIndex) {
if (!mIsActive) return;
...
mActivity.getStateManager().startStateForResult(
AlbumPage.class, REQUEST_DO_ANIMATION, data);
}
}
接收到动作消息,启动相册页面AlbumPage对象。另外一个界面的跳转与此相同。我关注的点不在这里,这里我的疑问是SlotView.SimpleListener是我们自定义的监听器,这个监听器是如何实现手势监听的?原来Android系统提供了一套手势监听的接口GestureDetector.OnGestureListener,它们由底层驱动实现,是标准的手势接口,我们调用它实现自己的手势监听。
public interface Listener {
public void onDown(int index);
public void onUp(boolean followedByLongPress);
public void onSingleTapUp(int index);
public void onLongTap(int index);
public void onScrollPositionChanged(int position, int total);
}
public static class SimpleListener implements Listener {
@Override public void onDown(int index) {}
@Override public void onUp(boolean followedByLongPress) {}
@Override public void onSingleTapUp(int index) {}
@Override public void onLongTap(int index) {}
@Override public void onScrollPositionChanged(int position, int total) {}
}
private class MyGestureListener implements GestureDetector.OnGestureListener {
...
@Override
public boolean onSingleTapUp(MotionEvent e) {
cancelDown(false);
if (mDownInScrolling) return true;
int index = mLayout.getSlotIndexByPosition(e.getX(), e.getY());
if (index != INDEX_NONE) mListener.onSingleTapUp(index);
return true;
}