Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom menu #162

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ResideMenu/res/layout/residemenu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
android:id="@+id/sv_left_menu"
android:scrollbars="none"
android:paddingLeft="30dp"
android:layout_width="150dp"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/layout_left_menu"
Expand All @@ -37,7 +37,7 @@
android:id="@+id/sv_right_menu"
android:scrollbars="none"
android:paddingRight="30dp"
android:layout_width="150dp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right">
<LinearLayout
Expand Down
54 changes: 33 additions & 21 deletions ResideMenu/src/com/special/ResideMenu/ResideMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ public class ResideMenu extends FrameLayout{

public static final int DIRECTION_LEFT = 0;
public static final int DIRECTION_RIGHT = 1;
private static final int PRESSED_MOVE_HORIZANTAL = 2;
private static final int PRESSED_MOVE_HORIZONTAL = 2;
private static final int PRESSED_DOWN = 3;
private static final int PRESSED_DONE = 4;
private static final int PRESSED_MOVE_VERTICAL = 5;

private ImageView imageViewShadow;
private ImageView imageViewBackground;
private View leftMenuView;
private View rightMenuView;
private View showingMenuView;
private LinearLayout layoutLeftMenu;
private LinearLayout layoutRightMenu;
private ScrollView scrollViewLeftMenu;
private ScrollView scrollViewRightMenu;
private ScrollView scrollViewMenu;
/** the activity that view attach to */
private Activity activity;
/** the decorview of the activity */
Expand Down Expand Up @@ -81,6 +83,8 @@ private void initViews(Context context){
layoutLeftMenu = (LinearLayout) findViewById(R.id.layout_left_menu);
layoutRightMenu = (LinearLayout) findViewById(R.id.layout_right_menu);
imageViewBackground = (ImageView) findViewById(R.id.iv_background);
leftMenuView = scrollViewLeftMenu;
rightMenuView = scrollViewRightMenu;
}

@Override
Expand Down Expand Up @@ -264,7 +268,7 @@ public void openMenu(int direction){
AnimatorSet scaleDown_activity = buildScaleDownAnimation(viewActivity, mScaleValue, mScaleValue);
AnimatorSet scaleDown_shadow = buildScaleDownAnimation(imageViewShadow,
mScaleValue + shadowAdjustScaleX, mScaleValue + shadowAdjustScaleY);
AnimatorSet alpha_menu = buildMenuAnimation(scrollViewMenu, 1.0f);
AnimatorSet alpha_menu = buildMenuAnimation(showingMenuView, 1.0f);
scaleDown_shadow.addListener(animationListener);
scaleDown_activity.playTogether(scaleDown_shadow);
scaleDown_activity.playTogether(alpha_menu);
Expand All @@ -279,7 +283,7 @@ public void closeMenu(){
isOpened = false;
AnimatorSet scaleUp_activity = buildScaleUpAnimation(viewActivity, 1.0f, 1.0f);
AnimatorSet scaleUp_shadow = buildScaleUpAnimation(imageViewShadow, 1.0f, 1.0f);
AnimatorSet alpha_menu = buildMenuAnimation(scrollViewMenu, 0.0f);
AnimatorSet alpha_menu = buildMenuAnimation(showingMenuView, 0.0f);
scaleUp_activity.addListener(animationListener);
scaleUp_activity.playTogether(scaleUp_shadow);
scaleUp_activity.playTogether(alpha_menu);
Expand All @@ -306,10 +310,10 @@ private void setScaleDirection(int direction){
float pivotY = getScreenHeight() * 0.5f;

if (direction == DIRECTION_LEFT){
scrollViewMenu = scrollViewLeftMenu;
showingMenuView = leftMenuView;
pivotX = screenWidth * 1.5f;
}else{
scrollViewMenu = scrollViewRightMenu;
showingMenuView = rightMenuView;
pivotX = screenWidth * -0.5f;
}

Expand Down Expand Up @@ -340,7 +344,7 @@ public void onClick(View view) {
@Override
public void onAnimationStart(Animator animation) {
if (isOpened()){
showScrollViewMenu(scrollViewMenu);
showMenu(showingMenuView);
if (menuListener != null)
menuListener.openMenu();
}
Expand All @@ -355,8 +359,8 @@ public void onAnimationEnd(Animator animation) {
}else{
viewActivity.setTouchDisable(false);
viewActivity.setOnClickListener(null);
hideScrollViewMenu(scrollViewLeftMenu);
hideScrollViewMenu(scrollViewRightMenu);
hideMenu(leftMenuView);
hideMenu(rightMenuView);
if (menuListener != null)
menuListener.closeMenu();
}
Expand Down Expand Up @@ -507,7 +511,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
break;

if(pressedState != PRESSED_DOWN &&
pressedState != PRESSED_MOVE_HORIZANTAL)
pressedState != PRESSED_MOVE_HORIZONTAL)
break;

int xOffset = (int) (ev.getX() - lastActionDownX);
Expand All @@ -519,19 +523,19 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
break;
}
if(xOffset < -50 || xOffset > 50) {
pressedState = PRESSED_MOVE_HORIZANTAL;
pressedState = PRESSED_MOVE_HORIZONTAL;
ev.setAction(MotionEvent.ACTION_CANCEL);
}
} else if(pressedState == PRESSED_MOVE_HORIZANTAL) {
} else if(pressedState == PRESSED_MOVE_HORIZONTAL) {
if (currentActivityScaleX < 0.95)
showScrollViewMenu(scrollViewMenu);
showMenu(showingMenuView);

float targetScale = getTargetScale(ev.getRawX());
ViewHelper.setScaleX(viewActivity, targetScale);
ViewHelper.setScaleY(viewActivity, targetScale);
ViewHelper.setScaleX(imageViewShadow, targetScale + shadowAdjustScaleX);
ViewHelper.setScaleY(imageViewShadow, targetScale + shadowAdjustScaleY);
ViewHelper.setAlpha(scrollViewMenu, (1 - targetScale) * 2.0f);
ViewHelper.setAlpha(showingMenuView, (1 - targetScale) * 2.0f);

lastRawX = ev.getRawX();
return true;
Expand All @@ -542,7 +546,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
case MotionEvent.ACTION_UP:

if (isInIgnoredView) break;
if (pressedState != PRESSED_MOVE_HORIZANTAL) break;
if (pressedState != PRESSED_MOVE_HORIZONTAL) break;

pressedState = PRESSED_DONE;
if (isOpened()){
Expand Down Expand Up @@ -592,15 +596,23 @@ public interface OnMenuListener{
public void closeMenu();
}

private void showScrollViewMenu(ScrollView scrollViewMenu){
if (scrollViewMenu != null && scrollViewMenu.getParent() == null){
addView(scrollViewMenu);
private void showMenu(View menuView){
if (menuView != null && menuView.getParent() == null){
addView(menuView);
}
}

private void hideScrollViewMenu(ScrollView scrollViewMenu){
if (scrollViewMenu != null && scrollViewMenu.getParent() != null){
removeView(scrollViewMenu);
private void hideMenu(View menuView){
if (menuView != null && menuView.getParent() != null){
removeView(menuView);
}
}

public void setLeftMenuView(View menuView){
this.leftMenuView = menuView;
}

public void setRightMenuView(View menuView){
this.rightMenuView = menuView;
}
}
13 changes: 13 additions & 0 deletions ResideMenuDemo/res/layout/left_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"/>
</LinearLayout>
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.special.ResideMenuDemo;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.special.ResideMenu.ResideMenu;
import com.special.ResideMenu.ResideMenuItem;
import com.special.ResideMenuDemo.R;

public class MenuActivity extends FragmentActivity implements View.OnClickListener{

Expand Down Expand Up @@ -61,6 +65,9 @@ private void setUpMenu() {
// You can disable a direction by setting ->
// resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);

Button btn = new Button(this);
resideMenu.setLeftMenuView(btn);

findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down