最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android實(shí)現(xiàn)3種側(cè)滑效果(仿qq側(cè)滑、抽屜側(cè)滑、普通側(cè)滑)

 更新時(shí)間:2017年04月19日 14:49:02   作者:gywuhengy  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多種側(cè)滑效果,包括仿qq側(cè)滑,抽屜側(cè)滑,普通側(cè)滑三種效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

自己實(shí)現(xiàn)了一下側(cè)滑的三種方式(注釋都寫(xiě)代碼里了)

本文Demo下載地址:Andriod側(cè)滑

本文實(shí)現(xiàn)所需框架:nineoldandroids下載地址:nineoldandroids

1.普通側(cè)滑:

主要是基于HorizontalScrollView做的:示例代碼如下

主要布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 xmlns:gaoyu="http://schemas.android.com/apk/res/gaoyu.com.myapplication"

 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_qqsideslip"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="gaoyu.com.myapplication.sideslip.QQSideslipActivity">

 <!--xmlns:gaoyu自定義命名空間 原有到res+包名-->

 <gaoyu.com.myapplication.sideslip.SlidingMenu_qq
 android:id="@+id/SlMenu_sideslip"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 gaoyu:rightPadding="100dp">

 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:orientation="horizontal">

 <include layout="@layout/sideslip_menu" />
 <!--這個(gè)LinearLayout就是content-->
 <LinearLayout

 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@drawable/sliding">

 <Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onClick="onClick_sideslip_qq"
 android:text="切換菜單" />
 </LinearLayout>

 </LinearLayout>
 </gaoyu.com.myapplication.sideslip.SlidingMenu_qq>


</RelativeLayout>

菜單的布局

<?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:orientation="vertical">

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_centerInParent="true">
 <RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <ImageView
 android:id="@+id/iv_sideslip1"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="20dp"
 android:layout_marginBottom="20dp"
 android:layout_marginLeft="20dp"
 android:src="@drawable/icon"/>
 <TextView
 android:layout_centerVertical="true"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/iv_sideslip1"
 android:layout_marginLeft="20dp"
 android:text="第一個(gè)item"/>
 </RelativeLayout>
 <RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <ImageView
 android:id="@+id/iv_sideslip2"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="20dp"
 android:layout_marginBottom="20dp"
 android:layout_marginLeft="20dp"
 android:src="@drawable/icon"/>
 <TextView
 android:layout_centerVertical="true"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/iv_sideslip2"
 android:layout_marginLeft="20dp"
 android:text="第二個(gè)item"/>
 </RelativeLayout><RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <ImageView
 android:id="@+id/iv_sideslip3"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="20dp"
 android:layout_marginBottom="20dp"
 android:layout_marginLeft="20dp"
 android:src="@drawable/icon"/>
 <TextView
 android:layout_centerVertical="true"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/iv_sideslip3"
 android:layout_marginLeft="20dp"
 android:text="第三個(gè)item"/>
 </RelativeLayout><RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <ImageView
 android:id="@+id/iv_sideslip4"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="20dp"
 android:layout_marginBottom="20dp"
 android:layout_marginLeft="20dp"
 android:src="@drawable/icon"/>
 <TextView
 android:layout_centerVertical="true"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/iv_sideslip4"
 android:layout_marginLeft="20dp"
 android:text="第四個(gè)item"/>
 </RelativeLayout><RelativeLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content">
 <ImageView
 android:id="@+id/iv_sideslip5"
 android:layout_width="50dp"
 android:layout_height="50dp"
 android:layout_marginTop="20dp"
 android:layout_marginBottom="20dp"
 android:layout_marginLeft="20dp"
 android:src="@drawable/icon"/>
 <TextView
 android:layout_centerVertical="true"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/iv_sideslip5"
 android:layout_marginLeft="20dp"
 android:text="第五個(gè)item"/>
 </RelativeLayout>
 </LinearLayout>

</RelativeLayout>

定義view類

public class SlidingMenu_qq extends HorizontalScrollView {

 private LinearLayout mWapper;
 private ViewGroup mMenu;
 private ViewGroup mContent;
 //menu的寬度
 private int mMenuWidth;
 //屏幕的寬度(內(nèi)容區(qū)的寬度就是屏幕寬度)
 private int mScreenWdith;
 //菜單與右邊的距離50dp
 private int mMenuRightPidding = 50;
 //調(diào)用一次
 private boolean once;
 //標(biāo)識(shí)狀態(tài)
 private boolean isOPen;

 /**
 * 未使用自定義屬性時(shí)調(diào)用
 * 由于設(shè)置了attr所以...
 *
 * @param context
 * @param attrs
 */
 public SlidingMenu_qq(Context context, AttributeSet attrs) {
 //調(diào)用三個(gè)參數(shù)的構(gòu)造方法
 this(context, attrs, 0);
 //獲取屏幕寬度(窗口管理器)
 /*WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
 //展示度量
 DisplayMetrics outMetrics = new DisplayMetrics();
 wm.getDefaultDisplay().getMetrics(outMetrics);
 mScreenWdith = outMetrics.widthPixels;
 //把dp轉(zhuǎn)換成px
 mMenuRightPidding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
*/
 }

 /**
 * 當(dāng)實(shí)現(xiàn)自定義屬性時(shí)會(huì)執(zhí)行三個(gè)參數(shù)的方法
 *
 * @param context
 * @param attrs
 * @param defStyleAttr
 */
 public SlidingMenu_qq(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);

 //獲取自定義的屬性

 TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu_qq, defStyleAttr, 0);
 //自定義屬性個(gè)數(shù)
 int n = a.getIndexCount();
 for (int i = 0; i < n; i++) {
 int attr = a.getIndex(i);
 switch (attr) {
 case R.styleable.SlidingMenu_qq_rightPadding:
 //設(shè)置默認(rèn)值是50dp
 mMenuRightPidding = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics()));

 break;
 }

 }
 //釋放一下
 a.recycle();

 //獲取屏幕寬度(窗口管理器)
 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
 //展示度量
 DisplayMetrics outMetrics = new DisplayMetrics();
 wm.getDefaultDisplay().getMetrics(outMetrics);
 mScreenWdith = outMetrics.widthPixels;

 }

 /**
 * new 一個(gè)TextView時(shí)傳一個(gè)上下文
 *
 * @param context
 */
 public SlidingMenu_qq(Context context) {
 //調(diào)用兩個(gè)參數(shù)的構(gòu)造方法
 super(context, null);
 }

 /**
 * 設(shè)置HorizontalScrollView子VIew的寬和高
 * 設(shè)置HorizontalScrollView自己的寬和高
 *
 * @param widthMeasureSpec
 * @param heightMeasureSpec
 */
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 //設(shè)置循環(huán)之調(diào)用一次
 if (!once) {
 //HorizontalScrollView 內(nèi)部只能有一個(gè)元素 所以直接get(0)就行就是那個(gè)Linerlayout mWapper
 mWapper = (LinearLayout) getChildAt(0);
 //獲取mWapper里的第一個(gè)元素menu
 mMenu = (ViewGroup) mWapper.getChildAt(0);
 //獲取mWapper里的第二個(gè)元素content
 mContent = (ViewGroup) mWapper.getChildAt(1);
 //菜單和內(nèi)容寬度
 mMenuWidth = mMenu.getLayoutParams().width = mScreenWdith - mMenuRightPidding;
 mContent.getLayoutParams().width = mScreenWdith;
 //由于子對(duì)象被設(shè)置了,mWapper就先不用了
 once = true;
 }
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 }

 /**
 * 通過(guò)設(shè)置偏移量 將menu隱藏
 * @param changed
 * @param l
 * @param t
 * @param r
 * @param b
 */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
 super.onLayout(changed, l, t, r, b);
 //限制多次調(diào)用
 if (changed) {
 //x為正滾動(dòng)條向右 內(nèi)容向左(移動(dòng)mMenuWidth 正好將菜單隱藏)
 this.scrollTo(mMenuWidth, 0);
 }
 }

 /**
 * 判斷將菜單滑出來(lái)多少了
 *
 * @param ev
 * @return
 */
 @Override
 public boolean onTouchEvent(MotionEvent ev) {
 int action = ev.getAction();
 switch (action) {
 case MotionEvent.ACTION_UP:
 //隱藏在左邊的寬度
 int scrollX = getScrollX();
 if (scrollX >= mMenuWidth / 2) {
 //scrollTo也行但是動(dòng)畫(huà)效果不好 (隱藏)
 this.smoothScrollTo(mMenuWidth, 0);
 //代表菜單隱藏
 isOPen = false;
 } else {
 this.smoothScrollTo(0, 0);
 //表菜單打開(kāi)
 isOPen = true;
 }
 return true;
 }
 return super.onTouchEvent(ev);
 }

 /**
 * 打開(kāi)菜單
 */
 public void openMenu() {
 //已經(jīng)打開(kāi)
 if (isOPen) return;
 this.smoothScrollTo(0, 0);
 isOPen = true;
 }

 /**
 * 關(guān)閉菜單
 */
 public void closeMenu() {
 //正在打開(kāi)
 if (!isOPen) return;
 this.smoothScrollTo(mMenuWidth, 0);
 isOPen = false;
 }

 /**
 * 切換菜單
 */
 public void toggle(){
 if (isOPen){
 closeMenu();
 }else {
 openMenu();
 }
 }
}

2.抽屜側(cè)滑(添加此方法)

/**
 * 實(shí)現(xiàn)抽屜滑動(dòng)
 * l隱藏在左邊的寬度
 * 后邊是變化梯度
 */

 @Override
 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
 super.onScrollChanged(l, t, oldl, oldt);
 float scale = l*1.0f/mMenuWidth;//1~0梯度的值
 //調(diào)用屬性動(dòng)畫(huà)
 ViewHelper.setTranslationX(mMenu,mMenuWidth*scale);
 }


3.qq5.0側(cè)滑,實(shí)現(xiàn)這個(gè)方法

 /**
 * 實(shí)現(xiàn)仿qq5.0
 * l等于隱藏在左邊的寬度(越來(lái)越?。?
 * 后邊是變化梯度
 */

 @Override
 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
 super.onScrollChanged(l, t, oldl, oldt);
 float scale = l * 1.0f / mMenuWidth;//1~0梯度的值

 //調(diào)用屬性動(dòng)畫(huà)

 //菜單的縮放操作
 float leftScale = 1.0f-scale*0.3f;
 //透明度
 float leftAlpha = 0.6f + 0.4f*(1-scale);
 ViewHelper.setTranslationX(mMenu, mMenuWidth * scale*0.8f);
 ViewHelper.setScaleX(mMenu,leftScale);
 ViewHelper.setScaleY(mMenu,leftScale);
 ViewHelper.setAlpha(mMenu,leftAlpha);

 //內(nèi)容區(qū)域不斷縮小
 float rightScale = 0.7f+0.3f*scale;
 //橫向縱向縮放(不更改縮放中心點(diǎn)就全隱藏了)
 ViewHelper.setPivotX(mContent,0);
 ViewHelper.setPivotY(mContent,mContent.getHeight()/2);
 ViewHelper.setScaleX(mContent,rightScale);
 ViewHelper.setScaleY(mContent,rightScale);

 }

更多學(xué)習(xí)內(nèi)容,可以點(diǎn)擊《Android側(cè)滑效果匯總》學(xué)習(xí)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實(shí)現(xiàn)RecyclerView添加分割線的簡(jiǎn)便方法

    Android實(shí)現(xiàn)RecyclerView添加分割線的簡(jiǎn)便方法

    這篇文章主要介紹了Android實(shí)現(xiàn)RecyclerView添加分割線的簡(jiǎn)便方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • 解決Android中自定義DialogFragment解決寬度和高度問(wèn)題

    解決Android中自定義DialogFragment解決寬度和高度問(wèn)題

    Android中自定義DialogFragment解決寬度和高度問(wèn)題但是我們很多時(shí)候想把DialogFragment的高度固定,那么我們需要設(shè)置DialogFragment的高度,在Fragment的onResume()聲明周期方法中設(shè)置window的寬高即可
    2017-12-12
  • Android UI設(shè)計(jì)與開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)歡迎引導(dǎo)頁(yè)面

    Android UI設(shè)計(jì)與開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)歡迎引導(dǎo)頁(yè)面

    這篇文章主要為大家詳細(xì)介紹了Android UI設(shè)計(jì)與開(kāi)發(fā)之使用ViewPager實(shí)現(xiàn)歡迎引導(dǎo)頁(yè)面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • 詳解Android TableLayout表格布局

    詳解Android TableLayout表格布局

    表格布局的標(biāo)簽是TableLayout,TableLayout繼承了LinearLayout。所以它依然是一個(gè)線性布局,通過(guò)本文給大家介紹Android TableLayout表格布局,感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • Android 使用Fragment模仿微信界面的實(shí)例代碼

    Android 使用Fragment模仿微信界面的實(shí)例代碼

    自從Android 3.0中引入fragments 的概念,根據(jù)詞海的翻譯可以譯為:碎片、片段。其目的是為了解決不同屏幕分辯率的動(dòng)態(tài)和靈活UI設(shè)計(jì)。下面通過(guò)本文給大家分享Android 使用Fragment模仿微信界面的實(shí)例代碼,需要的的朋友參考下吧
    2017-07-07
  • Android中Handler消息傳遞機(jī)制

    Android中Handler消息傳遞機(jī)制

    這篇文章主要介紹了Android中Handler消息傳遞機(jī)制 的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 詳解Android廣播Broadcast的啟動(dòng)流程

    詳解Android廣播Broadcast的啟動(dòng)流程

    這篇文章主要為大家介紹了Android廣播Broadcast啟動(dòng)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Android音頻開(kāi)發(fā)之音頻采集的實(shí)現(xiàn)示例

    Android音頻開(kāi)發(fā)之音頻采集的實(shí)現(xiàn)示例

    本篇文章主要介紹了Android音頻開(kāi)發(fā)之音頻采集的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法示例

    Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法示例

    這篇文章主要介紹了Android開(kāi)發(fā)中ProgressDialog簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Android使用ProgressDialog的進(jìn)度條顯示與關(guān)閉、更新等事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • Kotlin中的handler如何避免內(nèi)存泄漏詳解

    Kotlin中的handler如何避免內(nèi)存泄漏詳解

    Handler,我們已經(jīng)相當(dāng)熟悉了,而且經(jīng)常用得不亦樂(lè)乎,但就是因?yàn)樘煜ち?,才?huì)偶爾被它反捅一刀,血流不止,下面這篇文章主要給大家介紹了關(guān)于Kotlin中handler如何避免內(nèi)存泄漏的相關(guān)資料,需要的朋友可以參考下。
    2017-12-12

最新評(píng)論

手机| 灌南县| 建瓯市| 阿勒泰市| 绥江县| 昭通市| 鲁甸县| 眉山市| 广汉市| 民县| 敖汉旗| 东乡| 巴彦县| 民勤县| 故城县| 毕节市| 寿光市| SHOW| 盐亭县| 洞头县| 民和| 祁东县| 库伦旗| 宣汉县| 临江市| 喜德县| 鄂托克旗| 沾益县| 和林格尔县| 新化县| 怀柔区| 民权县| 桐城市| 东兰县| 苍梧县| 渑池县| 饶河县| 乐安县| 武宣县| 屏东市| 凌源市|