package com.ericssonlabs;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
public class ScrollLayout extends ViewGroup {
private static final String TAG = "ScrollLayout";
private VelocityTracker mVelocityTracker; // 用于判断甩动手势
private static final int SNAP_VELOCITY = 600;
private Scroller mScroller;
private int mCurScreen;
private int mDefaultScreen = 0;
private float mLastMotionX;
private OnViewChangeListener mOnViewChangeListener;
public ScrollLayout(Context context) {
super(context);
init(context);
}
public ScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public ScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
mCurScreen = mDefaultScreen;
mScroller = new Scroller(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
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 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
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
if (mOnViewChangeListener != null) {
mOnViewChangeListener.OnViewChange(mCurScreen);
}
}
}
@Override
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
final float x = event.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
}
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
mLastMotionX = x;
break;
case MotionEvent.ACTION_MOVE:
int deltaX = (int) (mLastMotionX - x);
if (IsCanMove(deltaX)) {
if (mVelocityTracker != null) {
mVelocityTracker.addMovement(event);
}
mLastMotionX = x;
scrollBy(deltaX, 0);
}
break;
case MotionEvent.ACTION_UP:
int velocityX = 0;
if (mVelocityTracker != null) {
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(1000);
velocityX = (int) mVelocityTracker.getXVelocity();
}
if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {
// Fling enough to move left
Log.e(TAG, "snap left");
snapToScreen(mCurScreen - 1);
} else if (velocityX < -SNAP_VELOCITY
&& mCurScreen < getChildCount() - 1) {
// Fling enough to move right
Log.e(TAG, "snap right");
snapToScreen(mCurScreen + 1);
} else {
snapToDestination();
}
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
break;
}
return true;
}
private boolean IsCanMove(int deltaX) {
if (getScrollX() <= 0 && deltaX < 0) {
return false;
}
if (getScrollX() >= (getChildCount() - 1) * getWidth() && deltaX > 0) {
return false;
}
return true;
}
public void SetOnViewChangeListener(OnViewChangeListener listener) {
mOnViewChangeListener = listener;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android首次运行滑动帮助实例(模仿微信)
共62个文件
png:18个
class:14个
xml:11个
5星 · 超过95%的资源 需积分: 9 35 下载量 199 浏览量
2013-05-05
21:16:30
上传
评论
收藏 1.01MB ZIP 举报
温馨提示
android首次运行滑动帮助实例(模仿微信) 软件启动之后,通过滑动进入主页面。主要用户宣传软件的功能等。
资源推荐
资源详情
资源评论
收起资源包目录
ScrollLayout.zip (62个子文件)
ScrollLayout
bin
classes.dex 11KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
page_indicator_unfocused.png 166B
page_indicator_focused.png 161B
whatsnew_btn_nor.9.png 2KB
ic_launcher.png 4KB
whatsnew_btn_pressed.9.png 2KB
whatsnew_left_m.png 15KB
whatsnew_right_m.png 13KB
drawable-mdpi
ic_launcher.png 2KB
classes
com
ericssonlabs
ScrollLayoutActivity$1.class 2KB
OtherActivity.class 516B
R$layout.class 417B
R$id.class 644B
R$drawable.class 923B
ScrollLayoutActivity.class 3KB
OnViewChangeListener.class 168B
R$attr.class 334B
ScrollLayout.class 5KB
R.class 607B
R$string.class 421B
R$color.class 421B
R$anim.class 506B
ScrollLayoutActivity$1$1.class 2KB
resources.ap_ 324KB
ScrollLayout.apk 332KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
page_indicator_unfocused.png 166B
page_indicator_focused.png 161B
whatsnew_left.jpg 29KB
whatsnew_btn_nor.9.png 2KB
ic_launcher.png 4KB
whatsnew_btn_pressed.9.png 2KB
w01.jpg 48KB
w02.jpg 54KB
w03.jpg 54KB
whatsnew_right.jpg 28KB
whatsnew_bg.jpg 60KB
whatsnew_left_m.png 15KB
whatsnew_right_m.png 13KB
drawable
button_bg.xml 441B
.DS_Store 6KB
page_indicator_bg.xml 318B
values
colors.xml 152B
strings.xml 184B
drawable-mdpi
ic_launcher.png 3KB
anim
translate_right.xml 331B
zoom_out_exit.xml 607B
zoom_out_enter.xml 455B
translate_left.xml 332B
layout
other.xml 549B
main.xml 6KB
assets
gen
com
ericssonlabs
R.java 2KB
proguard.cfg 1KB
src
com
ericssonlabs
ScrollLayout.java 5KB
OnViewChangeListener.java 152B
ScrollLayoutActivity.java 4KB
OtherActivity.java 288B
.project 815B
.classpath 356B
project.properties 361B
AndroidManifest.xml 778B
共 62 条
- 1
莫川
- 粉丝: 2272
- 资源: 186
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页