因?yàn)樽罱?xiàng)目的需要,需要實(shí)現(xiàn)一個(gè)拖動(dòng)效果,看了一下網(wǎng)上剛好有這種拖動(dòng)效果的demo,代碼大概如下:
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供姑蘇企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)制作、網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為姑蘇眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。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 } }); }