Android控制項拖動的實現

來源:互聯網
上載者:User

              這個也是從網上得到的代碼,例子比較簡單,但是如果有需要此功能的,這個例子可以提供很多提示,首先,給個




這個是拖動以後的效果,一個imageview和一個button控制項,提供兩份代碼下載吧,一份是只有一個Button的,另一份就是像,就是多了一個imagview!先看下代碼吧,比較簡單:

public class DraftTest extends Activity implements OnTouchListener{/** Called when the activity is first created. */int screenWidth;int screenHeight;int lastX;int lastY;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);DisplayMetrics dm = getResources().getDisplayMetrics();screenWidth = dm.widthPixels;screenHeight = dm.heightPixels - 50;Button button=(Button)findViewById(R.id.btn);ImageView imageView=(ImageView)findViewById(R.id.btn2);imageView.setOnTouchListener(this);button.setOnTouchListener(this);}@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubint action=event.getAction();Log.i("@@@@@@", "Touch:"+action);//Toast.makeText(DraftTest.this, "λ�ã�"+x+","+y, Toast.LENGTH_SHORT).show();switch(action){case MotionEvent.ACTION_DOWN:lastX = (int) event.getRawX();lastY = (int) event.getRawY();break;/** * layout(l,t,r,b) * l  Left position, relative to parent             t  Top position, relative to parent             r  Right position, relative to parent             b  Bottom position, relative to parent   * */case MotionEvent.ACTION_MOVE:int dx =(int)event.getRawX() - lastX;int dy =(int)event.getRawY() - lastY;int left = v.getLeft() + dx;int top = v.getTop() + dy;int right = v.getRight() + dx;int bottom = v.getBottom() + dy;if(left < 0){left = 0;right = left + v.getWidth();}if(right > screenWidth){right = screenWidth;left = right - v.getWidth();}if(top < 0){top = 0;bottom = top + v.getHeight();}if(bottom > screenHeight){bottom = screenHeight;top = bottom - v.getHeight();}v.layout(left, top, right, bottom);Log.i("@@@@@@", "position��" + left +", " + top + ", " + right + ", " + bottom);   lastX = (int) event.getRawX();lastY = (int) event.getRawY();break;case MotionEvent.ACTION_UP:break;        }return false;}}



高度減去50是減去狀態列和標題列的高度。

case MotionEvent.ACTION_DOWN:lastX = (int) event.getRawX();lastY = (int) event.getRawY();break;

然後擷取控制項一開始的位置,然後在ACTION_MOVIE中:

int dx =(int)event.getRawX() - lastX;int dy =(int)event.getRawY() - lastY;int left = v.getLeft() + dx;int top = v.getTop() + dy;int right = v.getRight() + dx;int bottom = v.getBottom() + dy;if(left < 0){left = 0;right = left + v.getWidth();}if(right > screenWidth){right = screenWidth;left = right - v.getWidth();}if(top < 0){top = 0;bottom = top + v.getHeight();}if(bottom > screenHeight){bottom = screenHeight;top = bottom - v.getHeight();}v.layout(left, top, right, bottom);Log.i("@@@@@@", "position��" + left +", " + top + ", " + right + ", " + bottom);   lastX = (int) event.getRawX();lastY = (int) event.getRawY();

getLeft()方法得到的是控制項座標距離父控制項原點(左上方,座標(0,0))的x軸距離,getReght()是控制項右邊距離父控制項原點的x軸距離,同理,getTop和getButtom是距離的y軸距離。

if(left < 0){left = 0;right = left + v.getWidth();}if(right > screenWidth){right = screenWidth;left = right - v.getWidth();}if(top < 0){top = 0;bottom = top + v.getHeight();}if(bottom > screenHeight){bottom = screenHeight;top = bottom - v.getHeight();}

這裡的判斷是為了是控制項不超出螢幕以外,即:到達邊界以後,不能再移動。

v.layout(left, top, right, bottom);

設定View的位置。

有一點忘記說了,就是像ImageView和TextView這些控制項,要想實現拖動,要在xml檔案中設定它的clickable為true。

android:clickable="true"

就這樣,這些就是這個demo的全部內容。

最後,是代碼的:


http://download.csdn.net/detail/aomandeshangxiao/4187376,

http://download.csdn.net/detail/aomandeshangxiao/4189910


聯繫我們

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