平板项目中,由于分为上下两个布局,横竖屏切换时,下方的界面需要显示更多的数据,所以就想到能不能拖动中间的
按钮,然后实时的改变布局的高度,让其显示更多的数据,找到一篇博客如下:转自:http://www.apkbus.com/thread-74732-1-1.html
功能如下:可实现上下界面分屏显示,拖动到上面屏幕以上时,下屏全屏显示;拖动到下面屏幕以下时,上屏全屏显示;拖动过程中附加动画效果;
实现步骤:
1.实现GestureDetector.OnGestureListener接口
拖动的处理在如下方法中处理:
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
mIsScrolling = true;
mScrolly += distanceY;
if (mScrolly > 0) {
upLP.height = upLP.height + (int) -mScrolly;
downLP.height = LayoutParams.FILL_PARENT - upLP.height;
if (upLP.height > 0 && upLP.height < 735) {
upFrameLayout.setLayoutParams(upLP);
downFrameLayout.setLayoutParams(downLP);
System.out.println("upHeight:" + upLP.height);
System.out.println("downHeight:" + downLP.height);
}
}
if (mScrolly < 0) {
upLP.height = upLP.height + (int) -mScrolly;
downLP.height = LayoutParams.FILL_PARENT - upLP.height;
if (upLP.height > 0 && upLP.height < 735) {
upFrameLayout.setLayoutParams(upLP);
downFrameLayout.setLayoutParams(downLP);
}
}
return false;
}
2.通过当前布局高度,触发布局异步改变
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP && // onScroll时的ACTION_UP
mIsScrolling == true) {
System.out.println("upHeight:" + upLP.height);
System.out.println("downHeight:" + downLP.height);
if (upLP.height >= 190 && upLP.height < 550) {
new AsynMove().execute(new Integer[] { -MOVE_WIDTH });//向中间收缩
} else if (upLP.height >= 550 || upLP.height < 200) {
new AsynMove().execute(new Integer[] { MOVE_WIDTH });//向上下两边收缩
}
}
return mGestureDetector.onTouchEvent(event);
}
};
3.异步改变布局,实现动画效果
class AsynMove extends AsyncTask<Integer, Integer, Void> {
@Override
protected Void doInBackground(Integer... params) {
int times;
if (upLP.height % Math.abs(params[0]) == 0)// 整除
times = upLP.height / Math.abs(params[0]);
else
// 有余数
times = upLP.height / Math.abs(params[0]) + 1;
for (int i = 0; i < times; i++) {
publishProgress(params);
try {
Thread.sleep(Math.abs(params[0]));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... params) {
if (params[0] < 0) {
upLP.height = Math.max(upLP.height + params[0], 380);
} else if (upLP.height > 190) {
upLP.height = Math.min(upLP.height + params[0], 735);
} else {
upLP.height = Math.min(upLP.height + params[0], 0);
}
System.out.println("progressHeight:" + upLP.height);
upFrameLayout.setLayoutParams(upLP);
}
}
资源下载地址为:http://www.apkbus.com/forum.php?mod=attachment&aid=MzcwMDd8YWNmYWMyY2F8MTQ4MjQ2MjQ1MXwwfDc0NzMy