Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. public class MainActivity extends Activity {
  2. ImageView imageView;
  3. ImageView dragHandle;
  4. RelativeLayout layout;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. imageView = (ImageView) findViewById(R.id.imageView1);
  11. imageView.setBackgroundColor(Color.MAGENTA);
  12. dragHandle = (ImageView) findViewById(R.id.imageView2);
  13. dragHandle.setBackgroundColor(Color.CYAN);
  14. layout = (RelativeLayout) findViewById(R.id.relativeLayout2);
  15. layout.setBackgroundColor(Color.YELLOW);
  16. setUpResize();
  17.  
  18. }
  19.  
  20. public void setUpResize() {
  21. dragHandle.setOnTouchListener(new View.OnTouchListener() {
  22.  
  23. float centerX, centerY, startR, startScale, startX, startY;
  24.  
  25. float startAngle;
  26. float zeroAngle;
  27. int firstPointX;
  28. int firstPointY;
  29.  
  30. public boolean onTouch(View v, MotionEvent e) {
  31.  
  32. if (e.getAction() == MotionEvent.ACTION_DOWN) {
  33.  
  34. // calculate center of image
  35. centerX = (imageView.getLeft() + imageView.getRight()) / 2f;
  36. centerY = (imageView.getTop() + imageView.getBottom()) / 2f;
  37.  
  38. // recalculate coordinates of starting point
  39. startX = e.getRawX() - dragHandle.getX() + centerX;
  40. startY = e.getRawY() - dragHandle.getY() + centerY;
  41.  
  42. // get starting distance and scale
  43. startR = (float) Math.hypot(e.getRawX() - startX, e.getRawY() - startY);
  44. startScale = imageView.getScaleX();
  45.  
  46. /*
  47. * Rotate code
  48. */
  49.  
  50. int[] locationOfLayout = new int[2];
  51. int[] locationOfDrag = new int[2];
  52.  
  53. layout.getLocationOnScreen(locationOfLayout);
  54. dragHandle.getLocationOnScreen(locationOfDrag);
  55.  
  56. firstPointX = locationOfLayout[0];
  57. firstPointY = locationOfLayout[1];
  58.  
  59. float secondPointX = e.getRawX();
  60. float secondPointY = e.getRawY();
  61.  
  62. zeroAngle = findRotation(firstPointX, firstPointY, secondPointX, secondPointY); // remember
  63. // "zero"
  64. // angle
  65. startAngle = layout.getRotation(); // remember angle at
  66. // which layout is
  67. // rotated at the start
  68.  
  69. } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
  70.  
  71. // calculate new distance
  72. float newR = (float) Math.hypot(e.getRawX() - startX, e.getRawY() - startY);
  73.  
  74. // set new scale
  75. float newScale = newR / startR * startScale;
  76. imageView.setScaleX(newScale);
  77. imageView.setScaleY(newScale);
  78.  
  79. // move handler image
  80. dragHandle.setX(centerX + imageView.getWidth() / 2f * newScale);
  81. dragHandle.setY(centerY + imageView.getHeight() / 2f * newScale);
  82.  
  83. /*
  84. * Rotate code
  85. */
  86.  
  87. layout.setRotation(findRotation(firstPointX, firstPointY, e.getRawX(), e.getRawY()) - zeroAngle
  88. + startAngle); // rotate relative to start and zero
  89. // angle
  90.  
  91. } else if (e.getAction() == MotionEvent.ACTION_UP) {
  92.  
  93. }
  94. return true;
  95. }
  96. });
  97. }
  98.  
  99. private float findRotation(float firstPointX, float firstPointY, float secondPointX, float secondPointY) {
  100. double delta_x = (secondPointX - firstPointX);
  101. double delta_y = (secondPointY - firstPointY);
  102. double radians = Math.atan2(delta_y, delta_x);
  103. return (float) Math.toDegrees(radians);
  104. }
  105. }
  106.  
  107. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  108. xmlns:tools="http://schemas.android.com/tools"
  109. android:layout_width="match_parent"
  110. android:layout_height="match_parent" >
  111.  
  112. <RelativeLayout
  113. android:id="@+id/relativeLayout2"
  114. android:layout_width="match_parent"
  115. android:layout_height="match_parent"
  116. android:layout_centerInParent="true" >
  117.  
  118. <ImageView
  119. android:id="@+id/imageView1"
  120. android:layout_width="wrap_content"
  121. android:layout_height="wrap_content"
  122. android:layout_centerHorizontal="true"
  123. android:layout_centerVertical="true"
  124. android:src="@drawable/ic_launcher" />
  125.  
  126. <ImageView
  127. android:id="@+id/imageView2"
  128. android:layout_width="20dp"
  129. android:layout_height="20dp"
  130. android:layout_below="@+id/imageView1"
  131. android:layout_toRightOf="@+id/imageView1"
  132. android:src="@drawable/meanicons" />
  133.  
  134. </RelativeLayout>
  135.  
  136. </RelativeLayout>
  137.  
  138. public void setUpResize() {
  139. dragHandle.setOnTouchListener(new View.OnTouchListener() {
  140.  
  141. float centerX, centerY, startR, startScale, startX, startY, startRotation, startA ;
  142.  
  143. public boolean onTouch(View v, MotionEvent e) {
  144.  
  145. if (e.getAction() == MotionEvent.ACTION_DOWN) {
  146.  
  147. centerX = (imageView.getLeft() + imageView.getRight()) / 2f;
  148. centerY = (imageView.getTop() + imageView.getBottom()) / 2f;
  149.  
  150. startX = e.getRawX() - dragHandle.getX() + centerX;
  151. startY = e.getRawY() - dragHandle.getY() + centerY;
  152.  
  153. startR = (float) Math.hypot(e.getRawX() - startX, e.getRawY() - startY);
  154. startA = (float) Math.toDegrees(Math.atan2(e.getRawY() - startY, e.getRawX() - startX));
  155.  
  156. startScale = imageView.getScaleX();
  157. startRotation = imageView.getRotation();
  158.  
  159. } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
  160.  
  161. float newR = (float) Math.hypot(e.getRawX() - startX, e.getRawY() - startY);
  162. float newA = (float) Math.toDegrees(Math.atan2(e.getRawY() - startY, e.getRawX() - startX));
  163. float newScale = newR / startR * startScale;
  164. float newRotation = newA - startA + startRotation;
  165.  
  166. imageView.setScaleX(newScale);
  167. imageView.setScaleY(newScale);
  168. imageView.setRotation(newRotation);
  169.  
  170.  
  171. // ----- this part takes some effort to understand... ------
  172. dragHandle.setX((float) (centerX + Math.hypot(imageView.getWidth(), imageView.getHeight())/2f * newScale
  173. * Math.cos(Math.toRadians(newRotation) + Math.atan2(imageView.getHeight(), imageView.getWidth()))));
  174.  
  175. dragHandle.setY((float) (centerY + Math.hypot(imageView.getWidth(), imageView.getHeight())/2f * newScale
  176. * Math.sin(Math.toRadians(newRotation) + Math.atan2(imageView.getHeight(), imageView.getWidth()))));
  177. //-----------------------------------------------------------
  178.  
  179. dragHandle.setPivotX(0);
  180. dragHandle.setPivotY(0);
  181. dragHandle.setRotation(newRotation);
  182.  
  183.  
  184. } else if (e.getAction() == MotionEvent.ACTION_UP) {
  185.  
  186. }
  187. return true;
  188. }
  189. });
  190. }
  191.  
  192. Math.hypot(imageView.getWidth(), imageView.getHeight()) / 2f * newScale
  193.  
  194. Math.atan2(imageView.getHeight(), imageView.getWidth())
  195.  
  196. Math.cos(Math.toRadians(newRotation) + Math.atan2(imageView.getHeight(), imageView.getWidth()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement