1. 程式人生 > >Android 通過手勢類實現的可拖動佈局例項

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