【问题标题】:How to make images on Bitmap smaller?如何使位图上的图像更小?
【发布时间】:2021-07-18 09:01:42
【问题描述】:

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

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

【问题讨论】:

    标签: java android bitmap android-canvas android-bitmap


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      How to resize bitmap in canvas?

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

      scale = 1.0f 将绘制相同的可见尺寸位图

      【讨论】:

      • 我将它添加到代码的末尾,并将比例值设置为 1.0f,但它仍然没有改变任何东西
      猜你喜欢
      • 1970-01-01
      • 2010-12-29
      • 1970-01-01
      • 2013-10-21
      • 2014-09-10
      • 2020-10-10
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多