Posted By

sarrory on 08/16/14


Tagged

dfd


Versions (?)

2y 7aga


 / Published in: Java
 

jfj

  1. import android.os.Bundle;
  2. import android.app.Activity;
  3. import android.view.MotionEvent;
  4. import android.view.View;
  5. import android.view.View.OnTouchListener;
  6. import android.view.animation.Animation;
  7. import android.view.animation.Animation.AnimationListener;
  8. import android.view.animation.TranslateAnimation;
  9. import android.widget.ImageView;
  10. import android.widget.LinearLayout;
  11.  
  12.  
  13. public class MainActivity extends Activity {
  14.  
  15. ImageView imageView;
  16. ImageView imageView2;
  17.  
  18.  
  19. private float mLastTouchX;
  20. private float mLastTouchY;
  21.  
  22. private static final int INVALID_POINTER_ID = -1;
  23.  
  24. private int move = 0;
  25. public int direction = 0;
  26.  
  27. // The �active pointer� is the one currently moving our object.
  28. private int mActivePointerId = INVALID_POINTER_ID;
  29.  
  30. /** ************************************************************************************/
  31.  
  32. private float mLastTouchX2;
  33. private float mLastTouchY2;
  34.  
  35. private static final int INVALID_POINTER_ID2 = -1;
  36.  
  37. private int move2 = 0;
  38. public int direction2 = 0;
  39.  
  40. // The �active pointer� is the one currently moving our object.
  41. private int mActivePointerId2 = INVALID_POINTER_ID2;
  42.  
  43. LinearLayout.LayoutParams mParams;
  44. LinearLayout.LayoutParams mParams2;
  45.  
  46. @Override
  47. public void onCreate(Bundle savedInstanceState) {
  48. super.onCreate(savedInstanceState);
  49. setContentView(R.layout.activity_main);
  50.  
  51.  
  52. imageView = (ImageView) findViewById(R.id.image1);
  53. imageView2 = (ImageView) findViewById(R.id.image2);
  54. mParams = (LinearLayout.LayoutParams) imageView.getLayoutParams();
  55. mParams2 = (LinearLayout.LayoutParams) imageView2.getLayoutParams();
  56.  
  57.  
  58.  
  59. imageView.setOnTouchListener(new OnTouchListener() {
  60.  
  61. @Override
  62. public boolean onTouch(View v, MotionEvent ev) {
  63.  
  64. final int action = ev.getAction();
  65.  
  66.  
  67. switch (action & MotionEvent.ACTION_MASK) {
  68. case MotionEvent.ACTION_DOWN: {
  69. final float x = ev.getX();
  70. final float y = ev.getY();
  71.  
  72. mLastTouchX = x;
  73. mLastTouchY = y;
  74.  
  75. // Save the ID of this pointer
  76. mActivePointerId = ev.getPointerId(0);
  77.  
  78. break;
  79. }
  80.  
  81. case MotionEvent.ACTION_MOVE: {
  82. // Find the index of the active pointer and fetch its
  83. // position
  84. final int pointerIndex = ev.findPointerIndex(mActivePointerId);
  85. final float x = ev.getX(pointerIndex);
  86. final float y = ev.getY(pointerIndex);
  87.  
  88. final float dx = x - mLastTouchX;
  89. final float dy = y - mLastTouchY;
  90.  
  91. if (move == 0) {
  92. if (Math.abs(dx) >= Math.abs(dy)) {
  93. move = 1;
  94. if (dx > 0){
  95. int fromX =mParams.leftMargin;
  96. int toX =mParams.leftMargin+60;
  97. int fromY =mParams.topMargin;
  98. int toY =mParams.topMargin;
  99.  
  100. Animation anima = new TranslateAnimation(fromX, toX, fromY, toY);
  101. anima.setDuration(500);
  102. anima.setRepeatCount( 0 );
  103. anima.setFillAfter(true);
  104. imageView.startAnimation(anima);
  105. anima.setAnimationListener(new AnimationListener() {
  106.  
  107. @Override
  108. public void onAnimationStart(Animation arg0) {
  109. // TODO Auto-generated method stub
  110.  
  111. }
  112.  
  113. @Override
  114. public void onAnimationRepeat(Animation arg0) {
  115. // TODO Auto-generated method stub
  116.  
  117. }
  118.  
  119. @Override
  120. public void onAnimationEnd(Animation arg0) {
  121. // TODO Auto-generated method stub
  122. imageView.clearAnimation();
  123. mParams.leftMargin = mParams.leftMargin + 60;
  124. imageView.setLayoutParams(mParams);
  125.  
  126. }
  127. });
  128.  
  129. int fromX2 =mParams2.leftMargin;
  130. int toX2= mParams2.leftMargin-60;
  131. int fromY2 =mParams2.topMargin;
  132. int toY2 = mParams2.topMargin;
  133.  
  134.  
  135. Animation anima2 = new TranslateAnimation(fromX2, toX2, fromY2, toY2);
  136. imageView2.startAnimation(anima2);
  137. anima2.setDuration(500);
  138. anima2.setRepeatCount( 0 );
  139. anima2.setFillAfter(true);
  140. anima2.setAnimationListener(new AnimationListener() {
  141.  
  142. @Override
  143. public void onAnimationStart(Animation animation) {
  144. // TODO Auto-generated method stub
  145.  
  146. }
  147.  
  148. @Override
  149. public void onAnimationRepeat(Animation animation) {
  150. // TODO Auto-generated method stub
  151.  
  152. }
  153.  
  154. @Override
  155. public void onAnimationEnd(Animation animation) {
  156. // TODO Auto-generated method stub
  157. imageView2.clearAnimation();
  158. mParams2.leftMargin = mParams.leftMargin - 60;
  159. imageView2.setLayoutParams(mParams2);
  160.  
  161. }
  162. });
  163.  
  164. }else{
  165. mParams.leftMargin = mParams.leftMargin - 60;
  166. imageView.setLayoutParams(mParams);
  167. }
  168.  
  169. }else {
  170. move = -1;
  171. if (dy > 0) {
  172. mParams.topMargin = mParams.topMargin + 60;
  173. imageView.setLayoutParams(mParams);
  174. } else {
  175. mParams.topMargin = mParams.topMargin - 60;
  176. imageView.setLayoutParams(mParams);
  177. }
  178. }
  179. }
  180.  
  181.  
  182. //imageView2.setLayoutParams(mParams2);
  183.  
  184. break;
  185. }
  186.  
  187. case MotionEvent.ACTION_UP: {
  188.  
  189. // mLastTouchX = mParams.leftMargin;
  190. // mLastTouchY = mParams.topMargin;
  191. move = 0;
  192. mActivePointerId = INVALID_POINTER_ID;
  193. // imageView.setLayoutParams(mParams);
  194. break;
  195. }
  196.  
  197. case MotionEvent.ACTION_CANCEL: {
  198. mActivePointerId = INVALID_POINTER_ID;
  199. break;
  200. }
  201.  
  202. case MotionEvent.ACTION_POINTER_UP: {
  203. // Extract the index of the pointer that left the touch
  204. // sensor
  205. final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  206. final int pointerId = ev.getPointerId(pointerIndex);
  207. if (pointerId == mActivePointerId) {
  208. // This was our active pointer going up. Choose a new
  209. // active pointer and adjust accordingly.
  210. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  211. mLastTouchX = ev.getX(newPointerIndex);
  212. mLastTouchY = ev.getY(newPointerIndex);
  213. mActivePointerId = ev.getPointerId(newPointerIndex);
  214. }
  215. break;
  216. }
  217. }
  218.  
  219. return true;
  220.  
  221. }
  222.  
  223.  
  224. });
  225. imageView2.setOnTouchListener(new OnTouchListener() {
  226. @Override
  227. public boolean onTouch(View v, MotionEvent ev) {
  228.  
  229. final int action = ev.getAction();
  230.  
  231. switch (action & MotionEvent.ACTION_MASK) {
  232. case MotionEvent.ACTION_DOWN: {
  233. final float x = ev.getX();
  234. final float y = ev.getY();
  235.  
  236. mLastTouchX2 = x;
  237. mLastTouchY2 = y;
  238.  
  239. // Save the ID of this pointer
  240. mActivePointerId2 = ev.getPointerId(0);
  241.  
  242. break;
  243. }
  244.  
  245. case MotionEvent.ACTION_MOVE: {
  246. // Find the index of the active pointer and fetch its
  247. // position
  248. final int pointerIndex = ev
  249. .findPointerIndex(mActivePointerId2);
  250. final float x = ev.getX(pointerIndex);
  251. final float y = ev.getY(pointerIndex);
  252.  
  253. final float dx = x - mLastTouchX2;
  254. final float dy = y - mLastTouchY2;
  255.  
  256. if (move2 == 0) {
  257. if (Math.abs(dx) >= Math.abs(dy)) {
  258. move2 = 1;
  259. if (dx > 0){
  260. mParams2.leftMargin = mParams2.leftMargin +60;
  261. }else{
  262. mParams2.leftMargin = mParams2.leftMargin - 60;
  263. }
  264.  
  265. }else {
  266. move2 = -1;
  267. if (dy > 0) {
  268. mParams2.topMargin = mParams2.topMargin + 60;
  269. } else {
  270. mParams2.topMargin = mParams2.topMargin - 60;
  271. }
  272. }
  273. }
  274.  
  275. imageView2.setLayoutParams(mParams2);
  276. //imageView2.setLayoutParams(mParams2);
  277.  
  278.  
  279. break;
  280. }
  281.  
  282.  
  283.  
  284. case MotionEvent.ACTION_UP: {
  285.  
  286. // mLastTouchX = mParams.leftMargin;
  287. // mLastTouchY = mParams.topMargin;
  288. move2 = 0;
  289. mActivePointerId2 = INVALID_POINTER_ID2;
  290. // imageView.setLayoutParams(mParams);
  291. break;
  292. }
  293.  
  294. case MotionEvent.ACTION_CANCEL: {
  295. mActivePointerId2 = INVALID_POINTER_ID2;
  296. break;
  297. }
  298.  
  299. case MotionEvent.ACTION_POINTER_UP: {
  300. // Extract the index of the pointer that left the touch
  301. // sensor
  302. final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  303. final int pointerId = ev.getPointerId(pointerIndex);
  304. if (pointerId == mActivePointerId2) {
  305. // This was our active pointer going up. Choose a new
  306. // active pointer and adjust accordingly.
  307. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  308. mLastTouchX2 = ev.getX(newPointerIndex);
  309. mLastTouchY2 = ev.getY(newPointerIndex);
  310. mActivePointerId2 = ev.getPointerId(newPointerIndex);
  311. }
  312. break;
  313. }
  314. }
  315.  
  316. return true;
  317.  
  318. }
  319.  
  320.  
  321. });
  322.  
  323. // setContentView(imageView);
  324. }
  325. }

Report this snippet  

You need to login to post a comment.