<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android仿新版酷狗滑動(dòng)側(cè)滑欄效果

          共 13156字,需瀏覽 27分鐘

           ·

          2021-05-09 10:08

          之前寫了模仿酷狗音樂(舊版本)的主頁面?zhèn)然Ч裉靵韺懣峁芬魳沸掳姹镜闹黜撁鎮(zhèn)然Ч@個(gè)效果跟舊版的很類似,但有區(qū)別,主要處理以下要點(diǎn):

          1、緩慢滑動(dòng)手指離開屏幕時(shí)判斷左右滑;

          2、快速滑動(dòng)時(shí)判斷屏幕左右滑;

          3、菜單頁打開時(shí),點(diǎn)擊內(nèi)容頁關(guān)閉菜單并且攔截觸摸事件;

          4、打開菜單頁時(shí),內(nèi)容頁增加一層陰影;

          我們先來看看效果圖:



          如上圖就是我們實(shí)現(xiàn)的效果,在實(shí)現(xiàn)之前,我們先搞懂菜單頁和內(nèi)容頁的位置關(guān)系,我畫了一個(gè)圖,如下:


          圖2中,黑色是手機(jī)屏幕,黃色是菜單頁,綠色是內(nèi)容頁,getScrollX就是我們待會(huì)需要求的一個(gè)距離,照例,下面貼出完整的代碼(配上詳細(xì)注釋)


          自定義KGNewSlidingMenu控件:

          public class KGNewSlidingMenu extends HorizontalScrollView {    private View mMenuView, mContentView, mShadow;    private int mMenuWidth;    private Context mContext;    private GestureDetector mDetector;//系統(tǒng)自帶手勢處理類    private boolean mMenuIsOpen = false;//菜單頁是否已經(jīng)打開    private boolean mIntercept = false;//是否攔截事件    private float startX, startY, offsetX, offsetY;
          public KGNewSlidingMenu(Context context) { this(context, null); }
          public KGNewSlidingMenu(Context context, AttributeSet attrs) { this(context, attrs, 0); }
          public KGNewSlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.mContext = context;
          TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.KGSlidingMenu); float rightMargin = array.getDimension(R.styleable.KGSlidingMenu_rightMargin, dip2px(50)); mMenuWidth = (int) (getScreenWidth(context) - rightMargin); array.recycle();
          //用于處理快速滑動(dòng)時(shí),進(jìn)行打開或者關(guān)閉菜單頁 mDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { //快速滑動(dòng)時(shí)就會(huì)回調(diào),打開菜單頁時(shí)往右滑動(dòng),關(guān)閉的時(shí)候往左快速滑動(dòng) //快速往左滑velocityX是負(fù)數(shù),快速往右滑velocityX是正數(shù) if (mMenuIsOpen) { if (Math.abs(velocityX) > Math.abs(velocityY)) {//橫向滑動(dòng)大于縱向滑動(dòng)再響應(yīng),增加用戶體驗(yàn) if (velocityX < 0) { close(); return true; } } } else { if (Math.abs(velocityX) > Math.abs(velocityY)) {//橫向滑動(dòng)大于縱向滑動(dòng)再響應(yīng),增加用戶體驗(yàn) if (velocityX > 0) { open(); return true; } } } return super.onFling(e1, e2, velocityX, velocityY); } }); }
          @Override protected void onFinishInflate() { //xml布局文件解析完畢后調(diào)用 super.onFinishInflate(); //1、內(nèi)容頁指定寬高,就是屏幕的寬度 //2、菜單頁指定寬度,就是屏幕寬度減去 - 自定義寬度 ViewGroup container = (ViewGroup) getChildAt(0);//外層LinearLayout if (container.getChildCount() != 2) { throw new RuntimeException("只能且必須放置兩個(gè)子View!"); }
          mMenuView = container.getChildAt(0);//菜單頁 //設(shè)置寬高 ViewGroup.LayoutParams menuParams = mMenuView.getLayoutParams(); menuParams.width = mMenuWidth; mMenuView.setLayoutParams(menuParams);//7.0以下的手機(jī)必須加這句

          //給內(nèi)容頁添加一層陰影:先從父布局取出內(nèi)容頁,然后放進(jìn)另一個(gè)容器, // 同時(shí)在該容器添加一層陰影(用View設(shè)置背景顏色即可),然后再把該容器放回父布局。 mContentView = container.getChildAt(1);//內(nèi)容頁 //設(shè)置寬高 container.removeView(mContentView);//從父布局取出內(nèi)容頁 RelativeLayout outContainer = new RelativeLayout(mContext);//創(chuàng)建新容器 outContainer.addView(mContentView);//把內(nèi)容頁加入新容器 mShadow = new View(mContext);//創(chuàng)建陰影層 mShadow.setBackgroundColor(Color.parseColor("#70000000"));//陰影層設(shè)置背景顏色 outContainer.addView(mShadow);//在新容器加入陰影層 ViewGroup.LayoutParams contentParams = mContentView.getLayoutParams(); contentParams.width = getScreenWidth(mContext); outContainer.setLayoutParams(contentParams);//7.0以下的手機(jī)必須加這句 container.addView(outContainer);//把新容器加入父布局 mShadow.setAlpha(0.0f);//初始化陰影層為不透明
          }
          @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN){ //記錄按下屏幕的初始位置 startX = ev.getX(); startY = ev.getY(); } mIntercept = false; //處理當(dāng)菜單頁打開的時(shí)候,觸摸內(nèi)容頁部門時(shí)關(guān)閉菜單頁,并且攔截事件 if (mMenuIsOpen) { float currentX = ev.getX(); if (currentX > mMenuWidth) { close(); //返回true :攔截子View的事件,但是會(huì)執(zhí)行自己的onTouchEvent方法 mIntercept = true; return true; } } return super.onInterceptTouchEvent(ev); }
          @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); //要在onLayout執(zhí)行后再調(diào)用scrollTo,否則無效果 //初始化的時(shí)候是關(guān)閉的,注意,此時(shí)的getScrollX = mMenuWidth close(); }
          @Override public boolean onTouchEvent(MotionEvent ev) { if (mIntercept) {//如果攔截子View的事件,同時(shí)不執(zhí)行自己的onTouchEvent return true; } if (mDetector.onTouchEvent(ev)) {//假如快速滑動(dòng)執(zhí)行了,以下代碼就不執(zhí)行 return true; }
          switch (ev.getAction()) { case MotionEvent.ACTION_MOVE: offsetX = ev.getX() - startX; offsetY = ev.getY() - startY; //與按下屏幕的初始位置相比,當(dāng)縱向滑動(dòng)大于橫向滑動(dòng)時(shí)攔截滑動(dòng)事件(橫向滑動(dòng)為主時(shí)才進(jìn)行滑動(dòng),增加用戶體驗(yàn)) if (Math.abs(offsetX) < Math.abs(offsetY)) { return false; } break; case MotionEvent.ACTION_UP: //根據(jù)手指抬起時(shí)的滾動(dòng)距離判斷,要么關(guān)閉,要么打開 int currentScrollX = getScrollX(); if (currentScrollX > mMenuWidth / 2) { //關(guān)閉菜單 close(); } else { //打開菜單 open(); } return true; }
          return super.onTouchEvent(ev); }
          //關(guān)閉菜單 private void close() { mMenuIsOpen = false; smoothScrollTo(mMenuWidth, 0); }
          //打開菜單 private void open() { mMenuIsOpen = true; smoothScrollTo(0, 0); }
          //處理左右滑時(shí)的縮放 @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); float scale = 1f * l / mMenuWidth;//scale從 1 - 0 //滑動(dòng)時(shí)改變內(nèi)容頁的陰影程度,最大1f,最小0f mShadow.setAlpha(1 - scale); //滑動(dòng)時(shí)菜單頁平移,抽屜效果 ViewCompat.setTranslationX(mMenuView, l * 0.55f); }
          //獲取屏幕寬度 private static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics displayMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.widthPixels; }
          private int dip2px(float dp) { final float scale = mContext.getResources().getDisplayMetrics().density; return (int) (dp * scale + 0.5f); }
          }


          activity_newkg.xml

          <util.KGNewSlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/slidingmenu"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff"    app:rightMargin="62dp">
          <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">
          <include layout="@layout/layout_home_menu2"/>
          <include layout="@layout/layout_home_content"/> </LinearLayout>
          </util.KGNewSlidingMenu>


          layout_home_menu2.xml

          <RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:background="#fff"    android:paddingTop="30dp"    android:padding="10dp">
          <LinearLayout android:id="@+id/layout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:orientation="horizontal">
          <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/default_avatar"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22dp" android:textColor="#000" android:padding="10dp" android:layout_gravity="center_vertical" android:text="張三"/>
          </LinearLayout>
          <LinearLayout android:layout_below="@+id/layout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="20dp" android:orientation="vertical">
          <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:padding="5dp" android:text="皮膚中心"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20dp" android:textColor="#000" android:padding="5dp" android:text="消息中心"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:padding="5dp" android:text="會(huì)員中心"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:padding="5dp" android:text="定時(shí)關(guān)閉"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:padding="5dp" android:text="聽歌識(shí)曲"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:padding="5dp" android:text="彩鈴模式"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:padding="5dp" android:text="私人云盤"/> </LinearLayout>
          <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:gravity="center" android:padding="5dp" android:text="設(shè)置"/> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:gravity="center" android:padding="5dp" android:text="登錄"/> <TextView android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#000" android:gravity="center" android:padding="5dp" android:text="關(guān)閉"/> </LinearLayout>
          </RelativeLayout>


          layout_home_content.xml

          <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"android:background="@color/colorPrimary"android:orientation="vertical"android:paddingTop="30dp"android:layout_height="match_parent"><android.support.v7.widget.Toolbar    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:background="@color/colorPrimary"    >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="內(nèi)容頁"        android:textColor="#fff"        android:textSize="20sp"/></android.support.v7.widget.Toolbar><TextView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:textSize="25sp"    android:text="我是內(nèi)容"    android:background="#fff"    android:textColor="@color/colorPrimary"    android:gravity="center"/>
          </LinearLayout>


          attrs.xml

          <resources> <declare-styleable name="SlidingMenu">        <attr name="rightMargin" format="dimension"/>    </declare-styleable></resources>


          NEWKGActivity.java

          public class NEWKGActivity extends AppCompatActivity {
          @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_newkg);    }  }


          以上為完整代碼,可以直接使用。


          到這里就結(jié)束啦。

          瀏覽 137
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天堂极品mv | 亚洲专区欧美专区 | 色小姐噜噜久久 | 欧美一区二区三区18 | 精品A片九九九九免费视频 |