简体   繁体   English

Java-使位图缩放到您的屏幕尺寸吗?

[英]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. 问题是没有什么可以缩放图像以适合手机的,它只是读取我导入图像的x和y大小。 What method is there for scaling images? 有什么方法可以缩放图像?

Use the createScaledBitmap of the Bitmap class 使用Bitmap类的createScaledBitmap

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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