package com.xu81.testflip;
import java.util.Vector;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;
public class ScrollLayout extends ViewGroup {
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private int mCurScreen;
private int mDefaultScreen = 0;
private static final int TOUCH_STATE_REST = 0;
private static final int TOUCH_STATE_SCROLLING = 1;
private static final int SNAP_VELOCITY = 500;
private int mTouchState = TOUCH_STATE_REST;
private int mTouchSlop;
private float mLastMotionX;
private int sensitivity = 30;
private boolean spring;
private Vector<LayoutChangeListener> listeners;
public ScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
mScroller = new Scroller(context);
mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
listeners = new Vector<LayoutChangeListener>();
}
public void addChangeListener(LayoutChangeListener listener) {
listeners.add(listener);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int childLeft = 0;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View childView = getChildAt(i);
if (childView.getVisibility() != View.GONE) {
final int childWidth = childView.getMeasuredWidth();
childView.layout(childLeft, 0, childLeft + childWidth,
childView.getMeasuredHeight());
childLeft += childWidth;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only canmCurScreen run at EXACTLY mode!");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"ScrollLayout only can run at EXACTLY mode!");
}
// The children are given the same width and height as the scrollLayout
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
scrollTo(mCurScreen * width, 0);
}
public void snapToDestination() {
final int screenWidth = getWidth();
final int destScreen = (getScrollX() + screenWidth / 2) / screenWidth;
snapToScreen(destScreen);
}
public void snapToScreen(int whichScreen) {
// get the valid layout page
int lastIndex = mCurScreen;
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
if (getScrollX() != (whichScreen * getWidth())) {
final int delta = whichScreen * getWidth() - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0,
Math.abs(delta) * 2);
mCurScreen = whichScreen;
invalidate(); // Redraw the layout
}
for (LayoutChangeListener listener : listeners)
listener.doChange(lastIndex, whichScreen);
}
public void setToScreen(int whichScreen) {
whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
mCurScreen = whichScreen;
scrollTo(whichScreen * getWidth(), 0);
}
public int getCurScreen() {
return mCurScreen;
}
@Override
public void computeScroll() {
// TODO Auto-generated method stub
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
public boolean isSpring() {
return spring;
}
public void setSpring(boolean spring) {
this.spring = spring;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
if (mVelocityTracker == null)
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
final int action = event.getAction();
final float x = event.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished())
mScroller.abortAnimation();
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) (mLastMotionX - x);
if (Math.abs(deltaX) > sensitivity) {
// 左滑动为正数、右为负数
if (spring) {
scrollBy(deltaX, 0);
mLastMotionX = x;
} else {
final int childCount = getChildCount();
boolean max = mCurScreen < childCount - 1;
boolean min = mCurScreen > 0;
boolean canMove = deltaX > 0 ? (max ? true : false)
: (min ? true : false);
if (canMove) {
scrollBy(deltaX, 0);
mLastMotionX = x;
}
}
}
break;
case MotionEvent.ACTION_UP:
final VelocityTracker velocityTracker = mVelocityTracker;
velocityTracker.computeCurrentVelocity(1000);
int velocityX = (int) velocityTracker.getXVelocity();
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
mTouchState = TOUCH_STATE_REST;
break;
case MotionEvent.ACTION_CANCEL:
mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE)
&& (mTouchState != TOUCH_STATE_REST))
return true;
final float x = ev.getX();
switch (action) {
case MotionEvent.ACTION_MOVE:
final int xDiff = (int) Math.abs(mLastMotionX - x);
if (xDiff > mTouchSlop)
mTouchState = TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_DOWN:
mLastMotionX = x;
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
: TOUCH_STATE_SCROLLING;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
mTouchState = TOUCH_STATE_REST;
break;
}
return mTouchState != TOUCH_STATE_REST;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
应用源码之FlipDemo(高仿QQ左右滑动菜单).zip (140个子文件)
resources.ap_ 624KB
FlipDemo.apk 636KB
TestFlip.apk 636KB
jarlist.cache 119B
ScrollLayout.class 6KB
TuliaoMainActivity.class 3KB
R$drawable.class 3KB
ScrollContent.class 2KB
R$color.class 635B
R.class 616B
R$id.class 536B
R$string.class 424B
R$layout.class 391B
R$anim.class 389B
BuildConfig.class 343B
R$attr.class 337B
LayoutChangeListener.class 166B
.classpath 380B
classes.dex 14KB
ScrollLayout.java 6KB
R.java 5KB
TuliaoMainActivity.java 3KB
ScrollContent.java 1KB
BuildConfig.java 159B
LayoutChangeListener.java 131B
largehead.png 486KB
largehead.png 486KB
mainbodytop.png 31KB
mainbodytop.png 31KB
faceback_head.png 9KB
faceback_head.png 9KB
group_head.png 7KB
group_head.png 7KB
h001.png 6KB
icon.png 6KB
h001.png 6KB
icon.png 6KB
show.png 5KB
show.png 5KB
chat_emotion_over.png 3KB
chat_emotion_over.png 3KB
chat_emotion.png 3KB
chat_emotion.png 3KB
faceback.png 3KB
faceback.png 3KB
option_selected.png 3KB
option_selected.png 3KB
bottombar_qzone_selected.png 3KB
bottombar_qzone_selected.png 3KB
bottombar_qzone_unselected.png 3KB
bottombar_qzone_unselected.png 3KB
icon_offline.png 2KB
icon_offline.png 2KB
icon_away.png 2KB
icon_away.png 2KB
icon_invisiable.png 2KB
icon_invisiable.png 2KB
hide.png 2KB
hide.png 2KB
icon_online.png 2KB
icon_online.png 2KB
tab3_over.png 2KB
tab3_over.png 2KB
tab2_normal.png 2KB
tab2_normal.png 2KB
chat_paopao.png 2KB
chat_paopao.png 2KB
tab1_over.png 1KB
tab1_over.png 1KB
chat_quit_nor.png 1KB
chat_quit_nor.png 1KB
tab2_over.png 1KB
tab2_over.png 1KB
tab3_normal.png 1KB
tab3_normal.png 1KB
option_normal.png 1KB
option_normal.png 1KB
topface_back.png 1KB
topface_back.png 1KB
tab1_normal.png 1KB
tab1_normal.png 1KB
shurukuang.9.png 841B
btn_check_on.png 818B
btn_check_on.png 818B
qq_btn_normal.9.png 786B
shurukuang.9.png 758B
qq_btn_pressed.9.png 729B
qq_btn_normal.9.png 663B
qq_btn_pressed.9.png 648B
input_over.9.png 592B
input.9.png 576B
faceback_manager.png 475B
faceback_manager.png 475B
input.9.png 453B
photobg.9.png 445B
input_over.9.png 421B
expand.png 410B
expand.png 410B
narrow_select.9.png 390B
narrow.9.png 386B
共 140 条
- 1
- 2
资源评论
Soft_Leader
- 粉丝: 720
- 资源: 1629
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功