如何使位图上的图像变小?

How to make images on Bitmap smaller?

我正在尝试在 android 上构建一个简单的游戏,但在我的主屏幕上设置背景图像和其他图像资产时,它显得太大,如下图所示。

图片的实际尺寸为 1920x1080,我希望它适合我的屏幕,如下图所示:

我用过的代码是:

public class PoliceView extends View {

private Bitmap police;
private Bitmap background;
private Paint scorePaint = new Paint();
private Bitmap life[] = new Bitmap[2];

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

    police = BitmapFactory.decodeResource(getResources(),R.drawable.police);
    background = BitmapFactory.decodeResource(getResources(),R.drawable.gamebackground);
    

    scorePaint.setColor(Color.BLACK);
    scorePaint.setTextSize(70);
    scorePaint.setTypeface(Typeface.DEFAULT_BOLD);
    scorePaint.setAntiAlias(true);

    life[0] = BitmapFactory.decodeResource(getResources(),R.drawable.heart);
    life[1] = BitmapFactory.decodeResource(getResources(),R.drawable.brokenheart);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //The order of draw matters as objects are drawn in this same order
    canvas.drawBitmap(background,0,0,null);
    canvas.drawBitmap(police, 0, 0, null);
    canvas.drawText("Score:",20, 60, scorePaint);
    //Three lives for the police
    canvas.drawBitmap(life[0],580, 10, null);
    canvas.drawBitmap(life[0],680, 10, null);
    canvas.drawBitmap(life[0],780, 10, null);
}}

如何调整图片大小以适应屏幕?

canvas.save();               // Save current canvas params (not only scale)
canvas.scale(scaleX, scaleY);
canvas.restore();            // Restore current canvas params

scale = 1.0f 将绘制相同可见大小的位图

我使用此代码调整图像大小

        Bitmap tmp = BitmapFactory.decodeFile(mPath);
        ResizeWidth = (int) (your size);
        ResizeHeight = (int) (your size);
    

    Bitmap bitmap = Bitmap.createBitmap(ResizeWidth, ResizeHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);


    Bitmap scaled = Bitmap.createScaledBitmap(tmp, ResizeWidth, ResizeHeight, true);
    int leftOffset = 0;
    int topOffset = 0;
    canvas.drawBitmap(scaled, leftOffset, topOffset, null);