仿QQ側拉選單
大體思路是通過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);
}
}
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;
}
}
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>
用到的素材:
整理自教程
相關文章
- Swift - 仿寫QQ側滑選單Swift
- Flutter | 超簡單仿微信QQ側滑選單元件Flutter元件
- (有圖)仿QQ側滑選單:RecyclerView側滑選單,長按拖拽,滑動刪除View
- 史上最簡單,一步整合側滑(刪除)選單,高仿QQ、IOS。iOS
- Flutter:手把手教你實現一個仿QQ側滑選單的功能Flutter
- 自定義view——仿酷狗的側滑選單View
- 介面無小事(六):來做個好看得側拉選單!
- 物件化JS之----仿outlook或者QQ的選單 (轉)物件JS
- Android-WItemTouchHelperPlus幾行程式碼搞定仿QQ側滑Android行程
- (六)仿QQ首頁drawer/側滑刪除/浮動imgaeView/角標拖拽View
- recyclerView的側拉效果。上拉載入。下拉重新整理,點選事件等等View事件
- 自定義控制元件?試試300行程式碼實現QQ側滑選單控制元件行程
- Android側滑選單DrawerLayout使用Android
- 左側分類導航選單
- jQuery實現左側分類選單jQuery
- 自定義View:側滑選單實現View
- HTML歷理 ICbA的側欄選單HTML
- Android 仿微信, QQ 裁剪Android
- 簡單仿QQ登入介面,儲存資訊到sd卡SD卡
- 側邊浮動網站客服QQ網站
- 電商左側商品分類選單實現
- 原生Android 側滑選單實踐(部分)Android
- JavaScript側欄緩動分享選單詳解JavaScript
- TornadoFx實現側邊欄選單效果
- layui自定義ajax左側三級選單UI
- 自定義View:側滑選單動畫實現View動畫
- android的左右側滑選單實現Android
- 微信小程式-仿QQ音樂微信小程式
- Android最簡單的側劃選單,DrawerLayout的使用Android
- 微信小程式仿閒魚『下拉選單』微信小程式
- Flutter 仿iOS側滑返回案例實現FlutteriOS
- OT部分頁面左側選單不顯示
- css3實現側邊滑動選單CSSS3
- 【只發精品】匠心打造Vue側滑選單元件Vue元件
- 類似微信、QQ的下拉選單,同時也支援選單往上彈
- Flutter 仿QQ討論組頭像Flutter
- jQuery實現高仿QQ音樂jQuery
- Vue 仿QQ左滑刪除元件Vue元件