0

我正在开发一个将屏幕从右向左移动的应用程序。我已经实现了屏幕从左向右的移动,但是我在从右向左移动时遇到了问题。

以下是我的代码,它适用于从左到右

ExpandAnimation 

/*  Created by Edward Akoto on 12/31/12.
 *  Email akotoe@aua.ac.ke
 *  Free for modification and distribution
 */


package com.example.hotelmenu;

import android.view.Gravity;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;

public class ExpandAnimation extends TranslateAnimation implements Animation.AnimationListener{

    private LinearLayout slidingLayout;
    int panelWidth;

    public ExpandAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
            float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

        super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);

        //Initialize
        slidingLayout = layout;
        panelWidth = width;
        setDuration(400);
        setFillAfter( false );
        setInterpolator(new AccelerateDecelerateInterpolator());
        setAnimationListener(this);
        slidingLayout.startAnimation(this);
    }


    public void onAnimationEnd(Animation arg0) {

        //Create margin and align left
        LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
        params.leftMargin = panelWidth;
        params.gravity = Gravity.LEFT;     
        slidingLayout.clearAnimation();
        slidingLayout.setLayoutParams(params);
        slidingLayout.requestLayout();

    }

    public void onAnimationRepeat(Animation arg0) {

    }

    public void onAnimationStart(Animation arg0) {

    }

}

CollapseAnimation 

/*  Created by Edward Akoto on 12/31/12.
 *  Email akotoe@aua.ac.ke
 *  Free for modification and distribution
 */

package com.example.hotelmenu;

import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;


public class CollapseAnimation extends TranslateAnimation implements TranslateAnimation.AnimationListener{

    private LinearLayout slidingLayout;
    int panelWidth;

    public CollapseAnimation(LinearLayout layout, int width, int fromXType, float fromXValue, int toXType,
            float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) {

        super(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue);

        //Initialize
        slidingLayout = layout;
        panelWidth = width;
        setDuration(400);
        setFillAfter( false );
        setInterpolator(new AccelerateDecelerateInterpolator());
        setAnimationListener(this);

        //Clear left and right margins
        LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
        params.rightMargin = 0;
        params.leftMargin = 0;
        slidingLayout.setLayoutParams(params);
        slidingLayout.requestLayout();       
        slidingLayout.startAnimation(this);

    }
    public void onAnimationEnd(Animation animation) {

    }

    public void onAnimationRepeat(Animation animation) {

    }

    public void onAnimationStart(Animation animation) {

    }

}

private LinearLayout slidingPanel;
    private boolean isExpanded;
    private DisplayMetrics metrics; 
    private LinearLayout menuPanel;
    private int panelWidth;
    FrameLayout.LayoutParams menuPanelParameters;
    FrameLayout.LayoutParams slidingPanelParameters;
    LinearLayout.LayoutParams headerPanelParameters ;
    LinearLayout.LayoutParams listViewParameters;
    FrameLayout mainframe;
metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        panelWidth = 200;//(int) ((metrics.widthPixels)*0.5);

menuPanel = (LinearLayout) findViewById(R.id.menuPanel);
        menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
        menuPanelParameters.width = panelWidth;
        menuPanel.setLayoutParams(menuPanelParameters);

        slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
        slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
        slidingPanelParameters.width = metrics.widthPixels;
        slidingPanel.setLayoutParams(slidingPanelParameters);

        mainframe = (FrameLayout) findViewById(R.id.mainframe);
        listViewParameters = (LinearLayout.LayoutParams) mainframe.getLayoutParams();
        listViewParameters.width = metrics.widthPixels;
        mainframe.setLayoutParams(listViewParameters);

if(!isExpanded){
                isExpanded = true;                                              
                menuPanel.setVisibility(View.VISIBLE);
                //Expand
                new ExpandAnimation(slidingPanel, panelWidth,
                Animation.RELATIVE_TO_SELF, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);                                        
            }else{
                isExpanded = false;
                menuPanel.setVisibility(View.GONE);
                //Collapse
                new CollapseAnimation(slidingPanel,panelWidth,
                TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);


            }     
4

1 回答 1

0

在 github 上试试这个代码。

https://github.com/gitgrimbo/android-sliding-menu-demo/

这是您想要的一个很好的例子

于 2013-03-19T10:37:49.360 回答