package com.android.launcher2;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;
public class AllAppScroller extends ViewGroup {
private boolean mFirstLayout = true;
private float finalx = 0;
private boolean isPortrait = false;
private int screenWidth = 1280;
private Scroller mScroller;
public void setScreenWidth(int screenWidth) {
this.screenWidth = screenWidth;
}
public AllAppScroller(Context context) {
super(context);
initScroller();
}
public AllAppScroller(Context context, AttributeSet attrs) {
super(context, attrs);
initScroller();
}
private void initScroller() {
// TODO Auto-generated method stub
mScroller = new Scroller(getContext());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int width = isPortrait ? 300 : 383;
// The children are given the same width and height as the workspace
final int count = getChildCount();
for (int i = 0; i < count; i++) {
if (isPortrait)
getChildAt(i).measure(300, 363);
else
getChildAt(i).measure(383, 280);
}
if (mFirstLayout) {
// if (isPortrait)
// scrollTo((count * width - screenWidth) / 2, 0);
// else
scrollTo((count * width - screenWidth) / 2 + 1280, 0);
// updateWallpaperOffset(width * (getChildCount() - 1));
mFirstLayout = false;
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childLeft = 1280;
final int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != View.GONE) {
final int childWidth = isPortrait ? 300 : 383;
child.layout(childLeft, 0, childLeft + childWidth, child
.getMeasuredHeight());
childLeft += childWidth;
}
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
finalx = ev.getX();
return false;
case MotionEvent.ACTION_MOVE:
return true;
case MotionEvent.ACTION_UP:
return false;
default:
return false;
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
switch (action) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
final int deltaX = (int) (finalx - x);
finalx = x;
final int mScrollX = this.getScrollX();
if (deltaX < 0) {
if (mScrollX > 0) {
scrollBy(Math.max(-mScrollX, deltaX), 0);
}
} else if (deltaX > 0) {
final int availableToScroll = (getChildAt(getChildCount() - 1)
.getRight()
- mScrollX - screenWidth + 1280);
if (availableToScroll > 0) {
scrollBy(Math.min(availableToScroll, deltaX), 0);
}
}
break;
case MotionEvent.ACTION_UP:
int whichone = (getScrollX() - 1280 + 640 + 383) / 383;
whichone = whichone > 0 ? whichone : 1;
whichone = whichone <= getChildCount() ? whichone : getChildCount();
int dx = 1280 + whichone * 383 - 191 - getScrollX() - 640;
mScroller.startScroll(getScrollX(), 0, dx, 0, Math.abs(dx));
invalidate();
break;
default:
break;
}
return true;
}
public void setOrientation(boolean isPortrait) {
this.isPortrait = isPortrait;
}
//隐藏方法,复写以实现使用scroller自动滚动
public void computeScroll() {
if (mScroller.computeScrollOffset()) {
mScrollX = mScroller.getCurrX();
mScrollY = mScroller.getCurrY();
// updateWallpaperOffset();
postInvalidate();
}
}
}