亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1630 | 回復(fù): 0
打印 上一主題 下一主題

[Android] QQ左側(cè)滑動(dòng)顯示 [復(fù)制鏈接]

論壇徽章:
1
操作系統(tǒng)版塊每日發(fā)帖之星
日期:2016-08-21 06:20:00
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2015-06-01 10:11 |只看該作者 |倒序?yàn)g覽
對于新版的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ì)代碼:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:background="@drawable/img_frame_background" >
  6.    
  7.     <LinearLayout
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:layout_centerInParent="true"
  11.         android:orientation="vertical">
  12.         
  13.         <RelativeLayout
  14.             android:layout_width="match_parent"
  15.             android:layout_height="match_parent">
  16.             <ImageView
  17.                android:id="@+id/img1"
  18.                android:layout_width="50dp"
  19.                android:layout_height="50dp"
  20.                android:src="@drawable/img_1"
  21.                android:layout_marginLeft="20dp"
  22.                android:layout_marginTop="20dp"
  23.                android:layout_centerVertical="true"
  24.                 />
  25.             <TextView
  26.                 android:layout_width="wrap_content"
  27.                 android:layout_height="wrap_content"
  28.                 android:layout_marginLeft="20dp"
  29.                 android:textColor="#fff"
  30.                 android:text="第一個(gè)item"
  31.                 android:layout_toRightOf="@id/img1"
  32.                 android:layout_centerVertical="true"
  33.                 />
  34.        </RelativeLayout>
  35.       
  36.         <RelativeLayout
  37.             android:layout_width="match_parent"
  38.             android:layout_height="match_parent">
  39.             <ImageView
  40.                android:id="@+id/img2"
  41.                android:layout_width="50dp"
  42.                android:layout_height="50dp"
  43.                android:src="@drawable/img_2"
  44.                android:layout_marginLeft="20dp"
  45.                android:layout_marginTop="20dp"
  46.                android:layout_centerVertical="true"
  47.                 />
  48.             <TextView
  49.                 android:layout_width="wrap_content"
  50.                 android:layout_height="wrap_content"
  51.                 android:layout_marginLeft="20dp"
  52.                 android:textColor="#fff"
  53.                 android:text="第二個(gè)item"
  54.                 android:layout_toRightOf="@id/img2"
  55.                 android:layout_centerVertical="true"
  56.                 />
  57.        </RelativeLayout>
  58.         
  59.     </LinearLayout>

  60. </RelativeLayout>
復(fù)制代碼
我們的主Activity的Layout代碼:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <com.example.menu.SlidingMenu
  11.         android:layout_width="match_parent"
  12.         android:layout_height="match_parent">
  13.         <LinearLayout
  14.             android:layout_width="wrap_content"
  15.             android:layout_height="match_parent"
  16.             android:orientation="horizontal"
  17.             >
  18.             
  19.             <include layout="@layout/left_menu"/>
  20.             
  21.             <LinearLayout
  22.                 android:layout_width="match_parent"
  23.                 android:layout_height="match_parent"
  24.                 android:background="@drawable/qq"
  25.                 />
  26.             
  27.         </LinearLayout>
  28.     </com.example.menu.SlidingMenu>
  29.    
  30. </RelativeLayout>
復(fù)制代碼
我們自定義的SlidingMenu:
  1. package com.example.menu;

  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.util.DisplayMetrics;
  5. import android.util.TypedValue;
  6. import android.view.MotionEvent;
  7. import android.view.ViewGroup;
  8. import android.view.WindowManager;
  9. import android.widget.HorizontalScrollView;
  10. import android.widget.LinearLayout;

  11. public class SlidingMenu extends HorizontalScrollView {

  12.     private LinearLayout mWapper;
  13.     private ViewGroup mMenu;
  14.     private ViewGroup mContent;
  15.     private int mScreenWidth;//屏幕的寬度
  16.     private int mMenuWidth;//設(shè)置Menu的寬度
  17.    
  18.     //dp
  19.     private int mMenuRightPadding;
  20.     private boolean once = false;
  21.    
  22.     /**
  23.      * 未使用自定義屬性時(shí)調(diào)用此方法
  24.      * @param context
  25.      * @param attrs
  26.      */
  27.     public SlidingMenu(Context context, AttributeSet attrs) {
  28.         super(context, attrs);
  29.         WindowManager wm = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
  30.         DisplayMetrics outMetrics = new DisplayMetrics();
  31.         wm.getDefaultDisplay().getMetrics(outMetrics );
  32.         mScreenWidth = outMetrics.widthPixels;
  33.         
  34.         //將dp轉(zhuǎn)換為px
  35.         mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
  36.         
  37.     }
  38.    
  39.     /**
  40.      * 設(shè)置內(nèi)部View的寬和高,以及自己的寬和高
  41.      */
  42.     @Override
  43.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  44.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  45.         
  46.         if(!once){
  47.             mWapper = (LinearLayout) getChildAt(0);
  48.             mMenu = (ViewGroup) mWapper.getChildAt(0);
  49.             mContent = (ViewGroup) mWapper.getChildAt(1);
  50.             mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
  51.             mContent.getLayoutParams().width = mScreenWidth;
  52.             once = true;
  53.         }
  54.         
  55.     }

  56.     /**
  57.      * 設(shè)置子View的放置位置
  58.      * 通過設(shè)置偏移量來隱藏Menu
  59.      */
  60.     @Override
  61.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  62.         super.onLayout(changed, l, t, r, b);
  63.         if(changed){
  64.             this.scrollTo(mMenuWidth, 0);
  65.         }
  66.     }
  67.    
  68.     /**
  69.      * 控制手指的滑動(dòng)效果
  70.      */
  71.     @Override
  72.     public boolean onTouchEvent(MotionEvent ev) {
  73.         int action = ev.getAction();
  74.         switch (action) {
  75.         case MotionEvent.ACTION_UP:
  76.             int scrollx = getScrollX();//Menu左側(cè)隱藏的區(qū)域?qū)挾?br />
  77.             if(scrollx >= mMenuWidth/2){
  78.                 this.smoothScrollTo(mMenuWidth, 0);
  79.             }else{
  80.                 this.smoothScrollTo(0, 0);
  81.             }
  82.             return true;
  83.         }
  84.         return super.onTouchEvent(ev);
  85.     }
  86.    
  87. }
復(fù)制代碼
我們的主Activity:
  1. public class MainActivity extends Activity {

  2.     @Override
  3.     protected void onCreate(Bundle savedInstanceState) {
  4.         super.onCreate(savedInstanceState);
  5.         requestWindowFeature(Window.FEATURE_NO_TITLE);//去除標(biāo)題
  6.         setContentView(R.layout.activity_main);
  7.         
  8.     }
  9. }
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP