android 高仿 猿题库,android 仿猿题库答题UI

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

9a7a2a248145

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

}

布局

9a7a2a248145

9a7a2a248145

9a7a2a248145

9a7a2a248145

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

}

});

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值