Methods for converting between the physics world coordinates and the screen coordinates. : Screen « Hardware « Android






Methods for converting between the physics world coordinates and the screen coordinates.

   

/*
 * Copyright (C) 2010 Adam Nyb?ck
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//package se.anyro.breakin;

import android.graphics.Point;

/**
 * Methods for converting between the physics world coordinates and the screen coordinates.
 * Note! setScreenSize has to be called before the other methods are used.
 */
class SizeUtil {
  private static int screenWidth = 320;
  private static int screenHeight = 480;
  private static int halfWidth = screenWidth / 2;
  private static int halfHeight = screenHeight / 2;
  private static float scale = 16;
  
  public static void setScreenSize(int width, int height) {
    screenWidth = width;
    screenHeight = height;
    
    halfWidth = width / 2;
    halfHeight = height / 2;
    
    // Scale to maximum height/width depending on the ratio
      if (width * 3 > height * 2) {
        scale = (float) height / 200;
      } else {
        scale = (float) width / 200;
      }
  }
  
  public static int getScreenWidth() {
    return screenWidth;
  }
  
  public static int getScreenHeight() {
    return screenHeight;
  }
  
  public static void toScreen(float worldX, float worldY, Point screen) {
    screen.x = halfWidth + (int) (worldX * scale);
    screen.y = halfHeight - (int) (worldY * scale);
  }
  
  public static int toScreen(float worldLen) {
    return (int) (worldLen * scale);
  }

  public static float fromScreen(int screenLen) {
    return screenLen / scale;
  }
}

   
    
    
  








Related examples in the same category

1.Multiscreen size
2.Nested PreferenceScreen
3.Get the size of Default Display Screen
4.Screen Orientation
5.Rows have different number of columns and content doesn't fit on screen: column 4 of row 2 shrinks all of the other columns
6.Demonstrates the Tab scrolling when too many tabs are displayed to fit in the screen.
7.Using PreferenceScreen
8.Splash Screen
9.Get the optimal preview size for the given screen size.
10.Get screen Orientation
11.add Home Screen Shortcut
12.Return the supported picture size that best fits on the device screen.
13.Screen Short
14.Set full screen mode