首页 > 解决方案 > 如何使位图上的图像更小?

问题描述

我正在尝试在 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);
}}

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

标签: javaandroidbitmapandroid-canvasandroid-bitmap

解决方案


如何调整画布中的位图大小?

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

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


推荐阅读