簡體   English   中英

Java-使位圖縮放到您的屏幕尺寸嗎?

[英]Java - Making bitmaps scale to your screen size?

我正在創建一個應用程序,並希望我的用戶界面可以隨手機擴展。 我將位圖導入畫布並按如下方式繪制它們:

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());

問題是沒有什么可以縮放圖像以適合手機的,它只是讀取我導入圖像的x和y大小。 有什么方法可以縮放圖像?

使用Bitmap類的createScaledBitmap

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

它將縮放您的位圖,但不會大於寬度和高度,這是因為它會導致位圖模糊。

說明文件:

    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.

全屏

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM