美文网首页程序员
android 仿猿题库答题UI

android 仿猿题库答题UI

作者: 天涯_a67d | 来源:发表于2018-05-13 13:54 被阅读0次

    需要做类似猿题库答题板效果,网上没有找到Android的,就自己写一个,浮层上滑底层UI跟着改变的UI

    public class DragLinearLayoutextends LinearLayout {

    private int screenWidth;

        private int screenHeight;

        int lastX, lastY;

        // 滑动监听

        public TouchListenertouchListener;

        public DragLinearLayout(Context context) {

    super(context);

            init();

        }

    public DragLinearLayout(Context context, AttributeSet attrs) {

    super(context, attrs);

            init();

        }

    public void setTouchListener(TouchListener touchListener) {

    this.touchListener = touchListener;

        }

    public void init() {

    DisplayMetrics dm = getResources().getDisplayMetrics();

            screenWidth = dm.widthPixels;

            screenHeight = dm.heightPixels;

        }

    @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);

        }

    /**

    *

        * @param groupViewHeight 父容器高度-拖拽按钮高度

        * @param v

        * @param event

        */

        public void slideView(int groupViewHeight,View v, MotionEvent event){

    int action = event.getAction();

            switch (action) {

    case MotionEvent.ACTION_DOWN:

    lastX = (int) event.getRawX();

                    lastY = (int) event.getRawY();

    break;

                case MotionEvent.ACTION_MOVE:

    int dx = (int) event.getRawX() -lastX;

                    int dy = (int) event.getRawY() -lastY;

                    int left = DragLinearLayout.this.getLeft() + dx;

                    int top = DragLinearLayout.this.getTop() + dy;

                    int right = DragLinearLayout.this.getRight() + dx;

                    int bottom = DragLinearLayout.this.getBottom() + dy;

                    if (left <0) {

    left =0;

                        right = left + DragLinearLayout.this.getWidth();

                    }

    if (right >screenWidth) {

    right =screenWidth;

                        left = right - DragLinearLayout.this.getWidth();

                    }

    if (top <0) {

    top =0;

                        bottom = top + DragLinearLayout.this.getHeight();

                    }

    if (top > groupViewHeight) {

    RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) getLayoutParams();

                        top = groupViewHeight;

                        rl.topMargin = top;

                        setLayoutParams(rl);

                        callBackMessage(left, top, right, bottom);

    return ;

                    }else {

    callBackMessage(left, top, right, bottom);

                    }

    RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) getLayoutParams();

                    rl.topMargin = top;

                    setLayoutParams(rl);

                    lastX = (int) event.getRawX();

                    lastY = (int) event.getRawY();

    break;

            }

    }

    private void callBackMessage(int left,int top,int right,int bottom){

    if(touchListener!=null){

    touchListener.backTouchState(left, top, right, bottom);

            }

    }

    /**

    * Touch监听接口

    */

        public interface TouchListener {public void backTouchState(int left, int top, int right, int bottom);}

    }

    布局

    MainActivity

    主要方法

    /**

    * 按钮的触摸事件

    */

    btn_drag.setOnTouchListener(new View.OnTouchListener() {

    @Override

        public boolean onTouch(View view, MotionEvent motionEvent) {

    //触摸按钮的高度

            int height_drag =btn_drag.getHeight();

            //父容器的高度

            int height =rel.getHeight();

            int i = height - height_drag;

            //滑动浮层

            sbrl.slideView(i,view,motionEvent);

    return false;

        }

    });

    /**

    * 被覆盖层随随着浮层的滑动改变大小

    */

    sbrl.setTouchListener(new DragLinearLayout.TouchListener() {

    @Override

        public void backTouchState(int left, int top, int right, int bottom) {

    int bottom1 =sbrl.getBottom();

            int height_drag  =btn_drag.getHeight();

            int height =rel.getHeight();

                RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams)neds.getLayoutParams();

                rl.bottomMargin =height-top-height_drag;

                neds.setLayoutParams(rl);

            Log.e("ice","------left---------" + left +"--top-" + top

    +"----right--" + right +"---bottom-" + bottom+

    "-------bottom1 = "+bottom1+" i "+i);

        }

    });

    //第一次写,希望能帮到大家,

    相关文章

      网友评论

        本文标题:android 仿猿题库答题UI

        本文链接:https://www.haomeiwen.com/subject/natldftx.html