android自定义view侧滑菜单
来源:互联网 发布:火影忍者数据 编辑:程序博客网 时间:2024/06/14 18:40
由于sdk版本过低,很多控件居然都没有!!!drawerlayout、 slidingdrawer统统没有!
在线升级SDK却一直失败!网上下载sdk,提示ADT版本过低!在线升级ADT依然无果!
我在电脑前捉鸡捉鸡!
github上有许多很好的项目demo。 多数是android studio,可惜我还停留在渣eclipse上。
此代码的功能主要是实现:左右侧滑菜单。
继承horizontalscrollView。
废话不多说,代码呈上:
package com.shit;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.TypedValue;import android.view.MotionEvent;import android.view.ViewGroup;import android.view.WindowManager;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;public class XSlidinMenu extends HorizontalScrollView{ private LinearLayout mWapper; private ViewGroup mMenu; private ViewGroup mContent; private int mScreenWidth; private int mMenuRightPadding; private int mMenuWidth = 0; private boolean once = false; //Menu是否处于显示状态 private boolean isSlideOut; public static final int RIGHT_PADDING = 100; public XSlidinMenu(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics); mScreenWidth = metrics.widthPixels; //将dp转化为px mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, XSlidinMenu.RIGHT_PADDING, context.getResources().getDisplayMetrics()); } /** * 设置子View的宽和高 * 设置自己的宽和高 */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // TODO Auto-generated method stub if(!once){ mWapper = (LinearLayout) getChildAt(0); mMenu = (ViewGroup) mWapper.getChildAt(0); mContent = (ViewGroup) mWapper.getChildAt(1); mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding; mContent.getLayoutParams().width = mScreenWidth; once = true; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } /** * 通过设置偏移量将Menu隐藏 */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub super.onLayout(changed, l, t, r, b); if(changed){ this.scrollTo(mMenuWidth, 0); } } @Override public boolean onTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_UP: //隐藏在左边宽度 int scrollX = getScrollX(); if(scrollX >= mMenuWidth /2){ //Menu 左滑隐藏起来 this.smoothScrollTo(mMenuWidth, 0); isSlideOut = false; }else{ //Menu 右滑 显示出来 this.smoothScrollTo(0, 0); isSlideOut = true; } return true; } return super.onTouchEvent(ev); } /** * 向右滑出菜单显示出来 */ public void slideOutMenu(){ if(!isSlideOut){ this.smoothScrollTo(0, 0); isSlideOut = true; }else{ return; } } /** * 向左滑出菜单隐藏起来 */ public void slideInMenu(){ if(isSlideOut){ this.smoothScrollTo(mMenuWidth, 0); isSlideOut = false; }else{ return; } } /** * 切换菜单向右滑出显示或向左滑出隐藏的状态 */ public void switchMenu(){ if(isSlideOut){ slideInMenu(); }else{ slideOutMenu(); } } /** * 滚动发生时 */ @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub super.onScrollChanged(l, t, oldl, oldt); //实现抽屉式滑动 float scale = l * 1.0f /mMenuWidth ;//1 ~ 0 float menuScale = 1.0f - scale * 0.3f; float menuAlpha = 0.0f + 1.0f * (1 - scale); float contentScale = 0.8f + 0.2f * scale; //调用属性动画,设置TranslationX ViewHelper.setTranslationX(mMenu, mMenuWidth*scale*0.8f); //左侧菜单的缩放 ViewHelper.setScaleX(mMenu, menuScale); ViewHelper.setScaleY(mMenu, menuScale); //左侧菜单的透明度缩放 ViewHelper.setAlpha(mMenu, menuAlpha); //右侧内容的缩放 ViewHelper.setPivotX(mContent, 0); ViewHelper.setPivotY(mContent, mContent.getHeight() / 2); ViewHelper.setScaleY(mContent, contentScale); ViewHelper.setScaleX(mContent, contentScale); } }
使用侧滑菜单
public class ShitActivit extends BaseActivity { private XSlidinMenu xcSlideMenu; private TextView btnSwitch; private TextView DEMO; private TextView fuck; private static final String SNOW_ONE = "*Date*: "+"2016-1-25-2016-2-4"+"\n";public void setContentView() {getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); xcSlideMenu = (XSlidinMenu) findViewById(R.id.slideMenu); btnSwitch = (TextView)findViewById(R.id.btnSwitch);btnSwitch.setClickable(true); btnSwitch.setOnClickListener(new OnClickListener() { public void onClick(View v) { xcSlideMenu.switchMenu(); } });}
最后还有左边的XML文件
<?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" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_centerInParent="true" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/menu1" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:layout_centerVertical="true" android:textColor="#ffffff" android:textSize="25sp" android:text="CALL" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/menu2" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:layout_centerVertical="true" android:textColor="#ffffff" android:textSize="25sp" android:text="第二个item2" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/menu3" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:layout_centerVertical="true" android:textColor="#ffffff" android:textSize="25sp" android:text="第三个item3" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/menu4" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:layout_centerVertical="true" android:textColor="#ffffff" android:textSize="25sp" android:text="第四个item4" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/menu5" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:gravity="center_vertical" android:layout_centerVertical="true" android:textColor="#ffffff" android:textSize="25sp" android:text="第五个item5" /> </RelativeLayout> </LinearLayout></RelativeLayout>
总布局XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.shit.XSlidinMenu android:id="@+id/slideMenu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/img_frame_background" android:scrollbars="none" > <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" > <include layout="@layout/left" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/qihu" ><com.fuck.Type android:id="@+id/last" android:layout_width="wrap_content" android:layout_height="wrap_content"></com.fuck.Type> <TextView android:id="@+id/btnSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_bg" android:padding="5dp" android:textSize="16sp" android:textColor="#FFF" android:layout_margin="10dp" android:text="切换菜单" /> </LinearLayout> </LinearLayout> </com.shit.XSlidinMenu></RelativeLayout>
文件中的图片,请自行切换ID。否则会找不到文件报错。
0 0
- android自定义view侧滑菜单
- Android自定义View之仿QQ侧滑菜单实现
- Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件
- 侧滑菜单(自定义View)
- 自定义view实战笔记--侧滑菜单
- 自定义View:侧滑菜单动画实现
- 自定义View:侧滑菜单实现
- ViewDragHelper实现自定义view侧滑菜单
- 自定义android侧滑菜单
- Android 自定义侧滑菜单
- Android 自定义侧滑菜单
- android 自定义 梅花形菜单 View
- Android自定义View——展开菜单
- 自定义View以及QQ5.0侧滑菜单实现
- 自定义view 之 仿QQ侧滑菜单
- android自定义侧滑菜单slidmenu
- android自定义侧滑菜单slidmenu
- Android:自定义侧滑菜单控件
- C#多线程(Thread)开发基础
- day28:Spark天堂之门解密
- hdu-1176 免费馅饼【区间dp】
- oracle 11g手工创建数据库
- Strut2错误
- android自定义view侧滑菜单
- XML基础
- 多示例学习 multiple instance learning (MIL)
- 解决Mac java.net Local host name unknown error的方法
- HTTP常用状态码
- 编程技巧
- uvaoj10161
- 学习笔记(四)
- iPhone 的状态栏