2014-06-11 39 views
0

我正在創建一個應用程序,並希望我的用戶界面可以通過手機進行擴展。我導入位圖到畫布和繪圖它們如下:Java - 使位圖縮放到您的屏幕尺寸?

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尺寸。有什麼方法可以縮放圖像?

回答

0

使用Bitmap類

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

它會擴展你的位圖,但不超過寬度和高度,這是因爲它會導致模糊你的位圖更大的createScaledBitmap

文檔:

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); 
+0

,那麼什麼纔是我的scaledHeight和scaledWidth – Divergent

+0

@Divergent的高度和寬度,你希望你的位圖是。 –

+0

那麼將它放在我的調整大小的方法中是一個好主意? – Divergent