Android底部/顶部滑动菜单,方便调用的工具类

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xyzdwf/article/details/54177369

大家都不用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;
    }


}


相关类,其实就是 ScrollView 取消了Touch事件

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;





猜你喜欢

转载自blog.csdn.net/xyzdwf/article/details/54177369
今日推荐