Here is an example.
In my app, there is a red box. It’s moving from left to right. The app starts, when you touch the screen.
Now, I installed this on my big and small smartphone. I touch the both devices at the same time.
And now I see, that the red box on my smaller smartphone touches the right side of the screen first.
In my app, there is a ease function from Robert Penner and I added Aspect ratio.
So, first the images:
(This is my libgdx.png file)
http://img213.imageshack.us/img213/5928/libgdx.png
(this is, where the red box starts)
http://img826.imageshack.us/img826/646/boxleft.png
(and this is, where the red box touches the right side)
http://img33.imageshack.us/img33/9328/boxright.png
And this is the code:
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class MyGdxGame implements ApplicationListener {
private static final int VIRTUAL_WIDTH = 800;
private static final int VIRTUAL_HEIGHT = 480;
private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
private Rectangle viewport;
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private float x = 0f;
private float y = 0f;
private boolean start = false;
private float diff = 200f;
private float minTime = 0;
private float maxTime = 770f;
private float iEaseOut = 0;
private float zTime = 0;
@Override
public void create() {
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 2, 2, 32, 32);
sprite = new Sprite(region);
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
@Override
public void render() {
float deltaTime = Gdx.graphics.getDeltaTime();
if (Gdx.input.isTouched()) {
start = true;
}
if (start) {
if (iEaseOut < diff) {
zTime = expoEaseOut(iEaseOut, minTime, maxTime, diff);
float deltaAdd = zTime*deltaTime;
zTime += deltaAdd;
x = zTime;
iEaseOut++;
} else {
start = false;
}
}
System.out.println(Gdx.graphics.getDeltaTime());
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
Gdx.gl.glViewport((int) viewport.x, (int) viewport.y, (int) viewport.width, (int) viewport.height);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.setPosition(x-VIRTUAL_WIDTH/2, y);
sprite.draw(batch);
batch.end();
}
public static float expoEaseOut(float t,float b , float c, float d) {
return (t==d) ? b+c : c * (-(float)Math.pow(2, -10 * t/d) + 1) + b;
}
@Override
public void resize(int width, int height) {
// calculate new viewport
float aspectRatio = (float)width/(float)height;
float scale = 1f;
Vector2 crop = new Vector2(0f, 0f);
if(aspectRatio > ASPECT_RATIO)
{
scale = (float)height/(float)VIRTUAL_HEIGHT;
crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
}
else if(aspectRatio < ASPECT_RATIO)
{
scale = (float)width/(float)VIRTUAL_WIDTH;
crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
}
else
{
scale = (float)width/(float)VIRTUAL_WIDTH;
}
float w = (float)VIRTUAL_WIDTH*scale;
float h = (float)VIRTUAL_HEIGHT*scale;
viewport = new Rectangle(crop.x, crop.y, w, h);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}