使用动画从左到右更改幻灯片菜单从右到左

时间:2013-10-14 05:04:36

标签: android android-animation

在我的代码中,我的滑出菜单从左向右改变方向,而我希望它从右向左改变,这是我的主要活动:

public class LayerStack extends Activity {

// Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private RelativeLayout headerPanel;
private RelativeLayout menuPanel;
private int panelWidth;
private ImageView menuViewButton;
Button menu1;
Button menu2;
TextView txtpays;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters;
LinearLayout.LayoutParams listViewParameters;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layer_stack);

    // Initialize
    metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    panelWidth = (int) ((metrics.widthPixels) * 0.75);

    headerPanel = (RelativeLayout) findViewById(R.id.header);
    headerPanelParameters = (LinearLayout.LayoutParams) headerPanel
            .getLayoutParams();
    headerPanelParameters.width = metrics.widthPixels;
    headerPanel.setLayoutParams(headerPanelParameters);

    menuPanel = (RelativeLayout) 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);

    // Slide the Panel

    menu1 = (Button) findViewById(R.id.menu_item_1);

    menu2 = (Button) findViewById(R.id.menu_item_2);

    menuViewButton = (ImageView) findViewById(R.id.menuViewButton);

    menuViewButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (!isExpanded) {
                isExpanded = true;

                // Expand
                new ExpandAnimation(slidingPanel, panelWidth,
                        Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, 0.75f, 0, 0.0f, 0, 0.0f);
            } else {
                isExpanded = false;

                // Collapse
                new CollapseAnimation(slidingPanel, panelWidth,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.75f,
                        TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f,
                        0, 0.0f);

            }
        }
    });

}

}

这是我的expandAnimation类:

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  *******************************put setting in blue lines in left side
    LayoutParams params = (LayoutParams) slidingLayout.getLayoutParams();
    params.leftMargin = panelWidth;
    params.gravity = Gravity.LEFT;     
    slidingLayout.clearAnimation();
    slidingLayout.setLayoutParams(params);
    slidingLayout.requestLayout();

}

我的折叠课程:

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);

}

和我的XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" 
android:layout_weight="1"
android:background="@drawable/blue_bg">

<!-- Menu Panel -->
<RelativeLayout
       android:id="@+id/menuPanel"
       android:layout_height="match_parent"
       android:layout_width="wrap_content"
       android:gravity="right"
       android:background="@drawable/gray_bg"
       android:orientation="vertical" >

        <Button
            android:id="@+id/menu_title_1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="0dp" 
            android:paddingLeft="15dp"             
            android:gravity="center_vertical"
            android:background="#353535"
            android:textColor="@android:color/white"
            android:text="@string/menu_title">     
        </Button>

        <View
            android:id="@+id/menu_item_divider_1"
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="0dp"
            android:layout_marginRight="0dp"
            android:layout_below="@+id/menu_title_1"
            android:background="#b5b5b5"/>

        <Button
            android:id="@+id/menu_item_1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_marginLeft="15dp"             
            android:gravity="center_vertical"
            android:layout_below="@+id/menu_item_divider_1"
            android:text="Pays libres ">     
        </Button>     
         <View
            android:id="@+id/menu_item_divider_2"
            android:layout_width="fill_parent"
            android:layout_height="0.5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_below="@+id/menu_item_1"
            android:background="#b5b5b5"/> 
        <Button
            android:id="@+id/menu_item_2"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/menu_item_divider_2"
            android:layout_marginLeft="15dp"
            android:gravity="center_vertical"
            android:text="Pays en attente">
        </Button>


        </RelativeLayout>

   <!-- Sliding Panel   -->   
    <LinearLayout
            android:id="@+id/slidingPanel"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="left"
            android:orientation="vertical"
            android:background="@android:color/white" >

            <RelativeLayout
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="@drawable/blue_bg" >

                <View
                    android:id="@+id/header_vertical_divider_1"
                    android:layout_width="2dp"
                    android:layout_height="fill_parent"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="15dp"
                    android:layout_toRightOf="@+id/menuViewButton"
                    android:background="@drawable/engraved_bg" />

                    <ImageView
                        android:id="@+id/menuViewButton"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_centerVertical="true"
                        android:layout_marginLeft="15dp"
                        android:clickable="true"
                        android:contentDescription="@string/description"
                        android:src="@drawable/icon_menu"
                        android:visibility="visible" />

            </RelativeLayout>


            <!--  from this point -->
        <LinearLayout

android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:orientation="vertical" >

</LinearLayout>


            <!--  
              <TextView
            android:id="@+id/listepays"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_below="@+id/menu_item_divider_2"
            android:layout_marginLeft="15dp"
            android:gravity="center_vertical" 
             android:textColorHighlight="#550022"  
            android:editable = "true">

        </TextView>

            <ListView
                android:id="@+id/listView1"
                android:layout_width="match_parent"
                android:layout_height="319dp" >

            </ListView>


            -->

    </LinearLayout>


</FrameLayout>

事先感谢您的帮助:)

0 个答案:

没有答案