android之luncher滑动效果
来源:互联网 发布:点击广告 软件 编辑:程序博客网 时间:2024/06/06 21:38
Java代码:
- import
android.content.Context; - import
android.util.Log; - import
android.content.res.TypedArray; - import
android.util.AttributeSet; - import
android.view.MotionEvent; - import
android.view.VelocityTracker; - import
android.view.View; - import
android.view.ViewGroup; - import
android.view.ViewConfiguration; - import
android.widget.Scroller; -
- public
class DragableSpace extendsViewGroup { -
Scroller mScroller; -
VelocityTracker mVelocityTracker; -
-
int mScrollX 0;= -
int mCurrentScreen 0;= -
-
float mLastMotionX; -
-
static final String "DragableSpace";LOG_TAG = -
-
static final int SNAP_VELOCITY 1000;= -
-
final static int TOUCH_STATE_REST 0;= -
final static int TOUCH_STATE_SCROLLING 1;= -
-
int mTouchState = TOUCH_STATE_REST; -
-
int mTouchSlop 0;= -
-
DragableSpace(Context context) { -
-
mScroller = Scroller(context); -
-
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); -
-
ViewGroup.LayoutParams( -
ViewGroup.LayoutParams.WRAP_CONTENT, -
ViewGroup.LayoutParams.FILL_PARENT)); -
} -
-
DragableSpace(Context context, AttributeSet attrs) { -
attrs); -
mScroller = Scroller(context); -
-
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); -
-
ViewGroup.LayoutParams( -
ViewGroup.LayoutParams.WRAP_CONTENT , -
ViewGroup.LayoutParams.FILL_PARENT)); -
-
TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.DragableSpace); -
mCurrentScreen = a.getInteger(R.styleable.DragableSpace_default_screen, -
} -
-
-
boolean onInterceptTouchEvent(MotionEvent ev) { -
-
-
-
int action = ev.getAction(); -
((action == MotionEvent.ACTION_MOVE) -
&& (mTouchState != TOUCH_STATE_REST)) { -
true; -
} -
-
float x = ev.getX(); -
-
(action) { -
MotionEvent.ACTION_MOVE: -
-
-
-
int xDiff int)= ( Math.abs(x - mLastMotionX); -
-
xMoved = xDiff > mTouchSlop; -
-
(xMoved) { -
Scroll if the user moved far enough along the X axis -
mTouchState = TOUCH_STATE_SCROLLING; -
} -
-
-
MotionEvent.ACTION_DOWN: -
Remember location of down touch -
mLastMotionX = x; -
-
-
mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; -
-
-
MotionEvent.ACTION_CANCEL: -
MotionEvent.ACTION_UP: -
Release the drag -
mTouchState = TOUCH_STATE_REST; -
-
} -
-
-
mTouchState != TOUCH_STATE_REST; -
} -
-
-
boolean onTouchEvent(MotionEvent event) { -
-
(mVelocityTracker null)== { -
mVelocityTracker = VelocityTracker.obtain(); -
} -
mVelocityTracker.addMovement(event); -
-
int action = event.getAction(); -
float x = event.getX(); -
-
(action) { -
MotionEvent.ACTION_DOWN: -
Log.i(LOG_TAG, : );down" -
-
(!mScroller.isFinished()) { -
mScroller.abortAnimation(); -
} -
-
Remember where the motion event started -
mLastMotionX = x; -
-
MotionEvent.ACTION_MOVE: -
Log.i(LOG_TAG,"event : move"); -
if (mTouchState == TOUCH_STATE_SCROLLING) { -
Scroll to follow the motion event -
int deltaX int)= ( (mLastMotionX - x); -
mLastMotionX = x; -
-
"event : move, deltaX " + deltaX + ", mScrollX " + mScrollX); -
-
(deltaX 0)< { -
(mScrollX 0)> { -
scrollBy(Math.max(-mScrollX, deltaX), -
} -
} if (deltaX 0)> { -
int availableToScroll 1)= getChildAt(getChildCount() - -
.getRight() -
- mScrollX - getWidth(); -
(availableToScroll 0)> { -
scrollBy(Math.min(availableToScroll, deltaX), -
} -
} -
} -
-
MotionEvent.ACTION_UP: -
Log.i(LOG_TAG, : );up" -
if (mTouchState == TOUCH_STATE_SCROLLING) { -
VelocityTracker velocityTracker = mVelocityTracker; -
velocityTracker.computeCurrentVelocity( -
velocityX int)= ( velocityTracker.getXVelocity(); -
-
(velocityX 0)> SNAP_VELOCITY && mCurrentScreen > { -
Fling hard enough to move left -
snapToScreen(mCurrentScreen - -
} if (velocityX < -SNAP_VELOCITY -
&& mCurrentScreen < getChildCount() - { -
Fling hard enough to move right -
snapToScreen(mCurrentScreen + -
} { -
snapToDestination(); -
} -
-
(mVelocityTracker null)!= { -
mVelocityTracker.recycle(); -
mVelocityTracker = -
} -
} -
mTouchState = TOUCH_STATE_REST; -
-
MotionEvent.ACTION_CANCEL: -
Log.i(LOG_TAG, : );cancel" -
mTouchState = TOUCH_STATE_REST; -
} -
mScrollX = -
-
true; -
} -
-
void snapToDestination() { -
int screenWidth = getWidth(); -
int whichScreen 2))= (mScrollX + (screenWidth / / screenWidth; -
Log.i(LOG_TAG, des" ); -
snapToScreen(whichScreen); -
} -
-
void snapToScreen( intwhichScreen) { -
Log.i(LOG_TAG, To Screen " + whichScreen); -
mCurrentScreen = whichScreen; -
int newX = whichScreen * getWidth(); -
int delta = newX - mScrollX; -
mScroller.startScroll(mScrollX, delta, 0,Math.abs(delta) 2);* -
invalidate(); -
} -
-
void setToScreen( intwhichScreen) { -
Log.i(LOG_TAG, To Screen " + whichScreen); -
mCurrentScreen = whichScreen; -
int newX = whichScreen * getWidth(); -
mScroller.startScroll(newX, 0, 0, 10); -
invalidate(); -
} -
-
-
void onLayout( booleanchanged, intl, intt, intr, intb) { -
childLeft 0;= -
-
int count = getChildCount(); -
( inti 0;= i < count; i++) { -
View child = getChildAt(i); -
(child.getVisibility() != View.GONE) { -
int childWidth = child.getMeasuredWidth(); -
child.layout(childLeft, childLeft + childWidth, child -
.getMeasuredHeight()); -
childLeft += childWidth; -
} -
} -
-
} -
-
-
void onMeasure( intwidthMeasureSpec, intheightMeasureSpec) { -
heightMeasureSpec); -
-
int width = MeasureSpec.getSize(widthMeasureSpec); -
int widthMode = MeasureSpec.getMode(widthMeasureSpec); -
(widthMode != MeasureSpec.EXACTLY) { -
new IllegalStateException( "errormode." ); -
} -
-
int heightMode = MeasureSpec.getMode(heightMeasureSpec); -
(heightMode != MeasureSpec.EXACTLY) { -
new IllegalStateException( "errormode." ); -
} -
-
The children are given the same width and height as the workspace -
int count = getChildCount(); -
( inti 0;= i < count; i++) { -
getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); -
} -
Log.i(LOG_TAG, to +mCurrentScreen);screen " -
scrollTo(mCurrentScreen * width, -
} -
-
-
void computeScroll() { -
(mScroller.computeScrollOffset()) { -
mScrollX = mScroller.getCurrX(); -
scrollTo(mScrollX, -
postInvalidate(); -
} -
} - }
- Java代码:
<?xml version= "1.0" encoding= "utf-8"?>
<com.matthieu.launcher.DragableSpacexmlns:app= "http://schemas.android.com/apk/res/com.matthieu.launcher"
xmlns:android= "http://schemas.android.com/apk/res/android"
android:id="@+id/space"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:default_screen="1"
>
<includeandroid:id= "@+id/left" layout= "@layout/left_screen" />
<includeandroid:id= "@+id/center" layout= "@layout/initial_screen" />
<includeandroid:id= "@+id/right" layout= "@layout/right_screen" />
</com.matthieu.launcher.DragableSpace>
<com.matthieu.launcher.DragableSpace
android:id="@+id/space"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:default_screen="1"
>
<include
<include
<include
</com.matthieu.launcher.DragableSpace>
values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="DragableSpace"> <attr name="default_screen" format="integer"/> </declare-styleable> </resources>
- android之luncher滑动效果
- Android 类似luncher 左右拖动效果
- 使用luncher实现类似iphone平滑滑动效果
- android luncher布局修改
- 【Android】 Android 滑动效果之ViewFlipper+GestureDetector
- android学习之-TextView的滑动效果
- Android图片之滑动效果(Gallery)
- android学习之-TextView的滑动效果
- Android之ScrollLayout左右滑动效果实现
- Luncher
- Luncher
- android 滑动效果---左右滑动
- android 创建桌面图标,luncher图标
- android的luncher的修改
- jQuery效果之滑动
- android 幕滑动效果
- Android 滑动效果----------ViewFlipper
- Android引导滑动效果
- 在线颜色生成器
- Ubuntu 上安装 DB2 Express-C
- 一起来学REST(7)——AJAX和REST
- C# MSChart 中柱状图和X轴自定义标签
- 一个FTP客户的简单的代码流程+简要总结分析
- android之luncher滑动效果
- linux+arm环境配置——minicom使用
- threads5 互斥信号量type
- 有理想的程序员必须知道的15件事
- InvalidateRect是干啥的
- Oracle中over(), rank()使用的两个例子
- 软件架构师探讨
- asp.net响应回车事件(包含模板页和非模板页)
- QT版:QQ面板抽屉效果实现