Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
  2. Context mContext;
  3.  
  4. private BoardThread thread;
  5. private float box_x = 140;
  6. private float box_y = 378;
  7. ImageView i = (ImageView) findViewById(R.id.box_view);
  8.  
  9. Bitmap box =
  10. (BitmapFactory.decodeResource
  11. (getResources(), R.drawable.box));
  12. private float boxWidth = box.getWidth();
  13. private float boxHeight = box.getHeight();
  14.  
  15.  
  16.  
  17. public BoardView(Context context){
  18. super(context);
  19. //surfaceHolder provides canvas that we draw on
  20. getHolder().addCallback(this);
  21. // controls drawings
  22. thread = new BoardThread(getHolder(),this);
  23. //intercepts touch events
  24. setFocusable(true);
  25.  
  26. }
  27.  
  28.  
  29. @Override
  30.  
  31. public void onDraw(Canvas canvas){
  32. canvas.drawColor(Color.WHITE);
  33.  
  34.  
  35. //draw box and set start location
  36. canvas.drawBitmap(box, box_x - (boxWidth/2),
  37. box_y - (boxHeight/2), null);
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. }
  45.  
  46. @Override
  47. public boolean onTouchEvent(MotionEvent event){
  48.  
  49. //boolean mode = false;
  50.  
  51. if(event.getAction() == MotionEvent.ACTION_DOWN){
  52. //int x = (int)event.getX();
  53. //int y = (int)event.getY();
  54.  
  55. //if (x > box_x && x < box_x + 29 && y > box_y && y < box_y + 30){
  56. //mode = true;
  57. box_x = (int)event.getX();
  58. //}
  59. }
  60.  
  61. if(event.getAction() == MotionEvent.ACTION_MOVE) {
  62. //int x = (int)event.getX();
  63. //int y = (int)event.getY();
  64.  
  65. //if (mode == true){
  66. box_x = (int)event.getX();
  67. //}
  68. }
  69.  
  70. invalidate();
  71.  
  72. return true;
  73. }
  74.  
  75.  
  76. @Override
  77. public void surfaceChanged(SurfaceHolder holder,
  78. int format, int width, int height ){
  79.  
  80. }
  81.  
  82.  
  83. @Override
  84. public void surfaceCreated(SurfaceHolder holder){
  85. thread.startRunning(true);
  86. thread.start();
  87. }
  88.  
  89. @Override
  90. public void surfaceDestroyed(SurfaceHolder holder){
  91. thread.startRunning(false);
  92. thread.stop();
  93. }
  94.  
  95.  
  96.  
  97. }
  98.  
  99. RectF rect = new RectF(x,y, x + box.getWidth(), y+box.geHeight());
  100.  
  101. if(rect.contains(touchX, touchY)) {
  102. // You hit the box, allow dragging...
  103. }
  104.  
  105. package teste.com.br.teste;
  106.  
  107. import android.content.Context;
  108. import android.graphics.Bitmap;
  109. import android.graphics.BitmapFactory;
  110. import android.graphics.Canvas;
  111. import android.view.MotionEvent;
  112. import android.view.View;
  113.  
  114. import java.util.ArrayList;
  115. import java.util.List;
  116.  
  117. /**
  118. * Created by Tacila on 03/07
  119. */
  120.  
  121. public class Game extends View {
  122. private Context context;
  123. private BitmaptArrastavel bmArrastavel;
  124. private BitmaptArrastavel bmArrastavel2;
  125. private BitmaptArrastavel bmArrastavelTeste;
  126. private List<BitmaptArrastavel> btms;
  127. private BitmaptArrastavel[] btmsAtivas;
  128.  
  129. private BitmaptArrastavel[] lake;
  130. private BitmaptArrastavel[] ativos;
  131.  
  132.  
  133. private int qntDeItens = 5;
  134.  
  135. public Game(Context context) {
  136. super(context);
  137. this.context = context;
  138. init();
  139. }
  140.  
  141.  
  142. public void init() {
  143. btms = new ArrayList<BitmaptArrastavel>();
  144. btmsAtivas = new BitmaptArrastavel[1];
  145.  
  146. ativos=new BitmaptArrastavel[1];
  147. lake = new BitmaptArrastavel[3];
  148.  
  149. lake[0] = new BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cartadc)), 200);
  150. lake[1] = new BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cartaao)), 210);
  151. lake[2] = new BitmaptArrastavel(escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cartake)), 220);
  152.  
  153. btms.add(bmArrastavel);
  154. btms.add(bmArrastavelTeste);
  155. btms.add(bmArrastavel2);
  156. }
  157.  
  158. @Override
  159. protected void onDraw(Canvas canvas) {
  160. super.onDraw(canvas);
  161.  
  162. canvas.drawRGB(0,0,139);
  163.  
  164. for(int i = 0; i<=2; i++){
  165. lake[i].drawOnCanvas(canvas);
  166. }
  167. }
  168.  
  169. public void travarArr(float x, float y ){
  170. for(int i = 0; i<=2 ; i++){
  171. if(lake[i].isDentro(x,y)){
  172. ativos[0]=lake[i];
  173. }
  174. }
  175. }
  176.  
  177.  
  178. public void destravarBitmap() {
  179. ativos[0]=null;
  180. }
  181.  
  182. public Bitmap escalaBitmap(Bitmap bm) {
  183.  
  184. return Bitmap.createScaledBitmap(bm, 200, 300, false);
  185. }
  186.  
  187. @Override
  188. public boolean onTouchEvent(MotionEvent event) {
  189.  
  190. final int action = event.getActionMasked();
  191.  
  192. switch (action) {
  193. case MotionEvent.ACTION_DOWN:
  194. System.out.println("Dentro do MotionEvent.ActionDown");
  195. travarArr(event.getX(),event.getY());
  196. break;
  197. case MotionEvent.ACTION_UP:
  198. case MotionEvent.ACTION_CANCEL:
  199. System.out.println("Dentro do MotionEvent.ActionUP e ActionCancel tenho q entrar no destrava ");
  200. destravarBitmap();
  201. break;
  202. case MotionEvent.ACTION_MOVE:
  203. System.out.println("Dentro do MotionEvent.ActionMove");
  204.  
  205. for(int i = 0; i<ativos.length;i++){
  206. if(ativos[i]!=null){
  207. ativos[i].mover((int)event.getX(),(int)event.getY());
  208. invalidate();
  209. }
  210. }
  211. break;
  212. case MotionEvent.ACTION_POINTER_DOWN:
  213. System.out.println("Dentro do MotionEvent.ActionPointerDown");
  214. travarArr(event.getX(), event.getY());
  215. break;
  216. case MotionEvent.ACTION_POINTER_UP:
  217. System.out.println("Dentro do MotionEvent.ActionPointerUp");
  218. destravarBitmap();
  219. break;
  220. default:
  221. return super.onTouchEvent(event);
  222. }
  223. return true;
  224. }
  225.  
  226.  
  227. // int rotation = 0;
  228. //
  229. // public Bitmap vertical() {
  230. // Matrix matrix = new Matrix();
  231. // matrix.postRotate(90);
  232. // Bitmap bm = escalaBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.carta4c));
  233. // return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
  234. // }
  235. }
  236.  
  237. package teste.com.br.teste;
  238.  
  239. import android.graphics.Bitmap;
  240. import android.graphics.Canvas;
  241.  
  242. /**
  243. * Created by Tacila on 05/07/2017.
  244. */
  245.  
  246. public class BitmaptArrastavel {
  247.  
  248. public int x, y, altura, largura;
  249. private Bitmap bitmap;
  250.  
  251. public BitmaptArrastavel(Bitmap b, int x) {
  252. bitmap = b;
  253. this.x = x;
  254. init();
  255. }
  256.  
  257. public void init() {
  258. largura = 200;
  259. altura = 350;
  260. }
  261.  
  262. public Bitmap escalaBitmap(Bitmap bm) {
  263. return Bitmap.createScaledBitmap(bm, largura, altura, false);
  264. }
  265.  
  266. public boolean isDentro(float x, float y) {
  267. return (x >= this.x && x <= this.x + largura && y >= this.y && y <= this.y + altura);
  268. }
  269. public void drawOnCanvas(Canvas canvas) {
  270.  
  271. canvas.drawBitmap(bitmap, x, y, null);
  272. }
  273.  
  274. public void mover(int x, int y) {
  275. this.x = x;
  276. this.y = y;
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement