android多媒体编程题库,android 仿猿题库答题UI
需要做類似猿題庫答題板效果,網上沒有找到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多媒体编程题库,android 仿猿题库答题UI的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 自定义paint,And
- 下一篇: android 6.0 sd卡读写权限,