Android底部/顶部滑动菜单,方便调用的工具类
来源:互联网 发布:故宫淘宝营销策略分析 编辑:程序博客网 时间:2024/06/05 11:57
大家都不用AlertDialog了,发现自己的应用还是AlertDialog,就想弄个打开底部/顶部菜单的工具类方法,封装个类可能好些,但是这样方便!
public class AndUtil { /** *打开底部/顶部滑动菜单 * @param context 上下文 Activity * @param contentView 菜单内容 * @param showTp 0:底部弹出 other:顶部 * @return 返回值为VerticalHolderView */ public static View openSldBtmMenu(final Context context, View contentView,final int showTp) { if (context == null || contentView == null) return null; int[] hdLyloc = new int[2]; final int sHeight=getScreenHeight(context); Activity act = (Activity) context; VerticalHolderView hdVw = new VerticalHolderView(context); hdVw.setVerticalScrollBarEnabled(false); FrameLayout.LayoutParams reLyPrms1 = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); hdVw.setLayoutParams(reLyPrms1); final RelativeLayout reLy = new RelativeLayout(context); FrameLayout.LayoutParams reLyPrms = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); reLy.setLayoutParams(reLyPrms); reLy.addView(contentView); ViewGroup vg = (ViewGroup) act.getWindow().getDecorView(); hdVw.addView(reLy); vg.addView(hdVw); vg.getLocationOnScreen(hdLyloc); final int reLHt=getViewHeight(reLy); final int hdoff=hdLyloc[1]; if(showTp==0) { doBtmMenuAnimation(context, reLy, sHeight, sHeight - hdoff - reLHt, 300, true); }else{ doBtmMenuAnimation(context, reLy,-reLHt,0, 300, true); } //添加滑动事件,如果觉得堆在这里很挤,可以把实现写出去,弄个构造方法 hdVw.setOnTouchListener(new View.OnTouchListener() { private Context contexta=context; private RelativeLayout reLya =reLy; private float ny, ny1, ry; private int magtop; private boolean mvf = false; private FrameLayout.LayoutParams params; private int reLHta=reLHt; private int showTpa=showTp; private int sHeighta=sHeight; private int hdoffa = hdoff; @Override public boolean onTouch(View v, MotionEvent event) {
boolean aniFlag=(boolean)reLya.getTag();//标志位,防止动画执行期间重复触发if(aniFlag) return aniFlag; ry = (int) event.getRawY(); if (event.getAction() == MotionEvent.ACTION_DOWN) { mvf = false; if(showTpa==0){ magtop = sHeighta-hdoffa-reLHta; }else{ magtop = 0; } params = (FrameLayout.LayoutParams) reLya.getLayoutParams(); } else if (event.getAction() == MotionEvent.ACTION_UP||event.getAction() == MotionEvent.ACTION_CANCEL) { if (mvf) { boolean b = false; if (ny > ny1) { b = true; } int endp; if(showTpa==0) { endp=b?sHeighta:sHeighta - hdoffa - reLHta; b=!b; }else{ endp=b?0:-reLHta; } doBtmMenuAnimation(contexta, reLya, magtop,endp, 300, b); } else {//这里处理类似单击事件 int lnup=magtop+hdoffa; int lndn=lnup+reLHta; if(ry>lndn||ry<lnup){ Log.d("AndUtil", "it looks like hdVw clicked! "); closeSldBtmMenu(contexta, (View)reLya.getParent()); } } } else if (event.getAction() == MotionEvent.ACTION_MOVE && Math.abs(ry - ny) >= 5) {//移动大于5px才算move,主要用来区别开click,有拖影就小一点 mvf = true; magtop += (int) (ry - ny); if(showTpa==0){ int mxmg=sHeighta-hdoffa-reLHta; if(magtop<=mxmg){ magtop=mxmg; } }else{ if(magtop>=0){ magtop=0; } } if (params != null) { params.topMargin = magtop; reLya.setLayoutParams(params); } } ny1 = ny; ny = ry; return true; } }); return hdVw; } /** *关闭底部/顶部滑动菜单 * @param context 上下文 Activity * @param genSldView VerticalHolderView */ public static void closeSldBtmMenu(Context context, View genSldView) { if (context == null || genSldView == null) return; ViewGroup vg=(ViewGroup)genSldView; View vw=vg.getChildAt(0); if(vw==null)return; int sHeight=getScreenHeight(context); FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) vw.getLayoutParams(); int k=params.topMargin; if(0==k){ doBtmMenuAnimation(context, vw,k,-getViewHeight(vw), 300, false); }else{ doBtmMenuAnimation(context, vw,k,sHeight, 300, false); } } /** *移除底部/顶部滑动菜单View * @param context 上下文 Activity * @param genSldView VerticalHolderView */ private static void removeBtmMenuVw(Context context, View genSldView){ if (context == null || genSldView == null) return; Activity act = (Activity) context; ViewGroup vg = (ViewGroup) act.getWindow().getDecorView(); vg.removeView(genSldView); } /** * 打开关闭底部/顶部滑动菜单动画 * @param context * @param vw 菜单动态部分 * @param begin 位置开始 * @param end 位置结束 * @param duration 持续时间 * @param b false:动画结束移除View */ private static void doBtmMenuAnimation(final Context context,final View vw,final int begin, final int end, int duration, final boolean b) { final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) vw.getLayoutParams(); ValueAnimator mAnimator = ValueAnimator.ofInt(begin, end); mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int aniVal = (int) animation.getAnimatedValue(); params.topMargin = aniVal; vw.setLayoutParams(params); } }); mAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) {
vw.setTag(true);//标志位
} @Override public void onAnimationEnd(Animator animation) { params.topMargin = end; vw.setLayoutParams(params);
vw.setTag(false);//标志位 if(!b){ removeBtmMenuVw(context,(View)vw.getParent()); } } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); mAnimator.setInterpolator(new DecelerateInterpolator()); mAnimator.setDuration(duration); mAnimator.start(); } public static int dp2Px(Context context, float dp) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); } public static float px2Dp(Context context, float px) { final float scale = context.getResources().getDisplayMetrics().density; return (px / scale); } public static int sp2px(Context context, float spVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, context.getResources().getDisplayMetrics()); } public static float px2sp(Context context, float pxVal) { return (pxVal / context.getResources().getDisplayMetrics().scaledDensity); } /** * 获得屏幕宽度 * * @param context * @return */ public static int getScreenWidth(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.widthPixels; } /** * 获得屏幕的高度 * * @param context * @return */ public static int getScreenHeight(Context context) { WindowManager wm = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); return outMetrics.heightPixels; } public static int getViewWidth(View view) { int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(width, height); int w = view.getMeasuredWidth(); return w; } public static int getViewHeight(View view) { int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); view.measure(width, height); int h = view.getMeasuredHeight(); return h; }}
public class VerticalHolderView extends ScrollView { public VerticalHolderView(Context context) { super(context); } public VerticalHolderView(Context context, AttributeSet attrs) { super(context, attrs); } public VerticalHolderView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent ev) { return false; }}
测试一下:
case R.id.btmCtn_Btn:
TextView t = new TextView(MainActivity.this); t.setText("Android底部/顶部滑动菜单,方便调用的工具类"); t.setBackgroundColor(0xffffffff); t.setGravity(Gravity.CENTER); t.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); t.setTextColor(0xff000000); t.setLineSpacing(0,1.2f); RelativeLayout.LayoutParams laypa = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); laypa.height=800; t.setLayoutParams(laypa); tmpView=AndUtil.openSldBtmMenu(MainActivity.this,t,1);
break; case R.id.btmCtn_closeBtn:
AndUtil.closeSldBtmMenu(MainActivity.this,tmpView); tmpView=null;
break;
0 0
- Android底部/顶部滑动菜单,方便调用的工具类
- Android底部/顶部滑动菜单SldMenu
- android去掉滑动到顶部和底部的阴影
- android去掉滑动到顶部和底部的阴影
- Android 仿美团顶部滑动菜单
- 简单的顶部滑动菜单
- 同时弹出顶部和底部的菜单
- android实现类似淘宝的底部滑动导航菜单
- Android 简单实现顶部滑动菜单效果
- android listview或者ScrollView 去除顶部下拉时的蓝底或者上下滑动顶部和底部的虚化
- android listview/ScrollView 去除顶部下拉时的半月形阴影和上下滑动顶部和底部的虚化
- android listview/ScrollView 去除顶部下拉时的半月形阴影和上下滑动顶部和底部的虚化
- Android开发--------------WebView(二)之WebView的滑动底部顶部监听,加载进度条等设置
- Android取消控件滑动顶部或底部造成的渐变效果
- Android - ScrollView滑动到底部或者顶部的两种响应方式
- Android ScrollView监听滑动到顶部和底部的两种方式
- 关于Android滑动控件顶部与底部阴影块的颜色替换
- Android的底部菜单
- 配置方法数超过 64K 的应用
- copy构造函数的调用时机1和2
- 222. Count Complete Tree Nodes
- APP开发实战148-使用AOP技术输出Log的具体实现
- 【WEB】前端系统配色方案(全览)
- Android底部/顶部滑动菜单,方便调用的工具类
- Java Web乱码 解决request中文乱码的问题
- gradle学习笔记之hook task
- 深入理解Java虚拟机3~垃圾收集器参数总结
- 使用Unity连接Bmob后端云
- Java数据结构和算法:HashMap,哈希表,哈希函数
- 产品与运营文章索引
- CSS中可以和不可以继承的属性
- win10+cuda8.0+cudnn+Tensorflow(GPU)安装