仿QQ側拉選單

銳湃發表於2015-11-03

大體思路是通過HorizontalScrollView來實現,效果圖如下:



上程式碼:

SlidingMenu.java

import android.R.menu;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

@SuppressLint("NewApi")
public class SlidingMenu extends HorizontalScrollView {

	private DisplayMetrics outMetrics;
	private float menuWidth;
	private RelativeLayout menuLayout;
	private LinearLayout contentLayout;

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public SlidingMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		initPramas();
	}

	public SlidingMenu(Context context) {
		super(context);
	}

	private void initPramas() {
		// 螢幕的寬高
		WindowManager windowManager = (WindowManager) getContext()
				.getSystemService(Context.WINDOW_SERVICE);
		outMetrics = new DisplayMetrics();
		windowManager.getDefaultDisplay().getMetrics(outMetrics);
	}

	/**
	 * 計算控制元件的寬高
	 */
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		View childAt = getChildAt(0);
		if (childAt instanceof LinearLayout) {
			LinearLayout linearLayout = (LinearLayout) childAt;
			menuLayout = (RelativeLayout) linearLayout.getChildAt(0);
			contentLayout = (LinearLayout) linearLayout.getChildAt(1);

			menuWidth = (int) (0.8f * outMetrics.widthPixels);

			menuLayout.getLayoutParams().width = (int) menuWidth;
			menuLayout.getLayoutParams().height = (int) (outMetrics.heightPixels * 0.6f);
			contentLayout.getLayoutParams().width = outMetrics.widthPixels;
		}
	}

	/**
	 * 擺放自控制元件
	 */
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (changed) {
			this.scrollTo((int) menuWidth, 0);
		}
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		// 獲取滑出去的距離0~menuWidth
		// 1、計算滑動的比例(0 ~ 1)
		float scale = l / menuWidth;

		// 2、縮放menu(計算menu縮放的比例)
		float leftScale = 1 - scale * 0.3f;
		menuLayout.setScaleX(leftScale);
		menuLayout.setScaleY(leftScale);

		// 給我們的menu選單加透明度變化(0.3 ~ 1.0)
		float leftAlpha = 1 - scale * 0.7f;
		menuLayout.setAlpha(leftAlpha);

		// 給menu選單加上反平移效果
		// menuLayout.setTranslationX(l * 0.7f);
		menuLayout.setRotationY(leftScale * menuWidth*2);

		// 內容部分加入縮放效果(0.8~1)
		float rightScale = 0.8f + 0.2f * scale;
		contentLayout.setScaleX(rightScale);
		contentLayout.setScaleY(rightScale);
	}
	
	
}


SlidingMenuAdapter.java

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SlidingMenuAdapter extends BaseAdapter {

	public enum MenuType {
		img_1(R.drawable.img_1, "開通會員"), img_2(R.drawable.img_2, "QQ錢包"), img_3(
				R.drawable.img_3, "個性裝扮"), img_4(R.drawable.img_4, "我的收藏"), img_5(
						R.drawable.img_5, "我的相簿"), img_6(R.drawable.img_3, "我的檔案");

		private int icon;
		private String title;

		private MenuType(int icon, String title) {
			this.icon = icon;
			this.title = title;
		}

		public int getIcon() {
			return icon;
		}

		public String getTitle() {
			return title;
		}

	}

	private LayoutInflater inflater;

	public SlidingMenuAdapter(Context context) {
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return MenuType.values().length;
	}

	@Override
	public Object getItem(int position) {
		return MenuType.values()[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.item_menu, parent, false);
			viewHolder = new ViewHolder();
			viewHolder.iconIV = (ImageView) convertView
					.findViewById(R.id.iv_icon);
			viewHolder.titleTV = (TextView) convertView
					.findViewById(R.id.tv_title);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		MenuType menuType = MenuType.values()[position];
		viewHolder.iconIV.setImageResource(menuType.getIcon());
		viewHolder.titleTV.setText(menuType.getTitle());
		return convertView;
	}

	class ViewHolder {
		ImageView iconIV;
		TextView titleTV;
	}

}


MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

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

	private void initView() {
		ListView menuLV = (ListView) findViewById(R.id.lv_menu);
		SlidingMenuAdapter menuAdapter = new SlidingMenuAdapter(this);
		menuLV.setAdapter(menuAdapter);
		menuLV.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				    MenuType menuType =  (SlidingMenuAdapter.MenuType) parent.getAdapter().getItem(position);
				    CharSequence title = menuType.getTitle();
					Toast.makeText(getApplicationContext(), title, Toast.LENGTH_SHORT).show();
			}
		});
	}

}

activity_main.xml

<com.my.slidingmenu.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/menu_bg"
    android:fadingEdge="none"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <!-- 選單佈局 -->

        <include layout="@layout/menu" />

        <!-- 主介面佈局 -->

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/p17" >

            <!-- fragment -->

        </LinearLayout>
    </LinearLayout>
</com.my.slidingmenu.SlidingMenu> 

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lv_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:cacheColorHint="@android:color/transparent" />

    </LinearLayout>

</RelativeLayout>


item_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:src="@drawable/img_1" />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/iv_icon"
        android:text="開通會員"
        android:textColor="#f0f0f0"
        android:textSize="20sp" />

</RelativeLayout>

用到的素材:




整理自教程

相關文章