package com.rsft.demo.ui360;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
public class FlatScrollViewGroup extends ViewGroup {
public float mTouchX;
public float mLastMotionX;
public int mActivePointerId = -1;
public Scroller mScroller;
private boolean mIsScorlling;
private ViewIndexChangeLinstener mViewIndexChangeLinstener;
private int mCurrentViewIndex = 0;
public FlatScrollViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
mScroller = new Scroller(context);
ComponentContainer cc1 = new ComponentContainer(context);
addView(cc1, 0);
cc1.init(1);
ComponentContainer cc2 = new ComponentContainer(context);
addView(cc2, 1);
cc2.init(2);
ComponentContainer cc3 = new ComponentContainer(context);
addView(cc3, 2);
cc3.init(3);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 0;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
final int childWidth = child.getMeasuredWidth();
child.layout(childLeft, 0, childLeft + childWidth,
child.getMeasuredHeight());
childLeft += childWidth;
}
}
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
mTouchX = mScroller.getCurrX();
int scrollY = mScroller.getCurrY();
scrollTo((int) mTouchX, scrollY);
postInvalidate();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"Workspace can only be used in EXACTLY mode.");
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException(
"Workspace can only be used in EXACTLY mode.");
}
final int count = getChildCount();
for (int i = 0; i < count; i++) {
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void dispatchDraw(Canvas canvas) {
final long drawingTime = getDrawingTime();
final int count = getChildCount();
for (int i = 0; i < count; i++) {
drawChild(canvas, getChildAt(i), drawingTime);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
mIsScorlling = false;
final int action = ev.getAction() & MotionEvent.ACTION_MASK;
if (action == MotionEvent.ACTION_CANCEL
|| action == MotionEvent.ACTION_UP) {
mIsScorlling = false;
return false;
}
switch (action) {
case MotionEvent.ACTION_DOWN:
mActivePointerId = ev.getPointerId(0);
mLastMotionX = ev.getX();
mIsScorlling = false;
break;
case MotionEvent.ACTION_MOVE:
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
try {
final float x = ev.getX(pointerIndex);
final float deltaX = mLastMotionX - x;
mLastMotionX = x;
final float absDeltaX = Math.abs(deltaX);
if (absDeltaX > 6) {
mIsScorlling = true;
}
} catch (ArrayIndexOutOfBoundsException e) {
}
break;
}
return mIsScorlling;
}
float downX = -1;
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mLastMotionX = ev.getX();
mActivePointerId = ev.getPointerId(0);
downX = mLastMotionX;
break;
case MotionEvent.ACTION_MOVE:
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
try {
final float x = ev.getX(pointerIndex);
final float deltaX = mLastMotionX - x;
mLastMotionX = x;
if (deltaX > 0) {
if (mCurrentViewIndex >= getChildCount() - 1) {
return true;
}
scrollBy((int) deltaX, 0);
} else if (deltaX < 0) {
if (mCurrentViewIndex <= 0) {
return true;
}
scrollBy((int) deltaX, 0);
} else {
awakenScrollBars();
}
} catch (ArrayIndexOutOfBoundsException e) {
}
break;
case MotionEvent.ACTION_UP:
int screenWidth = getWidth();
int whichView = (getScrollX() + (screenWidth / 2)) / screenWidth;
whichView = Math.max(0, Math.min(whichView, getChildCount() - 1));
int newX = whichView * getWidth();
int delta = newX - getScrollX();
mScroller.startScroll(getScrollX(), 0, delta, 0,
Math.abs(delta) * 2);
invalidate();
mCurrentViewIndex = whichView;
if (mViewIndexChangeLinstener != null) {
mViewIndexChangeLinstener.onIndexChange(whichView);
}
break;
case MotionEvent.ACTION_CANCEL:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
public interface ViewIndexChangeLinstener {
public void onIndexChange(int whichView);
}
public void setViewIndexChangeLinstener(ViewIndexChangeLinstener linstener) {
this.mViewIndexChangeLinstener = linstener;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
UI360Simulate.rar (48个子文件)
UI360Simulate
.project 849B
project.properties 361B
proguard.cfg 1KB
src
com
rsft
demo
ui360
FlatScrollViewGroup.java 6KB
UI360SimulateActivity.java 1KB
ComponentContainer.java 2KB
AndroidManifest.xml 744B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
type_component_bg_normal.png 3KB
background.png 380KB
rsft.png 30KB
type_component_bg_pressed.png 3KB
point_2.png 4KB
point_1.png 4KB
drawable
index_indicator_selector.xml 294B
type_component_bg.xml 417B
drawable-mdpi
ic_launcher.png 3KB
layout
component_container.xml 4KB
main.xml 3KB
values
strings.xml 192B
dimens.xml 763B
assets
gen
com
rsft
demo
ui360
R.java 3KB
.classpath 364B
bin
resources.ap_ 372KB
UI360Simulate.apk 379KB
classes
com
rsft
demo
ui360
R$layout.class 440B
R$dimen.class 905B
R$drawable.class 720B
R$attr.class 343B
FlatScrollViewGroup$ViewIndexChangeLinstener.class 298B
UI360SimulateActivity$1.class 1KB
UI360SimulateActivity.class 2KB
R$id.class 931B
ComponentContainer.class 2KB
FlatScrollViewGroup.class 5KB
R.class 587B
R$string.class 430B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
type_component_bg_normal.png 114B
background.png 331KB
rsft.png 22KB
type_component_bg_pressed.png 114B
point_2.png 1KB
point_1.png 1KB
drawable-mdpi
ic_launcher.png 2KB
classes.dex 10KB
共 48 条
- 1
资源评论
- xueguopeng2013-03-07非常值得学习!
- 卡卡宇科2013-01-31不错,很好的学习demo。
- guanyajuanyu2014-09-11是非常好的demo,但是不符合我的需求
- cs043182013-11-22很好的学习demo。
三秋桂子仪同三司
- 粉丝: 6
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功