Guest User

Untitled

a guest
Jan 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. class clsTarget : ImageView, ImageView.IOnTouchListener
  2. {
  3. private ScaleGestureDetector mScaleDetector;
  4. private ScaleListener sListener;
  5. private static float mScaleFactor = 0.6F;
  6. private static float scalePointX;
  7. private static float scalePointY;
  8.  
  9. private static int INVALID_POINTER_ID = -1;
  10. private int mActivePointerId = INVALID_POINTER_ID;
  11. private static float mPosX, mPosY, mLastTouchX, mLastTouchY;
  12.  
  13. public clsTarget(Context context)
  14. : base(context)
  15. {
  16. this.SetOnTouchListener(this);
  17.  
  18. sListener = new ScaleListener();
  19. mScaleDetector = new ScaleGestureDetector(context, sListener);
  20. mScaleFactor = 800F / (float)Math.Min(Resources.DisplayMetrics.WidthPixels, Resources.DisplayMetrics.HeightPixels);
  21. }
  22.  
  23. protected override void OnDraw(Android.Graphics.Canvas canvas)
  24. {
  25. canvas.Save();
  26.  
  27. canvas.Scale(mScaleFactor, mScaleFactor, scalePointX, scalePointY);
  28. canvas.Translate(mPosX, mPosY);
  29.  
  30. Paint p = new Paint(PaintFlags.AntiAlias);
  31. p.Color = Color.Orange;
  32. p.SetStyle(Paint.Style.Fill);
  33. p.StrokeWidth = 1F;
  34. canvas.DrawCircle(400, 400, 200, p);
  35.  
  36. canvas.Restore();
  37. }
  38.  
  39. public bool OnTouch(View v, MotionEvent e)
  40. {
  41. mScaleDetector.OnTouchEvent(e);
  42.  
  43. switch (e.Action & MotionEventActions.Mask)
  44. {
  45. case MotionEventActions.Down:
  46. float x = (e.GetX() - scalePointX) / mScaleFactor;
  47. float y = (e.GetY() - scalePointY) / mScaleFactor;
  48.  
  49. mLastTouchX = x;
  50. mLastTouchY = y;
  51.  
  52. mActivePointerId = e.GetPointerId(0);
  53. break;
  54.  
  55. case MotionEventActions.Move:
  56. int pointerIndex = e.FindPointerIndex(mActivePointerId);
  57.  
  58. float x2 = (e.GetX(pointerIndex) - scalePointX) / mScaleFactor;
  59. float y2 = (e.GetY(pointerIndex) - scalePointY) / mScaleFactor;
  60.  
  61. float dx = (x2 - mLastTouchX);
  62. float dy = (y2 - mLastTouchY);
  63.  
  64. if (!mScaleDetector.IsInProgress)
  65. {
  66. mPosX += dx;
  67. mPosY += dy;
  68.  
  69. mLastTouchX = x2;
  70. mLastTouchY = y2;
  71. }
  72.  
  73. this.Invalidate();
  74.  
  75. break;
  76.  
  77. case MotionEventActions.Up:
  78. mActivePointerId = INVALID_POINTER_ID;
  79. break;
  80.  
  81. case MotionEventActions.Cancel:
  82. mActivePointerId = INVALID_POINTER_ID;
  83. break;
  84.  
  85. case MotionEventActions.PointerUp:
  86. int pointerIndex2 = (int)(e.Action & MotionEventActions.PointerIndexMask) >> (int)MotionEventActions.PointerIndexShift;
  87. int pointerID = e.GetPointerId(pointerIndex2);
  88.  
  89. if (pointerID == mActivePointerId)
  90. {
  91. int newPointerIndex = pointerIndex2 == 0 ? 1 : 0;
  92.  
  93. mLastTouchX = (e.GetX(newPointerIndex) - scalePointX) / mScaleFactor;
  94. mLastTouchY = (e.GetY(newPointerIndex) - scalePointY) / mScaleFactor;
  95.  
  96. mActivePointerId = e.GetPointerId(newPointerIndex);
  97. }
  98.  
  99. break;
  100. }
  101.  
  102. return true;
  103. }
  104.  
  105. private class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener
  106. {
  107. public override bool OnScale(ScaleGestureDetector detector)
  108. {
  109. scalePointX = detector.FocusX;
  110. scalePointY = detector.FocusY;
  111.  
  112. mScaleFactor *= detector.ScaleFactor;
  113. mScaleFactor = Math.Max(0.5f, Math.Min(mScaleFactor, 7.0f));
  114.  
  115. return true;
  116. }
  117. }
  118. }
Add Comment
Please, Sign In to add comment