package com.chen.weixin.test;
import android.R.bool;
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.ViewConfiguration;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.widget.Scroller;
public class MyScrollLayout 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 int mTouchSlop;
// private static final int TOUCH_STATE_REST = 0;
// private static final int TOUCH_STATE_SCROLLING = 1;
// private int mTouchState = TOUCH_STATE_REST;
private OnViewChangeListener mOnViewChangeListener;
public MyScrollLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public MyScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}
public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init(context);
}
private void init(Context context)
{
mCurScreen = mDefaultScreen;
// mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
mScroller = new Scroller(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int widthMode = MeasureSpec.getMode(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() {
// TODO Auto-generated method stub
if (mScroller.computeScrollOffset()) {
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
postInvalidate();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
final float x = event.getX();
final float y = event.getY();
switch (action) {
case MotionEvent.ACTION_DOWN:
Log.i("", "onTouchEvent 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;
}
// mTouchState = TOUCH_STATE_REST;
break;
}
return true;
}
//
// public boolean onInterceptTouchEvent(MotionEvent ev) {
// // TODO Auto-generated method stub
// final int action = ev.getAction();
// if ((action == MotionEvent.ACTION_MOVE)
// && (mTouchState != TOUCH_STATE_REST)) {
// Log.i("", "onInterceptTouchEvent return true");
// return true;
// }
// final float x = ev.getX();
// final float y = ev.getY();
// 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;
//
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android 高仿微信导航页开门效果源码-IT计算机-毕业设计.zip (70个子文件)
Android 高仿微信导航页开门效果源码
Android 高仿微信导航页开门效果源码
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
TestWeiXinWhatsNew.apk 606KB
classes
com
chen
weixin
test
OtherActivity.class 524B
R$layout.class 429B
TestWeiXinWhatsNewActivity.class 3KB
R$id.class 656B
R$drawable.class 1KB
OnViewChangeListener.class 172B
R$attr.class 346B
TestWeiXinWhatsNewActivity$1$1.class 2KB
R.class 643B
TestWeiXinWhatsNewActivity$1.class 2KB
MyScrollLayout.class 5KB
R$string.class 433B
R$color.class 433B
R$anim.class 518B
resources.ap_ 597KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
page_indicator_unfocused.png 166B
page_indicator_focused.png 161B
w05.jpg 54KB
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
w04.jpg 64KB
w06.jpg 54KB
w03.jpg 53KB
w08.jpg 53KB
whatsnew_right.jpg 28KB
whatsnew_bg.jpg 60KB
whatsnew_left_m.png 15KB
whatsnew_right_m.png 13KB
w07.jpg 48KB
drawable
button_bg.xml 441B
page_indicator_bg.xml 318B
values
colors.xml 152B
strings.xml 202B
drawable-mdpi
ic_launcher.png 3KB
anim
translate_right.xml 335B
zoom_out_exit.xml 607B
zoom_out_enter.xml 459B
translate_left.xml 284B
layout
other.xml 464B
main.xml 7KB
assets
gen
com
chen
weixin
test
R.java 3KB
proguard.cfg 1KB
.settings
org.eclipse.core.resources.prefs 88B
org.eclipse.jdt.core.prefs 629B
src
com
chen
weixin
test
MyScrollLayout.java 9KB
TestWeiXinWhatsNewActivity.java 4KB
OnViewChangeListener.java 115B
OtherActivity.java 292B
.project 854B
.classpath 396B
project.properties 360B
AndroidManifest.xml 810B
1_120820213436_2.png 120KB
1_120820213436_1.png 122KB
共 70 条
- 1
资源评论
芝麻粒儿
- 粉丝: 6w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功