简体   繁体   中英

Java - Making bitmaps scale to your screen size?

I am creating an app and want my ui to scale with the phone. I am importing bitmaps into a canvas and drawing them as follows:

public class MainMenu extends View {

private RectF titleBounds, playBounds, scoreBounds, soundBounds, creditBounds;
private Paint titlePaint, playPaint;

private boolean playClick = false, startPlay = false, scoreClick = false, startScore = false, soundClick = false, startSound = false, creditsClick = false, startCredits = false;


public Bitmap play;
public Bitmap playGlow;

public Bitmap sound;
public Bitmap soundGlow;


public int screenWidth, screenHeight;

public MainMenu(Context context) {
    super(context);

    titleBounds = new RectF();
    playBounds = new RectF();
    scoreBounds = new RectF();
    soundBounds = new RectF();
    creditBounds = new RectF();

    titlePaint = new Paint();
    playPaint = new Paint();

    play = BitmapFactory.decodeResource(getResources(), R.drawable.play);
    playGlow = BitmapFactory.decodeResource(getResources(), R.drawable.playglow);

    sound = BitmapFactory.decodeResource(getResources(), R.drawable.sound);
    soundGlow = BitmapFactory.decodeResource(getResources(), R.drawable.soundglow);


    // Get window size and save it to screenWidth and screenHeight.
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size); 
    screenWidth = size.x;
    screenHeight = size.y;
}


@Override protected void onDraw(Canvas canvas) {


    ViewGroup parent = (ViewGroup)getParent();

    playBounds.set(screenWidth / 2 - play.getWidth() / 2, screenHeight * 1/3 - play.getHeight() / 2, playBounds.left + play.getWidth(), playBounds.top + play.getHeight());
    soundBounds.set(scoreBounds.centerX() - sound.getWidth() / 2, scoreBounds.bottom + 10, soundBounds.left + sound.getWidth(), soundBounds.top + sound.getHeight());

The problem is theres nothing to scale the image to fit the phone, it just reads the x and y size of the image I import. What method is there for scaling images?

Use the createScaledBitmap of the Bitmap class

bitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);

It will scale your bitmap but not greater than the width and height that is because it can cause blurriness to your bitmap.

documentation:

    Creates a new bitmap, scaled from an existing bitmap, when possible. 
   If the specified width and height are the same as the current width and height of the source bitmap,
   the source bitmap is returned and no new bitmap is created.

for full screen

bitmap  = Bitmap.createScaledBitmap(bitmap, screenWidth , screenHeight , true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM