android 控件跟隨手指移動

項目中遇到控件跟隨手指觸摸移動的部分,簡單測試了下。

[java] view plain copy

  1. package com.example.viewanimationtest;  

  2.   

  3. /** 

  4.  * @author maria 

  5.  * 2013-2-16 

  6.  */  

  7. import android.os.Bundle;  

  8. import android.app.Activity;  

  9. import android.view.MotionEvent;  

  10. import android.view.View;  

  11. import android.view.View.OnTouchListener;  

  12. import android.view.ViewGroup;  

  13. import android.widget.Button;  

  14. import android.widget.RelativeLayout;  

  15.   

  16. public class MainActivity extends Activity implements OnTouchListener {  

  17.   

  18.     Button _view;  

  19.     ViewGroup _root;  

  20.     private int _xDelta;  

  21.     private int _yDelta;  

  22.   

  23.     @Override  

  24.     public void onCreate(Bundle savedInstanceState) {  

  25.         super.onCreate(savedInstanceState);  

  26.         setContentView(R.layout.activity_main);  

  27.   

  28.         _root = (ViewGroup) findViewById(R.id.root);  

  29.   

  30.         _view = (Button) findViewById(R.id.id_text);  

  31.   

  32.         RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(  

  33.                 15050);  

  34.         layoutParams.leftMargin = 50;  

  35.         layoutParams.topMargin = 50;  

  36.         layoutParams.bottomMargin = -250;  

  37.         layoutParams.rightMargin = -250;  

  38.         _view.setLayoutParams(layoutParams);  

  39.         _view.setOnTouchListener(this);  

  40.     }  

  41.   

  42.     public boolean onTouch(View view, MotionEvent event) {  

  43.         final int X = (int) event.getRawX();  

  44.         final int Y = (int) event.getRawY();  

  45.         switch (event.getAction() & MotionEvent.ACTION_MASK) {  

  46.         case MotionEvent.ACTION_DOWN:  

  47.             RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view  

  48.                     .getLayoutParams();  

  49.             _xDelta = X - lParams.leftMargin;  

  50.             _yDelta = Y - lParams.topMargin;  

  51.             break;  

  52.         case MotionEvent.ACTION_UP:  

  53.             break;  

  54.         case MotionEvent.ACTION_POINTER_DOWN:  

  55.             break;  

  56.         case MotionEvent.ACTION_POINTER_UP:  

  57.             break;  

  58.         case MotionEvent.ACTION_MOVE:  

  59.             RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view  

  60.                     .getLayoutParams();  

  61.             layoutParams.leftMargin = X - _xDelta;  

  62.             layoutParams.topMargin = Y - _yDelta;  

  63.             layoutParams.rightMargin = -250;  

  64.             layoutParams.bottomMargin = -250;  

  65.             view.setLayoutParams(layoutParams);  

  66.             break;  

  67.         }  

  68.         _root.invalidate();  

  69.         return true;  

  70.     }  

  71. }  


 

.xml

[html] view plain copy

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     xmlns:tools="http://schemas.android.com/tools"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:id="@+id/root"  

  6.     tools:context=".MainActivity" >  

  7.   

  8.     <Button  

  9.         android:id="@+id/id_text"  

  10.         android:layout_width="wrap_content"  

  11.         android:layout_height="wrap_content"  

  12.         android:layout_centerHorizontal="true"  

  13.         android:layout_centerVertical="true"  

  14.         android:text="@string/hello_world" />  

  15.   

  16. </RelativeLayout>  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章