Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. public class DragDrop extends Activity{
  2.  
  3.  
  4. /** Called when the activity is first created. */
  5. @Override
  6. public void onCreate(Bundle savedInstanceState)
  7. {
  8. super.onCreate(savedInstanceState);
  9. //for no title
  10. this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  11. this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  12.  
  13. // draw the view
  14. setContentView(new DrawView(this));
  15.  
  16.  
  17. }
  18.  
  19. public class DrawView extends View
  20. {
  21. Context context;
  22. private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
  23. private int balID = 0; // variable to know what ball is being dragged
  24.  
  25. int sx,sy,tx,ty;
  26. Bitmap aa,pp,ppp,ll,ee,trans,nn,tt;
  27.  
  28. int a;
  29.  
  30. Point point1 = new Point();
  31. Point point2 = new Point();
  32. Point point3 = new Point();
  33.  
  34. public DrawView(Context context)
  35. {
  36. super(context);
  37. setFocusable(true); //necessary for getting the touch events
  38. //getting the screen size
  39.  
  40. Display display = getWindowManager().getDefaultDisplay();
  41.  
  42. sx = display.getWidth();
  43. sy = display.getHeight();
  44.  
  45. a=R.drawable.a;
  46. trans=BitmapFactory.decodeResource(getResources(), R.drawable.trans);
  47.  
  48. aa=BitmapFactory.decodeResource(getResources(), R.drawable.aa);
  49. nn=BitmapFactory.decodeResource(getResources(), R.drawable.nn);
  50. tt=BitmapFactory.decodeResource(getResources(), R.drawable.tt);
  51.  
  52.  
  53. // setting the start point for the balls
  54.  
  55. point1.x = sx-200;
  56. point1.y = 250;
  57.  
  58. point2.x = sx/2-50;
  59. point2.y = 250;
  60.  
  61. point3.x = 100;
  62. point3.y = 250;
  63.  
  64. // declare each ball with the ColorBall class
  65. colorballs[0] = new ColorBall(context,a, point1);
  66. colorballs[1] = new ColorBall(context,R.drawable.n, point2);
  67. colorballs[2] = new ColorBall(context,R.drawable.t, point3);
  68.  
  69.  
  70. }
  71.  
  72. // the method that draws the balls
  73. @Override protected void onDraw(Canvas canvas)
  74. {
  75.  
  76. canvas.drawBitmap(tt,100, 50, null);
  77. canvas.drawBitmap(nn,sx/2-50, 50, null);
  78. canvas.drawBitmap(aa,sx-200, 50, null);
  79.  
  80. if(set_a)
  81. {
  82. a=R.drawable.trans;
  83. }
  84.  
  85. //draw the balls on the canvas
  86. for (ColorBall ball : colorballs)
  87. {
  88.  
  89. tx=ball.getX();
  90. ty=ball.getY();
  91.  
  92. canvas.drawBitmap(ball.getBitmap(),tx, ty, null);
  93.  
  94. if(tx>sx/2-50 && tx<((sx/2)-50)+20 && ty>50 && ty<70 && balID==2)
  95. {
  96. nn=BitmapFactory.decodeResource(getResources(), R.drawable.n);
  97. }
  98.  
  99. if(tx>sx-200 && tx<(sx-200)+20 && ty>50 && ty<=75 && balID==1)
  100. {
  101. aa=BitmapFactory.decodeResource(getResources(), R.drawable.a1);
  102. set_a=true;
  103. }
  104.  
  105. if(tx>100 && tx<120 && ty>50 && ty<=75 && balID==3)
  106. {
  107. tt=BitmapFactory.decodeResource(getResources(), R.drawable.t);
  108. }
  109.  
  110. }
  111.  
  112.  
  113. }
  114.  
  115. // events when touching the screen
  116. public boolean onTouchEvent(MotionEvent event)
  117. {
  118. int eventaction = event.getAction();
  119.  
  120. int X = (int)event.getX();
  121. int Y = (int)event.getY();
  122.  
  123. switch (eventaction )
  124. {
  125.  
  126. case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
  127. balID = 0;
  128. for (ColorBall ball : colorballs)
  129. {
  130. // check if inside the bounds of the ball (circle)
  131. // get the center for the ball
  132. int centerX = ball.getX() + 25;
  133. int centerY = ball.getY() + 25;
  134.  
  135. // calculate the radius from the touch to the center of the ball
  136. double radCircle = Math.sqrt( (double) (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));
  137.  
  138. // if the radius is smaller then 23 (radius of a ball is 22), then it must be on the ball
  139. if (radCircle < 23)
  140. {
  141. balID = ball.getID();
  142. break;
  143. }
  144.  
  145. // check all the bounds of the ball (square)
  146. //if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
  147. // balID = ball.getID();
  148. // break;
  149. //}
  150. }
  151.  
  152. break;
  153.  
  154.  
  155. case MotionEvent.ACTION_MOVE: // touch drag with the ball
  156. // move the balls the same as the finger
  157. if (balID > 0)
  158. {
  159. colorballs[balID-1].setX(X-25);
  160. colorballs[balID-1].setY(Y-25);
  161. }
  162.  
  163. break;
  164.  
  165. case MotionEvent.ACTION_UP:
  166. // touch drop - just do things here after dropping
  167.  
  168. break;
  169. }
  170. // redraw the canvas
  171. invalidate();
  172. return true;
  173.  
  174. }
  175. }
  176.  
  177. }
  178.  
  179. public class ColorBall{
  180. private Bitmap img; // the image of the ball
  181. private int coordX = 0; // the x coordinate at the canvas
  182. private int coordY = 0; // the y coordinate at the canvas
  183. private int id; // gives every ball his own id, for now not necessary
  184. private static int count = 1;
  185. private boolean goRight = true;
  186. private boolean goDown = true;
  187.  
  188. public ColorBall(Context context, int drawable)
  189. {
  190.  
  191. BitmapFactory.Options opts = new BitmapFactory.Options();
  192. opts.inJustDecodeBounds = true;
  193. img = BitmapFactory.decodeResource(context.getResources(), drawable);
  194. id=count;
  195. count++;
  196.  
  197. }
  198.  
  199. public ColorBall(Context context, int drawable, Point point)
  200. {
  201.  
  202. BitmapFactory.Options opts = new BitmapFactory.Options();
  203. opts.inJustDecodeBounds = true;
  204. img = BitmapFactory.decodeResource(context.getResources(), drawable);
  205. id=count;
  206. count++;
  207. coordX= point.x;
  208. coordY = point.y;
  209.  
  210. }
  211.  
  212. public static int getCount()
  213. {
  214. return count;
  215. }
  216.  
  217. void setX(int newValue)
  218. {
  219. coordX = newValue;
  220. }
  221.  
  222. public int getX()
  223. {
  224. return coordX;
  225. }
  226.  
  227. void setY(int newValue)
  228. {
  229. coordY = newValue;
  230. }
  231.  
  232. public int getY()
  233. {
  234. return coordY;
  235. }
  236.  
  237. public int getID()
  238. {
  239. return id;
  240. }
  241.  
  242. public Bitmap getBitmap()
  243. {
  244. return img;
  245. }
  246.  
  247. public void moveBall(int goX, int goY)
  248. {
  249. // check the borders, and set the direction if a border has reached
  250. if (coordX > 270){
  251. goRight = false;
  252. }
  253. if (coordX < 0){
  254. goRight = true;
  255. }
  256. if (coordY > 400){
  257. goDown = false;
  258. }
  259. if (coordY < 0){
  260. goDown = true;
  261. }
  262. // move the x and y
  263. if (goRight){
  264. coordX += goX;
  265. }else
  266. {
  267. coordX -= goX;
  268. }
  269. if (goDown){
  270. coordY += goY;
  271. }else
  272. {
  273. coordY -= goY;
  274. }
  275.  
  276. }
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement