how to adapt the bitmaps to diff screen dimentions

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Aug 31, 2018, 6:18:17 PM via Website

i've a game where the user must guide a plane to avoid obstacles and such

my problem is that the bitmaps don't adapt to différent screens ( bitmaps too big on small devices and too small on big ones )

here's the plane's bitmap file

===============================================
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

public class Plane {
public float X = 0;
public float Y = 0;
public int ID = 0;
public int Life = C.PLANE_LIFE;
public float SpeedX = 0;
public float SpeedY = 0;
public float Acc = 0;

public Rect bmpRect = new Rect(0,0,118,109);
public Rect drawRect = new Rect(0,0,130,120);
public Rect collisionRect = new Rect(0,0,80,52);

public Bitmap []bmp = null;
public static int BMP_NUM = 3;

public Plane(float x, float y){
this.X = x;
this.Y = y;
}

public Plane(Bitmap []bmp, float x, float y){
this.X = x;
this.Y = y;
this.bmp = bmp;
bmpRect = new Rect(0,0, bmp[0].getWidth(), bmp[0].getHeight());
}

public void draw(Canvas canvas){
canvas.drawBitmap(bmp[ID], bmpRect, drawRect, null);
//canvas.drawLine(collisionRect.left, collisionRect.top, collisionRect.right, collisionRect.bottom, new Paint());
}

public void update(){
ID ++;
ID %= BMP_NUM;

SpeedY += Acc;
if(SpeedY > 0 && SpeedY > C.PLANE_SPEEDY_MAX) SpeedY = C.PLANE_SPEEDY_MAX;
if(SpeedY < 0 && SpeedY < -C.PLANE_SPEEDY_MAX) SpeedY = -C.PLANE_SPEEDY_MAX;
if(Y > C.SCR_H - drawRect.height()){
Y = C.SCR_H - drawRect.height();
SpeedY = 0;
}
if(Y < 0){
Y = 0;
SpeedY = 0;
}

Y += SpeedY;

drawRect.offsetTo((int)X, (int)Y);
collisionRect.offsetTo((int)X + 38, (int)Y + 50);
}

public void startAnime(){
if( Y > C.SCR_H/4)
Y -= 3;
if(X > C.SCR_W/5)
X -= 3;
ID ++;
ID %= BMP_NUM;
drawRect.offsetTo((int)X, (int)Y);
}

public boolean canPlay(){
return X <= C.SCR_W/5;
}

public void endAnime(){
if( Y < C.SCR_H/4 *3)
Y += 4;
else if( Y > C.SCR_H/4 *3)
Y -= 4;

if(X < C.SCR_W - 50)
X += 5;
ID ++;
ID %= BMP_NUM;
drawRect.offsetTo((int)X, (int)Y);
}

public boolean hasLand(int endX, int endY){
return (X >= endX) && (Y > endY);
}

please help i'm stuck:(

Be the first to answer

Recommended articles