Android 通过手势类实现的可拖动布局实例



      平板项目中,由于分为上下两个布局,横竖屏切换时,下方的界面需要显示更多的数据,所以就想到能不能拖动中间的
按钮,然后实时的改变布局的高度,让其显示更多的数据,找到一篇博客如下:转自: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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值