class PanelSwitcher extends FrameLayout {
private static final int MAJOR_MOVE = 60;
private static final int ANIM_DURATION = 400; private GestureDetector mGestureDetector;
private int mCurrentView;
private View mChild, mHistoryView;
private View children[]; private int mWidth;
private TranslateAnimation inLeft;
private TranslateAnimation outLeft; private TranslateAnimation inRight;
private TranslateAnimation outRight; private static final int NONE = 1;
private static final int LEFT = 2;
private static final int RIGHT = 3;
private int mPreviousMove; public PanelSwitcher(Context context, AttributeSet attrs) {
super(context, attrs);
mCurrentView = 0;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
int dx = (int) (e2.getX() - e1.getX()); // don't accept the fling if it's too short
// as it may conflict with a button push
if (Math.abs(dx) > MAJOR_MOVE && Math.abs(velocityX) > Math.abs(velocityY)) {
if (velocityX > 0) {
moveRight();
} else {
moveLeft();
}
return true;
} else {
return false;
}
}
});
} @Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
mWidth = w;
inLeft = new TranslateAnimation(mWidth, 0, 0, 0);
outLeft = new TranslateAnimation(0, -mWidth, 0, 0);
inRight = new TranslateAnimation(-mWidth, 0, 0, 0);
outRight = new TranslateAnimation(0, mWidth, 0, 0); inLeft.setDuration(ANIM_DURATION);
outLeft.setDuration(ANIM_DURATION);
inRight.setDuration(ANIM_DURATION);
outRight.setDuration(ANIM_DURATION);
} protected void onFinishInflate() {
int count = getChildCount();
children = new View[count];
for (int i = 0; i < count; ++i) {
children[i] = getChildAt(i);
if (i != mCurrentView) {
children[i].setVisibility(View.GONE);
}
}
} @Override
public boolean onTouchEvent(MotionEvent event) {
mGestureDetector.onTouchEvent(event);
return true;
} @Override
public boolean onInterceptTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
} void moveLeft() {
// <--
if (mCurrentView < children.length - 1 && mPreviousMove != LEFT) {
children[mCurrentView+1].setVisibility(View.VISIBLE);
children[mCurrentView+1].startAnimation(inLeft);
children[mCurrentView].startAnimation(outLeft);
children[mCurrentView].setVisibility(View.GONE); mCurrentView++;
mPreviousMove = LEFT;
}
} void moveRight() {
// -->
if (mCurrentView > 0 && mPreviousMove != RIGHT) {
children[mCurrentView-1].setVisibility(View.VISIBLE);
children[mCurrentView-1].startAnimation(inRight);
children[mCurrentView].startAnimation(outRight);
children[mCurrentView].setVisibility(View.GONE); mCurrentView--;
mPreviousMove = RIGHT;
}
} int getCurrentIndex() {
return mCurrentView;
}
}
屏幕一张张翻
原创
©著作权归作者所有:来自51CTO博客作者mb649166f4c151e的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
轻松复现一张AI图片
现在有一个非常漂亮的AI图片,你是不是想知道他是怎么生成的?今天我会交给大家三种方法,学会了,什么图都可以手到擒来了。
AIGC stable diffusion 程序那些事 -
一张图详解开源监控夜莺(Nightingale)的架构
使用一张非常详尽的架构图,解释开源夜莺监控项目的原理和数据流
开源监控 夜莺监控 开源夜莺 Nightingale Prometheus -
打印机不断打印出一张张空白纸——好像跟技术无关
一次打印机不断打印白纸的处理过程
打印机不断打印白纸 -
Python--随机漫步,生成了一张张超级好看的图
N个点自动生成的图片确定不来看看? ...
python 初始化 颜色渐变 坐标轴