麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 系統(tǒng) > Android > 正文

Android自定義實(shí)現(xiàn)側(cè)滑菜單效果

2019-10-21 21:26:16
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android自定義實(shí)現(xiàn)側(cè)滑菜單的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)原理:繼承ViewGroup控件要顯示到界面上需要重寫OnMeature() 
OnLayout(),因此在實(shí)現(xiàn)OnLayout()的時候,將菜單界面劃出到屏幕左側(cè),動態(tài)改變菜單界面距離scrollXto()左邊界的距離就能實(shí)現(xiàn)滑動效果。

1.繼承ViewGroup
2.事件分發(fā)機(jī)制
3.狀態(tài)監(jiān)聽

在主界面中添加兩個子控件

<com.oblivion.ui.SlideMenu xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/sliding_menu"  android:layout_width="match_parent"  android:layout_height="match_parent">  <!-- 在SlidingMenu中的索引0 -->  <include layout="@layout/menu" />  <!-- 在SlidingMenu中的索引1 -->  <include layout="@layout/main" /></com.oblivion.ui.SlideMenu>

menu菜單布局

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="240dp"  android:layout_height="match_parent"  android:background="@drawable/menu_bg">  <LinearLayout    android:layout_width="240dp"    android:layout_height="wrap_content"    android:orientation="vertical">    <TextView      android:id="@+id/tv_news"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:background="@drawable/select_menu"      android:clickable="true"      android:drawableLeft="@drawable/tab_news"      android:drawablePadding="10dp"      android:text="新聞" />    <TextView      android:id="@+id/tv_read"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_read"      android:drawablePadding="10dp"      android:text="訂閱" />    <TextView      android:id="@+id/tv_local"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_local"      android:drawablePadding="10dp"      android:text="本地" />    <TextView      android:id="@+id/tv_ties"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_ties"      android:drawablePadding="10dp"      android:text="跟帖" />    <TextView      android:id="@+id/tv_pics"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_pics"      android:drawablePadding="10dp"      android:text="圖片" />    <TextView      android:id="@+id/tv_ugc"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_ugc"      android:drawablePadding="10dp"      android:text="話題" />    <TextView      android:id="@+id/tv_vote"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_vote"      android:drawablePadding="10dp"      android:text="投票" />    <TextView      android:id="@+id/tv_focus"      style="@style/menu_item"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:drawableLeft="@drawable/tab_focus"      android:drawablePadding="10dp"      android:text="焦點(diǎn)" />  </LinearLayout></ScrollView>

顯示界面布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">  <LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@drawable/top_bar_bg"    android:orientation="horizontal">    <ImageView      android:id="@+id/iv_showmenu"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:src="@drawable/main_back" />    <View      android:layout_width="1dp"      android:layout_height="match_parent"      android:background="@drawable/top_bar_divider"></View>    <TextView      android:layout_width="match_parent"      android:layout_height="match_parent"      android:gravity="center"      android:text="新聞首頁"      android:textColor="#ffffff"      android:textSize="30sp" />  </LinearLayout>  <TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:text="我操,這么low"    android:textColor="#000000"    android:textSize="20sp" /></LinearLayout>

實(shí)現(xiàn)原理:繼承ViewGroup控件要顯示到界面上需要重寫OnMeature() OnLayout(),因此在實(shí)現(xiàn)OnLayout()的時候,將菜單界面劃出到屏幕左側(cè),動態(tài)改變菜單界面距離scrollXto()左邊界的距離就能實(shí)現(xiàn)滑動效果。

實(shí)現(xiàn)onMeasure()和onLayout()方法,對控件進(jìn)行布局

/**   * 測量布局中的控件   *   * @param widthMeasureSpec   * @param heightMeasureSpec   */  @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    super.onMeasure(widthMeasureSpec, heightMeasureSpec);//測量自身寬高    //得到menu界面    menuView = getChildAt(0);    menuViewWidth = menuView.getLayoutParams().width;    //得到main界面    mainView = getChildAt(1);    //設(shè)定menuView的寬------不用setMeasure...    menuView.measure(MeasureSpec.makeMeasureSpec(menuViewWidth, MeasureSpec.EXACTLY), heightMeasureSpec);    //設(shè)定mainView的寬    mainView.measure(widthMeasureSpec, heightMeasureSpec);  }  @Override  protected void onLayout(boolean changed, int l, int t, int r, int b) {    int menuleft = -menuViewWidth;//menu設(shè)定的left    int menuright = 0;//menu設(shè)定的right    int menutop = 0;//menu設(shè)定的top    int menubottom = b - t;//menu設(shè)定的bottom    //布局menuView 控件xx2    menuView.layout(menuleft, menutop, menuright, menubottom);    int mainleft = 0;//main設(shè)定的left    int mainright = r - l;//main設(shè)定的right    int maintop = 0;//main設(shè)定的top    int mainbottom = b - t;//main設(shè)定的bottom    //布局mainView ---------------控件.....你媽逼方向---------方向是left,top,right,bottom    mainView.layout(mainleft, maintop, mainright, mainbottom);    System.out.println(menuViewWidth--);//原來這這里減減了;  }

通過onTouch()方法,以及scrollX()和getScrollX()方法獲取邊界值,動態(tài)改變?nèi)?shí)現(xiàn)滑動。 
這里需要注意的是scrollX(),getScrollX()得到的值是相對于布局起始點(diǎn)的,所以需要重新封裝; 
平滑動畫,需要在構(gòu)造函數(shù)中創(chuàng)建一個Scroll 類,然后通過ComplentScroll..方法,判斷設(shè)定的平滑動畫是否結(jié)束。

最后通過,動畫結(jié)束后的邊界值,判斷是否打開,關(guān)閉狀態(tài)。

 

 /**   * 由于系統(tǒng)scrollTo是取反操作,所以需要封裝一下   *   * @param distance   */  private void scrollTo(int distance) {    super.scrollTo(-distance, 0);  }  public int getMyScrollX() {    return -getScrollX();  }  /**   * 控件觸莫狀態(tài)   *   * @param event   * @return   */  @Override  public boolean onTouchEvent(MotionEvent event) {    switch (event.getAction()) {      case MotionEvent.ACTION_DOWN:        //記錄按下點(diǎn)        down_dot = (int) event.getX();        System.out.println(down_dot);        break;      case MotionEvent.ACTION_MOVE:        int move_dot = (int) event.getX();        //動態(tài)的移動點(diǎn)和按下點(diǎn)的間距絕對值        move2down_distance = move_dot - down_dot;        //移動間距與上次抬起的點(diǎn)        int lastUp_dot = move2down_distance + up_dot;        // 控制邊界        if (lastUp_dot >= menuViewWidth) {          lastUp_dot = menuViewWidth;        } else if (lastUp_dot <= 0) {          lastUp_dot = 0;        }        //設(shè)定移動后的距離        scrollTo(lastUp_dot);        break;      case MotionEvent.ACTION_UP:        //當(dāng)前抬起手指點(diǎn)        up_dot = (int) event.getX();        //設(shè)定最終的狀態(tài)        setFinalScroll();        break;    }    return true;//將事件消費(fèi)掉  }  /**   * 當(dāng)手指抬起后,記錄最終的狀態(tài);   */  private void setFinalScroll() {    //得到當(dāng)前距離左邊界的距離    currrentScroll = getMyScrollX();    if (currrentScroll >= menuViewWidth / 2) {      //scrollTo(menuViewWidth);      rightAnimation();    } else {      leftAnimation();    }  }  /**   * 平滑向左的移動動畫   */  private void leftAnimation() {    //scrollTo(0);    int dx = 0 - currrentScroll;//要移動的距離    //設(shè)定平滑動畫    msScroller.startScroll(currrentScroll, 0, dx, 0, 300);    invalidate();    //設(shè)定一下抬起點(diǎn)    up_dot = 0;    //調(diào)用接口的關(guān)閉狀態(tài)    mOnDragStateListener.onDragClose();  }  /**   * 平滑向右移動的動畫   */  private void rightAnimation() {    int dx = menuViewWidth - currrentScroll;//要移動的距離    //設(shè)定平滑動畫    msScroller.startScroll(currrentScroll, 0, dx, 0, 300);    invalidate();    //設(shè)定一下抬起點(diǎn)    up_dot = menuViewWidth;    //調(diào)用接口的開啟狀態(tài)    mOnDragStateListener.onDragOpen();  }  /**   * 平滑移動動畫是否結(jié)束   */  @Override  public void computeScroll() {    super.computeScroll();    if (msScroller.computeScrollOffset()) {      int currX = msScroller.getCurrX();//模擬出來的數(shù)值      scrollTo(currX);      invalidate();    }  }

創(chuàng)建監(jiān)聽,以及回調(diào)接口

 /**   * 拖拽的監(jiān)聽   */  public void setOnDragStateListener(onDragStateListener listener) {    mOnDragStateListener = listener;  }  /**   * 回調(diào)修改狀態(tài)   *   * @param dragState   */  public void setStateChange(boolean dragState) {    if (dragState) {      currrentScroll = 0;      rightAnimation();    } else {      currrentScroll = menuViewWidth;      leftAnimation();    }  }  /**   * 創(chuàng)建拖拽的回調(diào)接口   */  public interface onDragStateListener {    /**     * 被拖拽開     */    void onDragOpen();    /**     * 被關(guān)閉     */    void onDragClose();  }

主Activity中實(shí)現(xiàn)監(jiān)聽效果

iv_showmenu.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        //圖片點(diǎn)擊后觸發(fā)改變狀態(tài)        sliding_menu.setStateChange(dragState);      }    });    sliding_menu.setOnDragStateListener(new SlideMenu.onDragStateListener() {      @Override      public void onDragOpen() {        ToastUtils.setToast(getApplicationContext(), "open");        dragState = false;        tv_news.setSelected(true);        tv_news.setTextColor(Color.BLUE);      }      @Override      public void onDragClose() {        ToastUtils.setToast(getApplicationContext(), "close");        dragState = true;        tv_news.setSelected(false);        tv_news.setTextColor(Color.WHITE);      }    });    tv_news.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        ToastUtils.setToast(getApplicationContext(), tv_news.getText().toString());      }    });  }

github源碼地址

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费一级在线视频 | 午夜激情视频网站 | 免费亚洲视频在线观看 | www日韩大片 | 亚洲第一成网站 | 国产精品99久久久久久宅女 | 国产在线精品一区二区夜色 | 国产亚洲精品久久久久5区 男人天堂免费 | 看国产一级毛片 | 久久国产在线观看 | 欧美一级黄色免费 | 日韩视频在线不卡 | 免费a视频在线观看 | 一区二区三区黄色 | asian gaysex| 蜜桃网站在线观看 | 亚洲精品在线观看免费 | 国产毛片自拍 | 国产精选久久久 | 国产91精品久久久久久久 | 性欧美极品xxxx欧美一区二区 | 欧美三级毛片 | 99视频有精品视频高清 | 毛片在线视频观看 | 欧美福利视频一区二区三区 | 国产激情精品一区二区三区 | 精品国产一区二 | 黄色羞羞视频在线观看 | 亚洲午夜在线观看 | 又黄又爽又色无遮挡免费 | 一级做受大片免费视频 | 最新黄色电影网站 | 成人免费一区二区三区在线观看 | 国产精品久久久久久久av | 色97色 | 看免费的毛片 | 日韩在线欧美在线 | 特逼视频 | 欧美成人综合视频 | 久久久久九九九女人毛片 | 国产午夜亚洲精品 |