thieumao

Bài 1: Vẽ 1 hình đơn giản lên màn hình (E3droid)

Sep 28th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. // Link: http://vietandroid.com/threads/bai-1-ve-1-hinh-don-gian-len-man-hinh-e3droid.6856/
  2. package Bai_1_e3droid.gioi;
  3.  
  4. import android.view.MotionEvent;
  5.  
  6. import com.e3roid.E3Activity;
  7. import com.e3roid.E3Engine;
  8. import com.e3roid.E3Scene;
  9. import com.e3roid.drawable.Shape;
  10.  
  11. public class Bai_1_e3droidActivity extends E3Activity {
  12.  
  13.     private final static int WIDTH  = 320;//Chiều rộng của màn hình
  14.     private final static int HEIGHT = 480;//Chiều cao của màn hình
  15.     private final static int BOX_SIZE = 50;//Kích thước của 1 box  
  16.  
  17.     private Shape shape1, shape2, shape3, shape4;//Khai báo 4 biến shape
  18.      
  19.     @Override
  20.     public E3Engine onLoadEngine() {
  21.         E3Engine engine = new E3Engine(this, WIDTH, HEIGHT);//Thiết lập chiều cao và rộng
  22.         engine.requestFullScreen();//Set màn hình full
  23.         engine.requestPortrait();
  24.         return engine;
  25.     }
  26.  
  27.     @Override
  28.     public E3Scene onLoadScene() {
  29.         E3Scene scene = new E3Scene();
  30.         scene.addEventListener(this);//Bắt sự kiện khi bạn chạm lên màn hình
  31.                  
  32.          
  33.         //Khỏi tạo màu cho các hình. (Các bạn thử thay đổi các giá trị xem sao nhé)
  34.         shape1.setColor(1, 0, 0, 1);
  35.         shape2.setColor(0, 1, 0, 1);
  36.         shape3.setColor(0, 0, 1, 1);
  37.         shape4.setColor(1, 1, 0, 1);
  38.          
  39.         //Hiện thị các hình này lên. Chú ý: nếu không add mấy cái shape vào thì sẽ không bao giờ nhìn thấy trên
  40.         //màn hình
  41.         scene.getTopLayer().add(shape1);
  42.         scene.getTopLayer().add(shape2);
  43.         scene.getTopLayer().add(shape3);
  44.         scene.getTopLayer().add(shape4);
  45.         return scene;
  46.     }
  47.  
  48.     @Override
  49.     public void onLoadResources() {
  50.         int centerX = getWidth() / 2;//Xác định ví trí trung tâm theo tọa độ X
  51.         int centerY = getHeight() / 2;//Xác định ví trí trung tâm theo tọa độ Y
  52.          
  53.         //Khởi tạo vị trí cho các hình này khi hiện thị lên màn hình
  54.         shape1 = new Shape(centerX, centerY, BOX_SIZE, BOX_SIZE);
  55.         shape2 = new Shape(centerX-BOX_SIZE, centerY-BOX_SIZE, BOX_SIZE, BOX_SIZE);
  56.         shape3 = new Shape(centerX-BOX_SIZE, centerY, BOX_SIZE, BOX_SIZE);
  57.         shape4 = new Shape(centerX, centerY-BOX_SIZE, BOX_SIZE, BOX_SIZE);
  58.     }
  59.      
  60.     @Override
  61.     public boolean onSceneTouchEvent(E3Scene scene, MotionEvent motionEvent) {
  62.         // shape1(red box) rotates and scales when touchEvent ACTION_DOWN occurs.
  63.         if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
  64.             //Thực hiện xoay hình shape1 và phóng to khi bạn chạm vào màn hình
  65.             shape1.rotate(shape1.getAngle() + 20.0f, BOX_SIZE/2, BOX_SIZE/2);
  66.             shape1.scale(shape1.getScaleX() + 0.2f, shape1.getScaleY() + 0.2f, BOX_SIZE / 2, BOX_SIZE / 2);
  67.         }
  68.         return false;
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment