拖動一個控制項

來源:互聯網
上載者:User

轉自http://gundumw100.iteye.com/blog/924736

這個我也試過,好像拖動控制項只能是一個Button控制項,我試過用TextView和那幾個布局邊框,都不行,下面是他人的代碼,我的還在更新中

實現效果: 滑鼠拖動btn SSS,SSS在水平的layout上移動。 當滑鼠抬起 響應UP事件。SSS會自動移動到距離其最近的Btn上,與其重合。即SSS只存在五個固定的顯示位置。

SSS響應setOnTouchListener事件。

在MotionEvent.ACTION_UP事件中,調用TranslateAnimation動畫效果,將其從UP事件位置移動到最近的btn所在位置。

即在UP事件中,響應函數:

Java代碼  
  1. private void setPosition() {  
  2.             int positionPixel = (touchBtn.getLeft()+touchBtn.getRight())/2;  
  3.             int positionIndex = (positionPixel)/btn[1].getWidth();  
  4.             int toPosition = positionIndex*btn[1].getWidth()+touchBtn.getWidth()/2;          
  5.             touchBtn.layout(positionIndex*btn[1].getWidth(), touchBtn.getTop(),positionIndex*btn[1].getWidth()+touchBtn.getWidth(),   
  6.                             touchBtn.getBottom());              
  7.             MoveAction = new TranslateAnimation(positionPixel - toPosition,0,0,0);  
  8.             MoveAction.setDuration(500);  
  9.             touchBtn.startAnimation(MoveAction);  
  10. //            touchBtn.invalidate();              
  11.         }  

動畫效果,將其移動到最近位置上

或者也可以這樣計算:Java代碼  

  1. /** 
  2. *獲得最佳停留位置 
  3. */  
  4. private void setBestPosition(View v) {  
  5.         int width=v.getWidth();  
  6.         int left = v.getLeft();  
  7.         int selectedPosition = Math.round(1.0F*left/width);//四捨五入    
  8.         int toPosition = selectedPosition*width;    
  9.         v.layout(selectedPosition*width, v.getTop(),    
  10.                 selectedPosition*width+width, v.getBottom());    
  11.         TranslateAnimation animation = new TranslateAnimation(left-toPosition,0,0,0);    
  12.         animation.setInterpolator(new LinearInterpolator());      
  13.         animation.setDuration(400);    
  14.         animation.setFillAfter(true);      
  15.         v.startAnimation(animation);   
  16. //        v.invalidate();  
  17.     }  

全代碼:Java代碼  

  1. public class App extends Activity{  
  2.     private static final String tag="App";  
  3.     private Context context;  
  4.     private FrameLayout container;  
  5.     private Button btn;  
  6.     /** Called when the activity is first created. */  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11.         context=this;  
  12.           
  13.         container=(FrameLayout)findViewById(R.id.container);  
  14.           
  15.         btn=(Button)findViewById(R.id.btn);  
  16.         btn.setBackgroundResource(R.drawable.tabswitcher_short);  
  17.         btn.setOnTouchListener(touchLisener);  
  18.         btn.setOnClickListener(new OnClickListener() {  
  19.               
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 // TODO Auto-generated method stub  
  23.                 Log.i(tag,"btn clicked");  
  24.                   
  25.             }  
  26.         });  
  27.     }  
  28.       
  29.     @Override  
  30.     protected void onResume() {  
  31.         // TODO Auto-generated method stub  
  32.         super.onResume();  
  33.     }  
  34.   
  35.     OnTouchListener touchLisener=new OnTouchListener() {  
  36.         int lastX, lastY;  
  37.         @Override  
  38.         public boolean onTouch(View v, MotionEvent event) {  
  39.             // TODO Auto-generated method stub  
  40.             switch (event.getAction()) {  
  41.             case MotionEvent.ACTION_DOWN:  
  42.                 lastX = (int) event.getRawX();  
  43.                 lastY = (int) event.getRawY();  
  44.                 break;  
  45.             case MotionEvent.ACTION_MOVE:  
  46.                 int dx = (int) event.getRawX() - lastX;    
  47. //              int dy = (int) event.getRawY() - lastY;    
  48.                 int dy = 0;    
  49.   
  50.                 int left = v.getLeft() + dx;    
  51.                 int top = v.getTop() + dy;    
  52.                 int right = v.getRight() + dx;    
  53.                 int bottom = v.getBottom() + dy;    
  54.   
  55.                 if (left < 0) {    
  56.                     left = 0;    
  57.                     right = left + v.getWidth();    
  58.                 }    
  59.   
  60.                 if (right > container.getMeasuredWidth()) {    
  61.                     right = container.getMeasuredWidth();    
  62.                     left = right - v.getWidth();    
  63.                 }    
  64.   
  65.                 if (top < 0) {    
  66.                     top = 0;    
  67.                     bottom = top + v.getHeight();    
  68.                 }    
  69.   
  70.                 if (bottom > container.getMeasuredHeight()) {    
  71.                     bottom = container.getMeasuredHeight();    
  72.                     top = bottom - v.getHeight();    
  73.                 }    
  74.   
  75.                 v.layout(left, top, right, bottom);    
  76.   
  77.                 lastX = (int) event.getRawX();    
  78.                 lastY = (int) event.getRawY();  
  79.                 break;  
  80.             case MotionEvent.ACTION_UP:  
  81.                 setBestPosition(v);  
  82.                 break;  
  83.   
  84.             default:  
  85.                 break;  
  86.             }  
  87.             return false;  
  88.         }  
  89.     };  
  90.       
  91.     private void setBestPosition(View v) {  
  92.         int width=v.getWidth();  
  93.         int left = v.getLeft();  
  94.         int selectedPosition = Math.round(1.0F*left/width);//四捨五入    
  95.         int toPosition = selectedPosition*width;    
  96.         v.layout(selectedPosition*width, v.getTop(),    
  97.                 selectedPosition*width+width, v.getBottom());    
  98.         TranslateAnimation animation = new TranslateAnimation(left-toPosition,0,0,0);    
  99.         animation.setInterpolator(new LinearInterpolator());      
  100.         animation.setDuration(400);    
  101.         animation.setFillAfter(true);      
  102.         v.startAnimation(animation);   
  103. //        v.invalidate();  
  104.     }  
  105. }  

布局Xml代碼  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res/com.ql.app"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent"  
  7.     >  
  8.     <TextView android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="TEST DRAG"  
  11.         android:textSize="20sp"  
  12.         />  
  13.          <FrameLayout android:id="@+id/container"  
  14.          android:layout_width="fill_parent"  
  15.         android:layout_height="fill_parent"  
  16.         android:layout_weight="1"  
  17.         >  
  18.             <Button android:id="@+id/btn"  
  19.             android:layout_width="80dp"  
  20.             android:layout_height="wrap_content"  
  21.             android:text="drag me!"  
  22.             />  
  23.         </FrameLayout>  
  24.         <EditText   
  25.         android:layout_width="fill_parent"  
  26.         android:layout_height="wrap_content"  
  27.         />  
  28. </LinearLayout> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.