android仿人人網右邊可推出的效果

來源:互聯網
上載者:User

添加了拖動功能,按住小表徵圖,拖動超過一半的時候開啟,沒到一半的時候關閉。只添加了OnGestureListener介面和OnTouchListener。 

具體代碼看下面: 

package com.dl.test;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.view.GestureDetector;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.ViewTreeObserver;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout.LayoutParams;public class App extends Activity implements OnPanelStatusChangedListener,OnTouchListener,GestureDetector.OnGestureListener{private boolean hasMeasured=false;private LinearLayout layout_left;private LinearLayout layout_right;private ImageView iv;private int layout_left_width,layout_right_width=0;/**每次自動延伸/收縮的範圍*/  private int MAX_WIDTH=0;/**每次自動延伸/收縮的速度*/      private final static int SPEED=20;        private GestureDetector mGestureDetector;private boolean isScrolling=false;private float mScrollX;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);              layout_left=(LinearLayout)findViewById(R.id.layout_left);        layout_right=(LinearLayout)findViewById(R.id.layout_right);        iv=(ImageView)findViewById(R.id.iv);                iv.setOnTouchListener(this);        //定義手勢識別  mGestureDetector = new GestureDetector(this,this);  mGestureDetector.setIsLongpressEnabled(false);          calculatorWidth();            }            @Overrideprotected void onResume() {// TODO Auto-generated method stubsuper.onResume();}@Overridepublic void onAttachedToWindow() {// TODO Auto-generated method stubsuper.onAttachedToWindow();}private void calculatorWidth(){ ViewTreeObserver observer = layout_right.getViewTreeObserver();//為了取得控制項的寬//       observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {//           @Override//           public void onGlobalLayout() {//        if (hasMeasured == false){//        layout_right_width = layout_right.getMeasuredWidth();//105//            layout_left_width=layout_left.getMeasuredWidth();//480//            MAX_WIDTH=layout_left_width-layout_right_width;//375//            hasMeasured = true;//        }//           }//       });       //為了取得控制項的寬       observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){           public boolean onPreDraw(){               if (hasMeasured == false){               layout_right_width = layout_right.getMeasuredWidth();                   layout_left_width=layout_left.getMeasuredWidth();                   MAX_WIDTH=layout_left_width-layout_right_width;//                   Log.i("tag", "MAX_WIDTH=="+MAX_WIDTH);                   hasMeasured = true;//設定可拉動容器的寬為全屏(即不可拉動容器)的寬                   View layout_max_width=findViewById(R.id.layout_max_width);                   LinearLayout.LayoutParams lp=(LinearLayout.LayoutParams)layout_max_width.getLayoutParams();                   lp.width=layout_left_width;                   layout_max_width.setLayoutParams(lp);               }               return true;           }       });}class AsynMove extends AsyncTask<Integer, Integer, Void> {                @Override          protected Void doInBackground(Integer... params) {              int times;              if (MAX_WIDTH % Math.abs(params[0]) == 0)// 整除                  times = MAX_WIDTH / Math.abs(params[0]);              else                  times = MAX_WIDTH / 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) {            LayoutParams lp = (LayoutParams)layout_right.getLayoutParams();            if (params[0] < 0)                  lp.leftMargin = Math.max(lp.leftMargin + params[0], 0);            else                  lp.leftMargin = Math.min(lp.leftMargin + params[0], MAX_WIDTH);              if(lp.leftMargin<=0){//展開之後                  onPanelOpened();//調用OPEN回呼函數              }            else if(lp.leftMargin>=MAX_WIDTH){//收縮之後                  onPanelClosed();//調用CLOSE回呼函數              }            layout_right.setLayoutParams(lp);          }      }@Overridepublic void onPanelOpened() {// TODO Auto-generated method stub//Log.i("tag", "=========onPanelOpened========");}@Overridepublic void onPanelClosed() {// TODO Auto-generated method stub//Log.i("tag", "=========onPanelClosed========");}@Overridepublic boolean onDown(MotionEvent e) {// TODO Auto-generated method stubmScrollX=0;isScrolling=false;return true;//需要返回ture才能觸發onSingleTapUp}@Overridepublic void onShowPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onSingleTapUp(MotionEvent e) {// TODO Auto-generated method stubLayoutParams lp = (android.widget.RelativeLayout.LayoutParams)layout_right.getLayoutParams();if (lp.leftMargin >=MAX_WIDTH)// CLOSE的狀態              new AsynMove().execute(new Integer[] { -SPEED });// 負數展開          else if (lp.leftMargin >= 0)// OPEN的狀態              new AsynMove().execute(new Integer[] { SPEED });// 正數收縮return true;}@Overridepublic boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {// TODO Auto-generated method stubisScrolling=true;mScrollX+=distanceX;//distanceX:向左為正,右為負LayoutParams lp = (LayoutParams)layout_right.getLayoutParams();lp.leftMargin=lp.leftMargin-(int)mScrollX;if(lp.leftMargin<=0){//展開之後  isScrolling=false;//拖過頭了不需要再執行AsynMove了lp.leftMargin=0;onPanelOpened();//調用OPEN回呼函數  }if(lp.leftMargin>=MAX_WIDTH){//收縮之後  isScrolling=false;lp.leftMargin=MAX_WIDTH;onPanelClosed();//調用CLOSE回呼函數  }layout_right.setLayoutParams(lp);return false;}@Overridepublic void onLongPress(MotionEvent e) {// TODO Auto-generated method stub}@Overridepublic boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stub//拖到一半放開手指時的運動if(event.getAction()==MotionEvent.ACTION_UP && isScrolling==true){LayoutParams lp=(LayoutParams) layout_right.getLayoutParams();if (lp.leftMargin >= (MAX_WIDTH>>1)) {//往左沒超過一半new AsynMove().execute(new Integer[] { SPEED });} else{new AsynMove().execute(new Integer[] { -SPEED });}}return mGestureDetector.onTouchEvent(event); }            }

TestMenu.zip (157.1
KB)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.