1

这不是之前提出的任何问题的重复。我想要一个双面幻灯片菜单。我提到了这个图书馆

我试图编辑这个项目,这里是我相应的文件: Sample Activity.java

package com.example.flyoutmenuexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

import com.example.flyoutmenuexample.view.viewgroup.FlyOutContainer;

public class SampleActivity extends Activity {

    FlyOutContainer root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.root = (FlyOutContainer) this.getLayoutInflater().inflate(
                R.layout.activity_sample, null);
        this.setContentView(root);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sample, menu);
        return true;
    }

    public void toggleLeftMenu(View v) {
        this.root.toggleLeftMenu();
    }

    public void toggleRightMenu(View v) {
        this.root.toggleRightMenu();
    }

}

FlyOutContainer.java

package com.example.flyoutmenuexample.view.viewgroup;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Interpolator;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class FlyOutContainer extends LinearLayout {

    Context ctx = getContext();
    WindowManager wm = (WindowManager) ctx
            .getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    int width = display.getWidth(), height = display.getHeight();

    // References to groups contained in this view.
    private View leftMenu, rightMenu;
    private View content;

    // Layout Constants
    protected static final int menuMargin = 150;

    public enum MenuState {
        CLOSED, OPEN, CLOSING, OPENING
    };

    // Position information attributes
    protected int currentContentOffset = 0;
    protected MenuState menuCurrentState = MenuState.CLOSED;

    // Animation objects
    protected Scroller menuAnimationScroller = new Scroller(this.getContext(),
            null);
    // new LinearInterpolator());
    // protected Scroller menuAnimationScroller = new
    // Scroller(this.getContext(),
    // new SmoothInterpolator());
    protected Runnable menuAnimationRunnable = new AnimationRunnable();
    protected Handler menuAnimationHandler = new Handler();

    // Animation constants
    private static final int menuAnimationDuration = 500;
    private static final int menuAnimationPollingInterval = 16;

    public FlyOutContainer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public FlyOutContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlyOutContainer(Context context) {
        super(context);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        this.leftMenu = this.getChildAt(0);
        this.content = this.getChildAt(1);
        this.rightMenu = this.getChildAt(2);

        this.leftMenu.setVisibility(View.GONE);
        this.rightMenu.setVisibility(View.GONE);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        if (changed)
            this.calculateChildDimensions();

        this.leftMenu.layout(left, top, right - menuMargin, bottom);

        this.content.layout(left + this.currentContentOffset, top, right
                + this.currentContentOffset, bottom);
        this.rightMenu.layout(left +menuMargin, top, right, bottom);

    }

    public void toggleLeftMenu() {
        switch (this.menuCurrentState) {
        case CLOSED:
            this.menuCurrentState = MenuState.OPENING;
            this.leftMenu.setVisibility(View.VISIBLE);
            this.menuAnimationScroller.startScroll(0, 0,
                    this.getLeftMenuWidth(), 0, menuAnimationDuration);
            break;
        case OPEN:
            this.menuCurrentState = MenuState.CLOSING;
            this.menuAnimationScroller.startScroll(this.currentContentOffset,
                    0, -this.currentContentOffset, 0, menuAnimationDuration);
            break;
        default:
            return;
        }

        this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable,
                menuAnimationPollingInterval);

        this.invalidate();
    }

    public void toggleRightMenu() {
        switch (this.menuCurrentState) {
        case CLOSED:
            this.menuCurrentState = MenuState.OPENING;
            this.rightMenu.setVisibility(View.VISIBLE);
            this.menuAnimationScroller.startScroll(this.width, 0,
                    this.getRightMenuWidth(), 0, menuAnimationDuration);
            break;
        case OPEN:
            this.menuCurrentState = MenuState.CLOSING;
            this.menuAnimationScroller.startScroll(this.currentContentOffset,
                    0, this.currentContentOffset, 0, menuAnimationDuration);
            break;
        default:
            return;
        }
        this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable,
                menuAnimationPollingInterval);

        this.invalidate();
    }

    private int getRightMenuWidth() {
        return  this.rightMenu.getLayoutParams().width;
    }

    private int getLeftMenuWidth() {
        return this.leftMenu.getLayoutParams().width;
    }

    private void calculateChildDimensions() {
        this.content.getLayoutParams().height = this.getHeight();
        this.content.getLayoutParams().width = this.getWidth();

        this.leftMenu.getLayoutParams().width = this.getWidth() - menuMargin;
        this.leftMenu.getLayoutParams().height = this.getHeight();

        this.rightMenu.getLayoutParams().width = this.getWidth() - menuMargin;
        this.rightMenu.getLayoutParams().height = this.getHeight();
    }

    private void adjustContentPosition(boolean isAnimationOngoing) {
        int scrollerOffset = this.menuAnimationScroller.getCurrX();

        this.content.offsetLeftAndRight(scrollerOffset
                - this.currentContentOffset);

        this.currentContentOffset = scrollerOffset;

        this.invalidate();

        if (isAnimationOngoing)
            this.menuAnimationHandler.postDelayed(this.menuAnimationRunnable,
                    menuAnimationPollingInterval);
        else
            this.onMenuTransitionComplete();
    }

    private void onMenuTransitionComplete() {
        switch (this.menuCurrentState) {
        case OPENING:
            this.menuCurrentState = MenuState.OPEN;
            break;
        case CLOSING:
            this.menuCurrentState = MenuState.CLOSED;
            this.leftMenu.setVisibility(View.GONE);
            break;
        default:
            return;
        }
    }

    protected class SmoothInterpolator implements Interpolator {

        @Override
        public float getInterpolation(float t) {
            return (float) Math.pow(t - 1, 5) + 1;
        }

    }

    protected class AnimationRunnable implements Runnable {

        @Override
        public void run() {
            FlyOutContainer.this
                    .adjustContentPosition(FlyOutContainer.this.menuAnimationScroller
                            .computeScrollOffset());
        }

    }
}

活动样本.xml

    <com.example.flyoutmenuexample.view.viewgroup.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Left Drawer Starts -->

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleLeftMenu"
            android:text="Button 1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleLeftMenu"
            android:text="Button 2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleLeftMenu"
            android:text="Button 3" />
    </LinearLayout>
    <!-- Left Drawer Ends -->


    <!-- Center Starts -->

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#888888"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:lines="2"
            android:onClick="toggleLeftMenu"
            android:text="Toggle Left" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:lines="2"
            android:onClick="toggleRightMenu"
            android:text="Toggle Right" />
    </RelativeLayout>
    <!-- Center Ends -->


    <!-- Right Starts -->

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleRightMenu"
            android:text="Button 1" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleRightMenu"
            android:text="Button 2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="toggleRightMenu"
            android:text="Button 3" />
    </LinearLayout>
    <!-- Right Ends -->

</com.example.flyoutmenuexample.view.viewgroup.FlyOutContainer>

我面临的逻辑问题是, 单击右侧切换按钮时,右侧的滑动菜单没有动画,并且它覆盖了与左侧滑动菜单相同的屏幕数量,但背景主要内容屏幕消失了

我已经详细介绍了我的问题,您可以直接复制粘贴这些文件并运行项目以更好地了解我的问题

感谢和问候

4

1 回答 1

0

您可以使用 jfeinstein 的库https://github.com/jfeinstein10/SlidingMenu,它允许您在内容的左侧和右侧都有菜单。

于 2013-10-30T18:00:04.697 回答