多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > android5.0多種側滑欄效果

android5.0多種側滑欄效果

來源:程序員人生   發布時間:2016-11-03 08:29:37 閱讀次數:2526次

1.普通側滑
效果圖:這里寫圖片描述
思路:通過自定義View繼承HorizontalScrollView,然后重寫onMeasure(),onLayout(),onTouchEvent()
方法并設置menu(通過動畫使menu開始時處于隱藏狀態)布局和content布局。(注意:使用ViewHelper類需要導入nineoldandroids⑵.4.0.jar包)
menu(left_menu)布局代碼:

<?xml version="1.0" encoding="utf⑻"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <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/id_img1" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_centerVertical="true" android:src="@mipmap/img_1"/> <TextView android:id="@+id/iv_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第1個item" android:textSize="21sp" android:textColor="#ffffff" android:layout_toRightOf="@+id/id_img1" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/id_img2" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_centerVertical="true" android:src="@mipmap/img_2"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第2個item" android:textSize="21sp" android:textColor="#ffffff" android:layout_toRightOf="@+id/id_img2" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/id_img3" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_centerVertical="true" android:src="@mipmap/img_3"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第3個item" android:textSize="21sp" android:textColor="#ffffff" android:layout_toRightOf="@+id/id_img3" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/id_img4" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_centerVertical="true" android:src="@mipmap/img_4"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第4個item" android:textSize="21sp" android:textColor="#ffffff" android:layout_toRightOf="@+id/id_img4" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageView android:id="@+id/id_img5" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:layout_centerVertical="true" android:src="@mipmap/img_5"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第5個item" android:textSize="21sp" android:textColor="#ffffff" android:layout_toRightOf="@+id/id_img5" android:layout_marginLeft="20dp" android:layout_centerVertical="true"/> </RelativeLayout> </LinearLayout> </RelativeLayout>

content(activity_main)布局代碼:

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:hyname="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/img_frame_background"> <com.imooc.view.SlidingMenu android:id="@+id/id_menu" android:layout_width="match_parent" android:layout_height="match_parent" hyname:rightPadding="100dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <include layout="@layout/left_menu"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@mipmap/qq"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="切換菜單" android:onClick="toogleMenu" android:textSize="21sp"/> </LinearLayout> </LinearLayout> </com.imooc.view.SlidingMenu> </LinearLayout>

自定義attr.xml文件代碼:

<?xml version="1.0" encoding="utf⑻"?> <resources> <attr name="rightPadding" format="dimension"/> <declare-styleable name="SlidingMenu"> <attr name="rightPadding"></attr> </declare-styleable> </resources>

自定義SlidingMenu代碼:

public class SlidingMenu extends HorizontalScrollView { private LinearLayout mWapper; private ViewGroup mMenu;//菜單布局 private ViewGroup mContent;//內容布局 private int mScreenWidth;//屏幕寬度 private int mMenuRightPadding=50; private boolean once; private int mMenuWidth; private boolean isOpen; public SlidingMenu(Context context) { this(context, null); } /** * 未使用自定義屬性時,調用 * @param context * @param attrs */ public SlidingMenu(Context context, AttributeSet attrs) { this(context, attrs,0); } /** * 自定義了屬性且使用時,調用次構造方法 * @param context * @param attrs * @param defStyleAttr */ public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); //獲得定義的屬性的數組 TypedArray typedValue=context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyleAttr, 0); int n=typedValue.getIndexCount(); for (int i=0;i<n;i++){ int attr=typedValue.getIndex(i); switch (attr){ case R.styleable.SlidingMenu_rightPadding: mMenuRightPadding=typedValue.getDimensionPixelSize(attr,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,context.getResources().getDisplayMetrics())); break; } } typedValue.recycle(); WindowManager mg= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); //初始化屏幕信息對象 DisplayMetrics outMetrics=new DisplayMetrics(); //把屏幕的信息存儲到DisplayMetrics中 mg.getDefaultDisplay().getMetrics(outMetrics); //獲得屏幕寬度賦值給mScreenWidth mScreenWidth=outMetrics.widthPixels; } /** * 設置子view的寬和高 * 設置自己的寬和高 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if(!once){ //獲得SlidingMenu中的Linearlayout布局 mWapper= (LinearLayout) getChildAt(0); //獲得LinearLayout中的menu布局 mMenu= (ViewGroup) mWapper.getChildAt(0); //獲得LinearLayout中的Content布局 mContent= (ViewGroup) mWapper.getChildAt(1); //獲得menu寬度 mMenuWidth= mMenu.getLayoutParams().width=mScreenWidth-mMenuRightPadding; //設置content的寬度 mContent.getLayoutParams().width=mScreenWidth; mWapper.getLayoutParams().width=mScreenWidth; once=true; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * 通過設置偏移量,將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); if(changed){ this.scrollTo(mMenuWidth,0); } } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()){ case MotionEvent.ACTION_UP: //隱藏在左側的寬度 int scrollX=getScrollX(); if (scrollX>=mMenuWidth/2){ this.smoothScrollTo(mMenuWidth,0); isOpen=false; }else { this.smoothScrollTo(0,0); isOpen=true; } return true; } return super.onTouchEvent(ev); } public void openMenu(){ if(isOpen)return; this.smoothScrollTo(0,0); isOpen=true; } public void closeMenu(){ if(!isOpen)return; this.smoothScrollTo(mMenuWidth,0); isOpen=false; } //切換菜單 public void toggle(){ if(isOpen){ closeMenu(); }else { openMenu(); } } }

主文件代碼:

public class MainActivity extends AppCompatActivity { private SlidingMenu mleftMenu; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mleftMenu= (SlidingMenu) findViewById(R.id.id_menu); textView= (TextView) findViewById(R.id.iv_text); //menu的第1個Item的點擊事件,可不寫 textView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mleftMenu.toggle(); } }); } public void toogleMenu(View view){ mleftMenu.toggle(); } }

2.抽屜式側滑(1)
效果圖:這里寫圖片描述
思路:在原來的基礎上,在自定義View文件中重寫onScrollChanged()方法
添加代碼:

/** * 轉動時產生 * @param l * @param t * @param oldl * @param oldt */ @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); //調用屬性動畫,設置TranslateX,l值為menu隱藏的寬度,menu由完全隱藏變成完全可見,變化梯度 scale由1~0,menu偏移量由大到小; float scale=l*1.0f/mMenuWidth; //1~0 ViewHelper.setTranslationX(mMenu, mMenuWidth * scale); }

3.抽屜式側滑(2)
效果圖:這里寫圖片描述
思路:在1的基礎上通過設置menu的縮放效果,content的縮放效果和縮放中心實現。
實現代碼:

/** * 轉動產生 * @param l * @param t * @param oldl * @param oldt */ @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); //調用屬性動畫,設置TranslateX,l值為menu隱藏的寬度,menu由完全隱藏變成完全可見,變化梯度scale由1~0,menu偏移量由大到小; float scale=l*1.0f/mMenuWidth; //1~0 // ViewHelper.setTranslationX(mMenu, mMenuWidth * scale); float leftScale=1.0f-scale*0.3f; //0.7~1.0 float leftAlpha=0.6f+0.4f*(1-scale); //0.6~1.0 float rightScale=0.7f+0.3f*scale; //1.0~0.7 //縮放動畫0.7~1.0 ViewHelper.setScaleX(mMenu, leftScale); ViewHelper.setScaleY(mMenu, leftScale); //透明度變化0.6~1.0 ViewHelper.setAlpha(mMenu, leftAlpha); ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.7f); ViewHelper.setPivotX(mContent, 0); ViewHelper.setPivotY(mContent, mContent.getHeight() / 2); //縮放動畫1.0~0.7 ViewHelper.setScaleX(mContent, rightScale); ViewHelper.setScaleY(mContent,rightScale); }

有不懂的地方可以到慕課網聽講授:http://www.imooc.com/learn/211
圖片來源于:http://blog.csdn.net/lmj623565791/article/details/39257409博客。

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 中文字幕第九页 | 91久色视频| 精品精品 | 2022国产精品网站在线播放 | 日韩一区在线视频 | 亚洲精品短视频 | 亚洲精品国产一区二区图片欧美 | 婷婷伊人网 | 福利视频第一区 | 午夜dj视频在线视频中文 | 精品亚洲福利一区二区 | 真实国产精品视频国产网 | 欧美成人一区二区三区不卡 | 国产成人午夜性a一级毛片 国产成人系列 | 最近高清中文字幕大全1 | 国产成年网站v片在线观看 国产成人 免费观看 | 黄色免费网站在线看 | 亚洲国产日韩欧美 | 色尼玛亚洲综合 | 国产模特众筹精品视频 | 最近高清中文在线字幕在线观看 | 国内精品久久久久久网站 | 亚洲精品综合 | 涩涩免费播放观看在线视频 | 国产一区2区3区 | 91精品国产高清久久久久 | 亚洲在线小视频 | 99久久久国产精品免费牛牛四川 | 亚洲全网成人资源在线观看 | 成人eeuss影院在线观看 | www.九色.com| 操一炮在线 | 国产一区二区三区在线免费观看 | 亚洲黄色片网站 | 欧美亚洲另类图片 | 中国女人freexxxx性mp4 | 欧美黑人喷潮水xxxx | 高清freexxxx性 | 福利视频欧美一区二区三区 | 亚洲激情校园春色 | 中文天堂|