android滑块实现

时间:2013-02-02 09:06:35

标签: android android-view android-framelayout

我正试图像facebook android app一样实现滑块菜单。

我已成功滑动菜单并重新滑动。

我有两个适合framelayout的视图 1.主要讲话(X)
2.菜单布局。(Y)

我向右滑动X,在滑动和向后滑动时显示Y我将X移回0隐藏菜单。

@Override
public void setContentView(int layout) {

    FrameLayout frame = new FrameLayout(getBaseContext());
    frame.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT));


    LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    mainLayout = inflator.inflate(layout, null);

    menuList = inflator.inflate(R.layout.menu_page, null);


    fakeView= inflator.inflate(R.layout.fake_transparent_view, null);
    menuAnimator = new MenuAnimation(mainLayout,menuList,fakeView);
    frame.addView(menuList);
    frame.addView(mainLayout);
    frame.addView(fakeView);

    fakeView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mainLayout.getLeft()>0)
            {

            menuAnimator.moveMenu("left");
            }

        }
    });

    super.setContentView(frame);


}





public void moveMenu(String direction) {
        int width=(int) (QuikrApplication.getWidth()*.85);
        if (direction.equals("right")) {
            this.direction="right";
            fakeView.setVisibility(View.VISIBLE);
            TranslateAnimation moveRight = new TranslateAnimation(0, width, 0, 0);
            moveRight.setDuration(500);
            parentLayout.setAnimation(moveRight);
            moveRight.setAnimationListener(animationListner);
            parentLayout.startAnimation(moveRight);
            fakeView.startAnimation(moveRight);
        }
        else if (direction.equals("left")){
            this.direction="left";

            TranslateAnimation moveLeft = new TranslateAnimation(0, -width, 0, 0);
            moveLeft.setDuration(500);
            parentLayout.setAnimation(moveLeft);
            moveLeft.setAnimationListener(animationListner);
            parentLayout.startAnimation(moveLeft);
            fakeView.startAnimation(moveLeft);
        }
    }

现在的问题是当我在主视图中执行某些操作时异步主视图重新定位(当菜单可见时)

3 个答案:

答案 0 :(得分:1)

试试这个,

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<FrameLayout
    android:id="@+id/menu_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/fake_layouy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone" >
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="inner button" />
</FrameLayout>

<RelativeLayout
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="menu" />
</RelativeLayout>

java代码

   public class MainActivity extends Activity {

private View toplayout;
private View sublayout;
private View fakeLayout;
private int screenWidth;
private int animToPosition;
private boolean menuOpen = false;
private int oldLeft;
private int oldTop;
private int newleft;
private int newTop;
private Button button1;
private Button button2;
private AnimationListener AL;
private DisplayMetrics metrics;
private Display display;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_ugsimply_beta);
    toplayout = (View) findViewById(R.id.main_layout);
    sublayout = (View) findViewById(R.id.menu_layout);
    fakeLayout = (View) findViewById(R.id.fake_layouy);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    metrics = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    display = getWindowManager().getDefaultDisplay();
    screenWidth = display.getWidth();
    int calcAnimatePosition = (screenWidth / 4);
    animToPosition = screenWidth - calcAnimatePosition;
    RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(
            animToPosition, RelativeLayout.LayoutParams.FILL_PARENT);
    sublayout.setLayoutParams(parms);

    /** Animatio Listner */

    AL = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
            button2.setClickable(false);
            toplayout.setEnabled(false);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            if (menuOpen) {

                toplayout.layout(oldLeft, oldTop,
                        oldLeft + toplayout.getMeasuredWidth(), oldTop
                                + toplayout.getMeasuredHeight());
                menuOpen = false;
                // sublayout.setEnabled(false);
                button2.setClickable(true);
                toplayout.setEnabled(true);

            } else if (!menuOpen) {

                toplayout.layout(newleft, newTop,
                        newleft + toplayout.getMeasuredWidth(), newTop
                                + toplayout.getMeasuredHeight());
                button2.setClickable(true);

                menuOpen = true;
                toplayout.setEnabled(true);
            }

        }
    };

    button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!menuOpen) {
                animSlideRight();
            }
        }
    });

    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (menuOpen) {

                animSlideLeft();
            }
        }
    });

}

/** Animation right */

private void animSlideRight() {

    fakeLayout.setVisibility(View.VISIBLE);
    newleft = toplayout.getLeft() + animToPosition;

    newTop = toplayout.getTop();
    TranslateAnimation slideRight = new TranslateAnimation(0, newleft, 0, 0);
    slideRight.setDuration(500);
    slideRight.setFillEnabled(true);
    slideRight.setAnimationListener(AL);
    toplayout.startAnimation(slideRight);

}

/** Animation left */

private void animSlideLeft() {
    // TODO Auto-generated method stub
    fakeLayout.setVisibility(View.GONE);
    oldLeft = toplayout.getLeft() - animToPosition;

    oldTop = toplayout.getTop();
    TranslateAnimation slideLeft = new TranslateAnimation(newleft, oldLeft,
            0, 0);
    slideLeft.setDuration(500);
    slideLeft.setFillEnabled(true);
    slideLeft.setAnimationListener(AL);
    toplayout.setAnimation(slideLeft);

}

}

单击菜单按钮时,右滑动画。然后点击内置按钮它就会动画离开。检查下面的屏幕

enter image description here enter image description here

答案 1 :(得分:1)

除了LibSlideMenu之外,还有另外一个关于github的项目,现在有超过280个提交,我已经在一些非常容易实现的项目中使用了很多功能,你也可以查看它。

https://github.com/jfeinstein10/SlidingMenu

答案 2 :(得分:0)

如果我可以提出建议,github上的facebook / google + style滑块菜单有一个非常好的库实现:

https://github.com/bk138/LibSlideMenu

我用了一两个项目,效果很好。你应该尝试一下。