Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 16th, 2012  |  syntax: None  |  size: 3.45 KB  |  hits: 76  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Android canvas.translate()
  2. public class draw extends View {
  3.     View v;
  4.     Paint paint;
  5.  
  6.     int width;
  7.     int height;
  8.  
  9.     int view_x;
  10.     int view_y;
  11.  
  12.     static final int MAX_GAME_SPEED=25;
  13.     static int fps;
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.     public draw(Context context) {
  25.             super(context);
  26.  
  27.            // tb.loadimg();
  28.  
  29.  
  30.             Thread myThread = new Thread(new UpdateThread());
  31.             myThread.start();
  32.         }  
  33.  
  34.             @Override
  35.         protected void onDraw(Canvas c){
  36.         super.onDraw(c);
  37.         paint = new Paint(); //Paint paint = new Paint();
  38.         paint.setStyle(Paint.Style.FILL);
  39.  
  40.  
  41.         //get screen size
  42.         WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE);
  43.         Display display = wm.getDefaultDisplay();
  44.  
  45.  
  46.         width = display.getWidth();  // deprecated
  47.         height = display.getHeight();  // deprecated
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.             // make the entire canvas white
  56.             paint.setColor(Color.WHITE);
  57.             c.drawPaint(paint);
  58.  
  59.  
  60.             if (tb.dirt != null && tb.stone != null && tb.sand != null && tb.gold != null && tb.iron != null && tb.coal != null && tb.kies != null && tb.diamond != null && tb.redstone != null && tb.lava != null && tb.azur != null && tb.water != null)
  61.             {
  62.             c.drawBitmap(tb.dirt,0,0, paint);
  63.             c.drawBitmap(tb.stone,0,50, paint);
  64.             c.drawBitmap(tb.sand,0,100, paint);
  65.             c.drawBitmap(tb.gold,0,150, paint);
  66.             c.drawBitmap(tb.iron,50,0, paint);
  67.             c.drawBitmap(tb.coal,50,50, paint);
  68.             c.drawBitmap(tb.kies,50,100, paint);
  69.             c.drawBitmap(tb.diamond,50,150, paint);
  70.             c.drawBitmap(tb.redstone,100,0, paint);
  71.             c.drawBitmap(tb.lava,100,50, paint);
  72.             c.drawBitmap(tb.azur,100,100, paint);
  73.             c.drawBitmap(tb.water,100,150, paint);
  74.  
  75.  
  76.  
  77.             }
  78.             if (tb.map == null)
  79.             {
  80.  
  81.             }
  82.             view_x = 100;
  83.             view_y = 100;
  84.  
  85.  
  86.             c.translate(0, -4);
  87.  
  88.  
  89.  
  90.  
  91.     }
  92.  
  93.             public Handler updateHandler = new Handler(){
  94.                 /** Gets called on every message that is received */
  95.                 // @Override
  96.                 public void handleMessage(Message msg) {
  97.  
  98.                   invalidate();
  99.                     super.handleMessage(msg);
  100.                 }
  101.             };
  102.  
  103.  
  104.  
  105.             public class UpdateThread implements Runnable {
  106.  
  107.                 @Override
  108.                 public void run() {
  109.                     while(true){ //Game Loop
  110.  
  111.                         long startTime = System.currentTimeMillis();
  112.                         draw.this.updateHandler.sendEmptyMessage(0); //veranlassen, dass paint() erneut aufgerufen werden soll
  113.                         //for (int i=0; i<999999; i++); //Bremse
  114.  
  115.                         Thread.yield();                
  116.                         long executionTime = System.currentTimeMillis()-startTime;
  117.  
  118.                         if (executionTime<MAX_GAME_SPEED){
  119.                             try {
  120.                                 Thread.sleep(MAX_GAME_SPEED-(int)executionTime);
  121.                             } catch (InterruptedException e) {
  122.                                 // TODO Auto-generated catch block
  123.                                 e.printStackTrace();
  124.                             }
  125.                             fps=1000/MAX_GAME_SPEED;
  126.                         } else fps=(int) (1000/executionTime);
  127.  
  128.                     }
  129.  
  130.                 }
  131.  
  132.             }
  133.  
  134.     }