final View cv = new View(this); setContentView(cv); TextView tv = new TextView(this); tv.setBackgroundColor(0xffeeeeee); tv.setTextColor(0xff000000); tv.setTextSize(24); tv.setText("click me then drag me"); tv.setPadding(8, 8, 8, 8); mPopup = new PopupWindow(tv, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
OnTouchListener otl = new OnTouchListener() { private float mDx; private float mDy; @Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_DOWN) { mDx = mCurrentX - event.getRawX(); mDy = mCurrentY - event.getRawY(); } else if (action == MotionEvent.ACTION_MOVE) { mCurrentX = (int) (event.getRawX() + mDx); mCurrentY = (int) (event.getRawY() + mDy); mPopup.update(mCurrentX, mCurrentY, -1, -1); } return true; } }; tv.setOnTouchListener(otl); mCurrentX = 20; mCurrentY = 50; cv.post(new Runnable() { @Override public void run() { mPopup.showAtLocation(cv, Gravity.NO_GRAVITY, mCurrentX, mCurrentY); } });
或者:
layout_counter.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { if (currentState != State.EDIT_MOVE) return false; FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams(); if (view.getId() != R.id.layout_counter) return false; switch (event.getAction()) { case MotionEvent.ACTION_MOVE: params.topMargin = (int) event.getRawY() - view.getHeight(); params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2); view.setLayoutParams(params); break; case MotionEvent.ACTION_UP: params.topMargin = (int) event.getRawY() - view.getHeight(); params.leftMargin = (int) event.getRawX() - (view.getWidth() / 2); view.setLayoutParams(params); break; case MotionEvent.ACTION_DOWN: view.setLayoutParams(params); break; } return true; } });
或者:
public class MyActivity extends Activity implements View.OnTouchListener { TextView _view; ViewGroup _root; private int _xDelta; private int _yDelta; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); _root = (ViewGroup)findViewById(R.id.root); _view = new TextView(this); _view.setText("TextView!!!!!!!!"); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 50); layoutParams.leftMargin = 50; layoutParams.topMargin = 50; layoutParams.bottomMargin = -250; layoutParams.rightMargin = -250; _view.setLayoutParams(layoutParams); _view.setOnTouchListener(this); _root.addView(_view); } public boolean onTouch(View view, MotionEvent event) { final int X = (int) event.getRawX(); final int Y = (int) event.getRawY(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); _xDelta = X - lParams.leftMargin; _yDelta = Y - lParams.topMargin; break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_POINTER_UP: break; case MotionEvent.ACTION_MOVE: RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); layoutParams.leftMargin = X - _xDelta; layoutParams.topMargin = Y - _yDelta; layoutParams.rightMargin = -250; layoutParams.bottomMargin = -250; view.setLayoutParams(layoutParams); break; } _root.invalidate(); return true; }}