因?yàn)樽罱?xiàng)目的需要,需要實(shí)現(xiàn)一個(gè)拖動(dòng)效果,看了一下網(wǎng)上剛好有這種拖動(dòng)效果的demo,代碼大概如下:
十載的晉江網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整晉江建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“晉江網(wǎng)站設(shè)計(jì)”,“晉江網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
private void initListener(){ screenWidth = getScreenWidth(this);//獲取屏幕寬度 screenHeight = getScreenHeight(this) - getStatusHeight(MainActivity.this);//屏幕高度-狀態(tài)欄 testTv.setOnTouchListener(new View.OnTouchListener() { @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 top = v.getTop() + dy; int left = v.getLeft() + dx; if (top <= 0) { top = 0; } if (top >= screenHeight - testTv.getHeight() ) { top = screenHeight - testTv.getHeight(); } if (left >= screenWidth - testTv.getWidth()) { left = screenWidth - testTv.getWidth(); } if (left <= 0) { left = 0; } v.layout(left, top, left+v.getWidth(), top+v.getHeight()); v.postInvalidate(); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return true;//這里根據(jù)項(xiàng)目需要選擇false與true } }); }
可是我的項(xiàng)目本身是有涂鴉的界面,還有很多復(fù)雜的事件,發(fā)現(xiàn)會(huì)有很奇怪的效果,抖動(dòng)很厲害,而且時(shí)不時(shí)回到原來(lái)的位置,后來(lái)發(fā)現(xiàn)這個(gè)v.layout(left, top, left+v.getWidth(), top+v.getHeight());可能不適合項(xiàng)目的需求,所以我想了另一種解法。代碼如下:
private void initListener(){ screenWidth = getScreenWidth(this);//獲取屏幕寬度 screenHeight = getScreenHeight(this) - getStatusHeight(MainActivity.this);//屏幕高度-狀態(tài)欄 testTv.setOnTouchListener(new View.OnTouchListener() { @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 top = v.getTop() + dy; int left = v.getLeft() + dx; if (top <= 0) { top = 0; } if (top >= screenHeight - testTv.getHeight() ) { top = screenHeight - testTv.getHeight(); } if (left >= screenWidth - testTv.getWidth()) { left = screenWidth - testTv.getWidth(); } if (left <= 0) { left = 0; } RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight()); param.leftMargin = left; param.topMargin = top; v.setLayoutParams(param); // v.layout(left, top, left+v.getWidth(), top+v.getHeight()); v.postInvalidate(); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return true; } }); }
效果如下圖:
覺(jué)得可以了,不過(guò)要注意一些事項(xiàng):鍵盤彈出需要自己處理一下(比如處理成彈出鍵盤就不能移動(dòng)這些,不然會(huì)變形的),我覺(jué)得這種寫法可以應(yīng)用于各種復(fù)雜的場(chǎng)景。好了,今天一些項(xiàng)目問(wèn)題總結(jié)就到這里了,希望對(duì)大家有幫助,代碼也上傳了,地址如下:Android實(shí)現(xiàn)拖動(dòng)效果
以上所述是小編給大家介紹Android實(shí)現(xiàn)拖動(dòng)效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!