免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1515 | 回复: 0
打印 上一主题 下一主题

[Android] QQ左侧滑动显示 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-08-21 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-06-01 10:11 |只看该作者 |倒序浏览
对于新版的QQ界面,左侧增加了一个滑动效果,相信小伙伴们早已按耐不住激动的心情,这种效果是如何实现的呢?本篇我们就一起来探讨一二。既然是滑动效果这里就要使用到HorizontalScrollView类,一个水平滑动的效果。

  对于这个效果我们可以分为一个Item Menu和Layout,通过对Item Menu设置padding值来隐藏和显示左侧的Menu菜单。

  我们的Menu设计代码:
  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="第一个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="第二个item"
  54.                 android:layout_toRightOf="@id/img2"
  55.                 android:layout_centerVertical="true"
  56.                 />
  57.        </RelativeLayout>
  58.         
  59.     </LinearLayout>

  60. </RelativeLayout>
复制代码
我们的主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>
复制代码
我们自定义的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;//设置Menu的宽度
  17.    
  18.     //dp
  19.     private int mMenuRightPadding;
  20.     private boolean once = false;
  21.    
  22.     /**
  23.      * 未使用自定义属性时调用此方法
  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转换为px
  35.         mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
  36.         
  37.     }
  38.    
  39.     /**
  40.      * 设置内部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.      * 设置子View的放置位置
  58.      * 通过设置偏移量来隐藏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.      * 控制手指的滑动效果
  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左侧隐藏的区域宽度
  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. }
复制代码
我们的主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);//去除标题
  6.         setContentView(R.layout.activity_main);
  7.         
  8.     }
  9. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP