- 論壇徽章:
- 1
|
對于新版的QQ界面,左側(cè)增加了一個(gè)滑動(dòng)效果,相信小伙伴們早已按耐不住激動(dòng)的心情,這種效果是如何實(shí)現(xiàn)的呢?本篇我們就一起來探討一二。既然是滑動(dòng)效果這里就要使用到HorizontalScrollView類,一個(gè)水平滑動(dòng)的效果。
對于這個(gè)效果我們可以分為一個(gè)Item Menu和Layout,通過對Item Menu設(shè)置padding值來隱藏和顯示左側(cè)的Menu菜單。
我們的Menu設(shè)計(jì)代碼:- <?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="@drawable/img_frame_background" >
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_centerInParent="true"
- android:orientation="vertical">
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/img1"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/img_1"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:layout_centerVertical="true"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:textColor="#fff"
- android:text="第一個(gè)item"
- android:layout_toRightOf="@id/img1"
- android:layout_centerVertical="true"
- />
- </RelativeLayout>
-
- <RelativeLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:id="@+id/img2"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:src="@drawable/img_2"
- android:layout_marginLeft="20dp"
- android:layout_marginTop="20dp"
- android:layout_centerVertical="true"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="20dp"
- android:textColor="#fff"
- android:text="第二個(gè)item"
- android:layout_toRightOf="@id/img2"
- android:layout_centerVertical="true"
- />
- </RelativeLayout>
-
- </LinearLayout>
- </RelativeLayout>
復(fù)制代碼 我們的主Activity的Layout代碼:- <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"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context=".MainActivity" >
- <com.example.menu.SlidingMenu
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- >
-
- <include layout="@layout/left_menu"/>
-
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@drawable/qq"
- />
-
- </LinearLayout>
- </com.example.menu.SlidingMenu>
-
- </RelativeLayout>
復(fù)制代碼 我們自定義的SlidingMenu:- package com.example.menu;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.DisplayMetrics;
- import android.util.TypedValue;
- import android.view.MotionEvent;
- import android.view.ViewGroup;
- import android.view.WindowManager;
- import android.widget.HorizontalScrollView;
- import android.widget.LinearLayout;
- public class SlidingMenu extends HorizontalScrollView {
- private LinearLayout mWapper;
- private ViewGroup mMenu;
- private ViewGroup mContent;
- private int mScreenWidth;//屏幕的寬度
- private int mMenuWidth;//設(shè)置Menu的寬度
-
- //dp
- private int mMenuRightPadding;
- private boolean once = false;
-
- /**
- * 未使用自定義屬性時(shí)調(diào)用此方法
- * @param context
- * @param attrs
- */
- public SlidingMenu(Context context, AttributeSet attrs) {
- super(context, attrs);
- WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
- DisplayMetrics outMetrics = new DisplayMetrics();
- wm.getDefaultDisplay().getMetrics(outMetrics );
- mScreenWidth = outMetrics.widthPixels;
-
- //將dp轉(zhuǎn)換為px
- mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
-
- }
-
- /**
- * 設(shè)置內(nèi)部View的寬和高,以及自己的寬和高
- */
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
-
- if(!once){
- mWapper = (LinearLayout) getChildAt(0);
- mMenu = (ViewGroup) mWapper.getChildAt(0);
- mContent = (ViewGroup) mWapper.getChildAt(1);
- mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
- mContent.getLayoutParams().width = mScreenWidth;
- once = true;
- }
-
- }
- /**
- * 設(shè)置子View的放置位置
- * 通過設(shè)置偏移量來隱藏Menu
- */
- @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(mMenuWidth, 0);
- }
- }
-
- /**
- * 控制手指的滑動(dòng)效果
- */
- @Override
- public boolean onTouchEvent(MotionEvent ev) {
- int action = ev.getAction();
- switch (action) {
- case MotionEvent.ACTION_UP:
- int scrollx = getScrollX();//Menu左側(cè)隱藏的區(qū)域?qū)挾?br />
- if(scrollx >= mMenuWidth/2){
- this.smoothScrollTo(mMenuWidth, 0);
- }else{
- this.smoothScrollTo(0, 0);
- }
- return true;
- }
- return super.onTouchEvent(ev);
- }
-
- }
復(fù)制代碼 我們的主Activity:- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);//去除標(biāo)題
- setContentView(R.layout.activity_main);
-
- }
- }
復(fù)制代碼
1.png (165.54 KB, 下載次數(shù): 65)
下載附件
2015-06-01 10:11 上傳
|
|