通过Android中的触摸,像SKOUT和从一个视图滑动到另一个视图的菜单

Nar*_*rma 29 android slidingdrawer android-layout android-fragments android-viewpager

我想实现像FB或G + app这样的滑动菜单,我从FB菜单演示https://github.com/jfeinstein10/SlidingMenu中找到了一些示例代码

这些都是好的开始,但我需要额外的东西.就像这里一样,它仅适用于单击菜单按钮,但我也希望通过手势移动它.我希望有一个中心视图的行为,并且在向右移动该中心时,将出现一个视图,并且在向左移动时,菜单将出现.假设有三个视图A,B,C,当我向左滑动C然后出现A时,当我向右滑动C然后出现B. C位于A和B的中间.

1.中间视图向右移动

那个屏幕在中间 走向正确 菜单出现

2.将中间视图移向左侧

通过触摸向左移动 向左移动第三种观点

现在我的问题是:开发这样的观点的最佳实践是什么.我从某人那里听说我应该使用片段和View寻呼机.那么我该如何开发呢?是否有人做过任何样本实施?任何帮助和建议表示赞赏.

有关参考,请参阅此应用程序使用此类型的滑动黑白视图Skout应用程序

Com*_*are 17

最简单的解决方案可能是使用android-undergarment,它内置了边框滑动,基于项目自述文件:

用户还可以通过从屏幕左侧滑动的挡板来控制抽屉以打开抽屉,并从右侧执行相同操作以关闭抽屉.如果要阻止此触摸功能,可以调用setDrawerEnabled(false).


Abh*_*ndi 12

您可以简单地使用TranslateAnimation您想要移动的视图以及用于淡入淡出的弹出窗口和用于您的菜单的另一个弹出窗口.我已经在我的应用程序中实现了它,它就像一个魅力.
此图像显示何时需要动画和其他组件


代码:

public class SlidingOptionsMenuActivity extends Activity {

    /**
     * Signifies that the menu is already visible
     */
    boolean alreadyShowing = false;
    /**
     * Width of the current window
     */
    private int windowWidth;
    /**
     * Height of the current window
     */
    private int windowHeight;
    /**
     * Reference of the {@link PopupWindow} which dims the screen
     */
    private PopupWindow fadePopup;
    /**
     * The translate animation
     */
    private Animation ta;
    /**
     * The view which needs to be translated
     */
    private RelativeLayout baseView;
    /**
     * Reference of the {@link LayoutInflater}
     */
    LayoutInflater inflater;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Display display = getWindowManager().getDefaultDisplay();
        windowWidth = display.getWidth();
        windowHeight = display.getHeight();
        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        findViewById(R.id.btnOptions).setOnClickListener(new OnClickListener() {

            /*
             * (non-Javadoc)
             * 
             * @see android.view.View.OnClickListener#onClick(android.view.View)
             */
            @Override
            public void onClick(View v) {
                if(!alreadyShowing){
                    alreadyShowing = true;
                    openSlidingMenu();
                }
            }
        });
    }


    /**
     * Fades the entire screen, gives a dim background
     */
    private void showFadePopup() {
        final View layout = inflater.inflate(R.layout.fadepopup, (ViewGroup) findViewById(R.id.fadePopup));
        fadePopup = new PopupWindow(layout, windowWidth, windowHeight, false);
        fadePopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
    }

    /**
     * Opens the sliding Menu
     */
    private void openSlidingMenu() {
        showFadePopup();
        // The amount of view which needs to be moved out. equivalent to the
        // width of the menu
        int width = (int) (windowWidth * 0.6f);
        translateView((float) (width));
        int height = LayoutParams.FILL_PARENT;
        // creating a popup
        final View layout = inflater.inflate(R.layout.option_popup_layout,(ViewGroup) findViewById(R.id.popup_element));

        final PopupWindow optionsPopup = new PopupWindow(layout, width, height, true);
        optionsPopup.setBackgroundDrawable(new PaintDrawable());

        optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);

        optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            public void onDismiss() {
                //Removing the fade effect
                fadePopup.dismiss();    
                //to clear the previous animation transition in
                cleanUp();
                //move the view out
                translateView(0);
                //to clear the latest animation transition out
                cleanUp();
                //resetting the variable
                alreadyShowing = false;
            }
        });
    }

    /**
     * This method is responsible for view translation. It applies a translation
     * animation on the root view of the activity
     * 
     * @param right The position to translate to
     */
    private void translateView(float right) {

        ta = new TranslateAnimation(0f, right, 0f, 0f);
        ta.setDuration(300);
        ta.setFillEnabled(true);
        ta.setFillAfter(true);

        baseView = (RelativeLayout) findViewById(R.id.baseView);
        baseView.startAnimation(ta);
        baseView.setVisibility(View.VISIBLE);
    }

    /**
     * Basic cleanup to avoid memory issues. Not everything is release after
     * animation, so to immediately release it doing it manually
     */
    private void cleanUp(){
        if (null != baseView) {
            baseView.clearAnimation();
            baseView = null;
        }
        if (null != ta) {
            ta.cancel();
            ta = null;
        }
        fadePopup = null;
    }
} //END of Class
//END of file
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助.

实际截图.


小智 10

另一个我发现非常好的开源库是SlidingMenu.它应该适合您的需要,因为您可以通过"菜单"单击和挡板滑动来打开和关闭抽屉.我发现将它与ActionBarSherlockjohannilsson的android- actionbar库等Actionbar库集成只需更改库项目中的一行或两行代码.SlidingMenu库的自述文件介绍了如何与ABSherlock库集成.

值得注意的是,SlidingMenu示例项目演示了许多不同的抽屉打开关闭动画.这些是我见过的这种菜单/导航风格的最佳动画.