自定义控件(优酷菜单)

 1.activity

package com.example.youkumenu;

import com.example.youkumenu.Utils.animationUtil;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

	private RelativeLayout rl_level1;
	private RelativeLayout rl_level2;
	private RelativeLayout rl_level3;
	private ImageButton ib_home;
	private ImageButton ib_menu;
	private ImageButton ib_channel4;
	private int canRotate = 0;
	/**
	 * Level3是否可见
	 */
	private boolean isLevel3CanSee = true;
	/**
	 * Level2是否可见
	 */
	private boolean isLevel2CanSee = true;
	/**
	 * Level1是否可见
	 */
	private boolean isLevel1CanSee = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initUI();
		initOnclickShowAnimation();
	}

	/**
	 * 设置点击动画
	 */
	private void initOnclickShowAnimation() {
		// 设置rl_level2中的ib_menu的点击事件
		ib_menu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 动画未执行完就退出
				if (animationUtil.canRotate > 0) {
					return;
				}
				if (isLevel3CanSee) {
					// 如果rl_level3可见,就转出去
					animationUtil.StartOutRotateAnimation(rl_level3, 0);
				} else {
					// 如果rl_level3不可见,就转进来
					animationUtil.StartInRotateAnimation(rl_level3, 0);
				}
				// 置反
				isLevel3CanSee = !isLevel3CanSee;
			}
		});
		ib_home.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 动画未执行完就退出
				if (animationUtil.canRotate > 0) {
					return;
				}
				long dely = 0;

				if (isLevel3CanSee) {
					// 如果rl_level3可见,就转出去
					animationUtil.StartOutRotateAnimation(rl_level3, dely);
					dely += 100;
					isLevel3CanSee = !isLevel3CanSee;
				}
				if (isLevel2CanSee) {
					// 如果rl_level2可见,就转出去
					animationUtil.StartOutRotateAnimation(rl_level2, dely);
				} else {
					// 如果rl_level2不可见,就转进来
					animationUtil.StartInRotateAnimation(rl_level2, dely);
				}
				// 置反
				isLevel2CanSee = !isLevel2CanSee;
			}
		});
	}

	/**
	 * 找到控件
	 */
	private void initUI() {
		rl_level1 = (RelativeLayout) findViewById(R.id.rl_level1);
		rl_level2 = (RelativeLayout) findViewById(R.id.rl_level2);
		rl_level3 = (RelativeLayout) findViewById(R.id.rl_level3);
		ib_home = (ImageButton) findViewById(R.id.ib_home);
		ib_menu = (ImageButton) findViewById(R.id.ib_menu);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			// 动画未执行完就退出
			if (animationUtil.canRotate > 0) {
				return true;
			}
			long dely = 0;
			if (isLevel1CanSee) {
				// 转出去
				if (isLevel3CanSee) {
					animationUtil.StartOutRotateAnimation(rl_level3, dely);
					dely += 100;
					isLevel3CanSee = !isLevel3CanSee;
				}
				if (isLevel2CanSee) {
					animationUtil.StartOutRotateAnimation(rl_level2, dely);
					dely += 100;
					isLevel2CanSee = !isLevel2CanSee;
				}
				animationUtil.StartOutRotateAnimation(rl_level1, dely);
			} else {
				// 转进来
				animationUtil.StartInRotateAnimation(rl_level1, dely);
				dely += 100;
				animationUtil.StartInRotateAnimation(rl_level2, dely);
				dely += 100;
				animationUtil.StartInRotateAnimation(rl_level3, dely);
				// 置反
				isLevel2CanSee = !isLevel2CanSee;
				isLevel3CanSee = !isLevel3CanSee;
			}
			isLevel1CanSee = !isLevel1CanSee;
		}
		return super.onKeyDown(keyCode, event);
	}
}

2.utils

package com.example.youkumenu.Utils;

import android.view.View;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

public class animationUtil {
	public static int canRotate = 0;

	public static void StartOutRotateAnimation(View v, long dely) {
		RotateAnimation ra = new RotateAnimation(0, -180,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 1.0f);
		ra.setDuration(500);
		ra.setFillAfter(true);
		ra.setStartOffset(dely);
		ra.setAnimationListener(new myListener());
		v.startAnimation(ra);
	}

	public static void StartInRotateAnimation(View v, long dely) {
		RotateAnimation ra = new RotateAnimation(-180, 0,
				RotateAnimation.RELATIVE_TO_SELF, 0.5f,
				RotateAnimation.RELATIVE_TO_SELF, 1.0f);
		ra.setDuration(500);
		ra.setFillAfter(true);
		ra.setStartOffset(dely);
		ra.setAnimationListener(new myListener());
		v.startAnimation(ra);
	}

	static class myListener implements AnimationListener {

		@Override
		public void onAnimationStart(Animation animation) {
			canRotate++;
		}

		@Override
		public void onAnimationEnd(Animation animation) {
			canRotate--;
		}

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

		}

	}
}

3.布局文件

<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"
    tools:context="${relativePackage}.${activityClass}" >

    <RelativeLayout
        android:id="@+id/rl_level1"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level1" >

        <ImageButton
            android:id="@+id/ib_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@null"
            android:src="@drawable/icon_home" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_level2"
        android:layout_width="180dp"
        android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level2" >

        <ImageButton
            android:id="@+id/ib_menu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:background="@null"
            android:src="@drawable/icon_menu" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="55dp"
            android:background="@null"
            android:src="@drawable/icon_search" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="55dp"
            android:background="@null"
            android:src="@drawable/icon_myyouku" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rl_level3"
        android:layout_width="280dp"
        android:layout_height="140dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level3" >

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="10dp"
            android:background="@null"
            android:src="@drawable/channel1" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="35dp"
            android:layout_marginTop="60dp"
            android:background="@null"
            android:src="@drawable/channel2" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="70dp"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/channel3" />

        <ImageButton
            android:id="@+id/ib_channel4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@null"
            android:src="@drawable/channel4" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:background="@null"
            android:src="@drawable/channel5" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="35dp"
            android:layout_marginTop="60dp"
            android:background="@null"
            android:src="@drawable/channel6" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="70dp"
            android:layout_marginTop="20dp"
            android:background="@null"
            android:src="@drawable/channel7" />
    </RelativeLayout>

</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/sinat_40387150/article/details/81186791