開發安卓應用,你需要先安裝 eclipse 和 jdk 和 sdk ,然後建立安卓手機模擬器。這裡就不多說了。
安卓的拖曳功能在許多場景中都會出現,那麼如何實現這一功能,經本人修改調試通過。
程式碼如下:
先在試圖介面上建立imageView1與imageView2兩個實例,再將下面程式碼貼入java類別檔案
private int screenWidth; private int screenHeight; private ImageView img1; private ImageView img2; img1 = (ImageView) findViewById(R.id.imageView1); img2 = (ImageView) findViewById(R.id.imageView2); DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels - 50; img1.setOnTouchListener(movingEventListener); img2.setOnTouchListener(movingEventListener); private OnTouchListener movingEventListener = new OnTouchListener() { int lastX, lastY; @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; 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); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return true; } };
是不是已經成功了,歡迎來php中文網學習程式設計
文章網址:http://www.php.cn/java-article-375823.html
______________ PHP中文網_______________
以上是安卓實現拖曳功能~整理~親測可用的詳細內容。更多資訊請關注PHP中文網其他相關文章!