마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

슬라이드 메뉴바 코드를 넣고 싶어요

0 추천

졸작이라 각자 할부분을 나눠서 했는데

친구가 만든 메뉴바를 어디에다 넣어야할지 모르겠습니다..

메인 액티비티에 넣어야하나요?

 

첫번째 엑티비티

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 toggleMenu(View v){
		this.root.toggleMenu();
	}

}





두번째 엑티비티

package com.example.flyoutmenuexample.view.viewgroup;

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

public class FlyOutContainer extends LinearLayout {

	// References to groups contained in this view.
	private View menu;
	private View content;

	// Constants
	protected static final int menuMargin = 300;

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

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

    protected Scroller menuAnimationScroller = new Scroller(this.getContext(),
            new SmoothInterpolator());
    protected Runnable menuAnimationRunnable = new AnimationRunnable();
    protected Handler menuAnimationHandler = new Handler();

    private static final int menuAnimationDuration = 1000;
    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.menu = this.getChildAt(0);
		this.content = this.getChildAt(1);

		this.menu.setVisibility(View.GONE);
	}

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

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

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

	}

	public void toggleMenu() {
		switch (this.menuCurrentState) {
		case CLOSED:
            this.menuCurrentState = MenuState.OPENING;
			this.menu.setVisibility(View.VISIBLE);
			this.menuAnimationScroller.startScroll(0, 0, this.getMenuWidth(),
                    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);
	}

	private int getMenuWidth() {
		return this.menu.getLayoutParams().width;
	}

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

		this.menu.getLayoutParams().width = this.getWidth() - menuMargin;
		this.menu.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.menu.setVisibility(View.GONE);
            break;
        default:
            return;
        }
    }

    protected class SmoothInterpolator implements Interpolator{

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

    private class AnimationRunnable implements Runnable {

        @Override
        public void run() {
            boolean isAnimationOngoing = FlyOutContainer.this.menuAnimationScroller
                    .computeScrollOffset();



            FlyOutContainer.this.adjustContentPosition(isAnimationOngoing);
        }
    }
}

 

꿀로이드 (550 포인트) 님이 2015년 5월 20일 질문

1개의 답변

+1 추천
 
채택된 답변
https://github.com/gitgrimbo/android-sliding-menu-demo

 

위의 사이트에 가시면 소스코드가 있습니다

보시면 충분히 쉽게 이해가 가요

 

--------------------------------------

doridori2013@nate.com
익명사용자 님이 2015년 5월 20일 답변
꿀로이드님이 2015년 5월 20일 채택됨
저 메뉴바가 필요한 java안에 넣어주면 되나요?
...