public class MainActivity extends Activity implements OnTouchListener {
public static final int SNAP_VELOCITY = 200;
private int screenWidth;
private int leftEdge;
private int rightEdge = 0;
private int menuPadding = 80;
private View content;
private View menu;
private menuParams;
private float xDown;
private float xMove;
private float xUp;
private boolean isMenuVisible;
private VelocityTracker mVelocityTracker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(.activity_main);
initValues();
(this);
}
private void initValues() {
WindowManager window = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
screenWidth = ().getWidth();
content = findViewById();
menu = findViewById();
menuParams = () ();
= screenWidth - menuPadding;
leftEdge = -;
= leftEdge;
().width = screenWidth;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
createVelocityTracker(event);
switch (()) {
case MotionEvent.ACTION_DOWN:
xDown = ();
break;
case MotionEvent.ACTION_MOVE:
xMove = ();
int distanceX = (int) (xMove - xDown);
if (isMenuVisible) {
= distanceX;
} else {
= leftEdge + distanceX;
}
if ( < leftEdge) {
= leftEdge;
} else if ( > rightEdge) {
= rightEdge;
}
(menuParams);
break;
case MotionEvent.ACTION_UP:
xUp = ();
if (wantToShowMenu()) {
if (shouldScrollToMenu()) {
scrollToMenu();
} else {
scrollToContent();
}
} else if (wantToShowContent()) {
if (shouldScrollToContent()) {
scrollToContent();
} else {
scrollToMenu();
}
}
recycleVelocityTracker();
break;
}
return true;
}
private boolean wantToShowContent() {
return xUp - xDown < 0 && isMenuVisible;
}
private boolean wantToShowMenu() {
return xUp - xDown > 0 && !isMenuVisible;
}
private boolean shouldScrollToMenu() {
return xUp - xDown > screenWidth / 2 || getScrollVelocity() > SNAP_VELOCITY;
}
private boolean shouldScrollToContent() {
return xDown - xUp + menuPadding > screenWidth / 2 || getScrollVelocity() > SNAP_VELOCITY;
}
private void scrollToMenu() {
new ScrollTask().execute(30);
}
private void scrollToContent() {
new ScrollTask().execute(-30);
}
private void createVelocityTracker(MotionEvent event) {
if (mVelocityTracker == null) {
mVelocityTracker = ();
}
(event);
}
private int getScrollVelocity() {
(1000);
int velocity = (int) ();
return (velocity);
}
private void recycleVelocityTracker() {
();
mVelocityTracker = null;
}
class ScrollTask extends AsyncTask<Integer, Integer, Integer> {
@Override
protected Integer doInBackground(Integer... speed) {
int leftMargin = ;
while (true) {
leftMargin = leftMargin + speed[0];
if (leftMargin > rightEdge) {
leftMargin = rightEdge;
break;
}
if (leftMargin < leftEdge) {
leftMargin = leftEdge;
break;
}
publishProgress(leftMargin);
sleep(20);
}
if (speed[0] > 0) {
isMenuVisible = true;
} else {
isMenuVisible = false;
}
return leftMargin;
}
@Override
protected void onProgressUpdate(Integer... leftMargin) {
= leftMargin[0];
(menuParams);
}
@Override
protected void onPostExecute(Integer leftMargin) {
= leftMargin;
(menuParams);
}
}
private void sleep(long millis) {
try {
(millis);
} catch (InterruptedException e) {
();
}
}
}