界面右边菜单滑入划出效果

用到的几个简单动画效果

left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="100"
        android:fromXDelta="-150"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0" />

</set>

menu_and_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="100"
        android:fromXDelta="150"
        android:fromYDelta="0"
        android:toXDelta="0"
        android:toYDelta="0" />

</set>

menu_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="100"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="150"
        android:toYDelta="0" />
</set>

布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="150dp"
        android:layout_height="fill_parent"
        android:visibility="visible"
        android:layout_alignParentRight="true"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="menu" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/main"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_toLeftOf="@+id/menu" >

        <Button
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_gravity="center"
            android:text="show menu"
            android:id="@+id/btn" />
    </LinearLayout>



</RelativeLayout>

code

package com.testformenu;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

	private LinearLayout menu = null;
	private LinearLayout main = null;

	private Animation outAnimation = null;
	private Animation leftInAnimation = null;
	private Animation menuInAnimation = null;
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button btn = (Button) findViewById(R.id.btn);
		menu = (LinearLayout) findViewById(R.id.menu);
		main = (LinearLayout) findViewById(R.id.main);
		outAnimation = AnimationUtils.loadAnimation(this, R.anim.menu_and_left_out);
		leftInAnimation = AnimationUtils.loadAnimation(this, R.anim.left_in);
		menuInAnimation = AnimationUtils.loadAnimation(this, R.anim.menu_in);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (menu.getVisibility() == View.VISIBLE) {
					main.startAnimation(leftInAnimation);
					menu.startAnimation(menuInAnimation);
					menu.setVisibility(View.GONE);
				} else {
					main.startAnimation(outAnimation);
					menu.startAnimation(outAnimation);
					menu.setVisibility(View.VISIBLE);
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}

相关推荐