Android移动ImageView

前端之家收集整理的这篇文章主要介绍了Android移动ImageView前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在网上搜索了一个简单的解决方案来自由移动ImageView.我终于找到了一些产生完美结果的代码

  1. public class MainActivity extends Activity implements View.OnTouchListener {
  2. private int _xDelta;
  3. private int _yDelta;
  4. @Override
  5. public void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. ImageView j = (ImageView)findViewById(R.id.j);
  9. j.setOnTouchListener(this);
  10. }
  11. public boolean onTouch(View view,MotionEvent event) {
  12. final int X = (int) event.getRawX();
  13. final int Y = (int) event.getRawY();
  14. ImageView j = (ImageView)findViewById(R.id.j);
  15. switch (event.getAction() & MotionEvent.ACTION_MASK) {
  16. case MotionEvent.ACTION_DOWN:
  17. RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  18. _xDelta = (int) (X - j.getTranslationX());
  19. _yDelta = (int) (Y - j.getTranslationY());
  20. break;
  21. case MotionEvent.ACTION_UP:
  22. break;
  23. case MotionEvent.ACTION_POINTER_DOWN:
  24. break;
  25. case MotionEvent.ACTION_POINTER_UP:
  26. break;
  27. case MotionEvent.ACTION_MOVE:
  28. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
  29. j.setTranslationX(X - _xDelta);
  30. j.setTranslationY(Y - _yDelta);
  31. break;
  32. }
  33. return true;
  34. }}

由于我不知道我的代码是如何工作的,我想看看是否有更好的解决方案.

最佳答案
正如谷歌建议的那样,如果您定位Android 3.0及更高版本,则可以使用DragListener.

对于pre-Honeycomb版本,您可以使用以下内容

  1. // The ‘active pointer’ is the one currently moving our object.
  2. private int mActivePointerId = INVALID_POINTER_ID;
  3. @Override
  4. public boolean onTouchEvent(MotionEvent ev) {
  5. // Let the ScaleGestureDetector inspect all events.
  6. mScaleDetector.onTouchEvent(ev);
  7. final int action = MotionEventCompat.getActionMasked(ev);
  8. switch (action) {
  9. case MotionEvent.ACTION_DOWN: {
  10. final int pointerIndex = MotionEventCompat.getActionIndex(ev);
  11. final float x = MotionEventCompat.getX(ev,pointerIndex);
  12. final float y = MotionEventCompat.getY(ev,pointerIndex);
  13. // Remember where we started (for dragging)
  14. mLastTouchX = x;
  15. mLastTouchY = y;
  16. // Save the ID of this pointer (for dragging)
  17. mActivePointerId = MotionEventCompat.getPointerId(ev,0);
  18. break;
  19. }
  20. case MotionEvent.ACTION_MOVE: {
  21. // Find the index of the active pointer and fetch its position
  22. final int pointerIndex =
  23. MotionEventCompat.findPointerIndex(ev,mActivePointerId);
  24. final float x = MotionEventCompat.getX(ev,pointerIndex);
  25. final float y = MotionEventCompat.getY(ev,pointerIndex);
  26. // Calculate the distance moved
  27. final float dx = x - mLastTouchX;
  28. final float dy = y - mLastTouchY;
  29. mPosX += dx;
  30. mPosY += dy;
  31. invalidate();
  32. // Remember this touch position for the next move event
  33. mLastTouchX = x;
  34. mLastTouchY = y;
  35. break;
  36. }
  37. case MotionEvent.ACTION_UP: {
  38. mActivePointerId = INVALID_POINTER_ID;
  39. break;
  40. }
  41. case MotionEvent.ACTION_CANCEL: {
  42. mActivePointerId = INVALID_POINTER_ID;
  43. break;
  44. }
  45. case MotionEvent.ACTION_POINTER_UP: {
  46. final int pointerIndex = MotionEventCompat.getActionIndex(ev);
  47. final int pointerId = MotionEventCompat.getPointerId(ev,pointerIndex);
  48. if (pointerId == mActivePointerId) {
  49. // This was our active pointer going up. Choose a new
  50. // active pointer and adjust accordingly.
  51. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  52. mLastTouchX = MotionEventCompat.getX(ev,newPointerIndex);
  53. mLastTouchY = MotionEventCompat.getY(ev,newPointerIndex);
  54. mActivePointerId = MotionEventCompat.getPointerId(ev,newPointerIndex);
  55. }
  56. break;
  57. }
  58. }
  59. return true;
  60. }

正如谷歌here所建议的那样.

猜你在找的Android相关文章