2014-06-11 38 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