【问题标题】:How to resize bitmap in canvas?如何调整画布中的位图大小?
【发布时间】:2016-02-11 04:28:56
【问题描述】:

我在此处和 Google 上找不到其他问题的答案。

问题是,在 GameView 中创建的位图在某些屏幕上太大(在我的 Galaxy S5 上是正确的),它们应该使用 GameView.getDensity() 进行缩放。

GameView 类:

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.circle_green);

圈类:

private int score;
private int y = 0;
private int x = 0;
private int Speed, width, height;
private GameView theGameView;
private Bitmap bmp;
private Random rnd;

public Circle(GameView theGameView, Bitmap bmp) {
    this.theGameView = theGameView;
    this.bmp = bmp;
    this.width = bmp.getWidth();
    this.height = bmp.getHeight();
    this.score = theGameView.getScore();

    rnd = new Random();
    x = (int) (30 * theGameView.getDensity() + rnd.nextInt((int) (theGameView.getWidth() - width - 50 * theGameView.getDensity())));
    y = (int) (60 * theGameView.getDensity() + rnd.nextInt((int) (theGameView.getHeight() - height - 80 * theGameView.getDensity())));
    Speed = (int) (2 * theGameView.getDensity());
}

private void bounceOff() {
    if (x + 10 * theGameView.getDensity() > theGameView.getWidth() - width - Speed || x + Speed < 10 * theGameView.getDensity()) {
        Speed = -Speed;
    }

    x = x + Speed;

    if (y + 60 * theGameView.getDensity() > theGameView.getHeight() - height - Speed || y + Speed < 60 * theGameView.getDensity()) {
        Speed = -Speed;
    }

    y = y + Speed;
}

public void onDraw(Canvas canvas) {
    bounceOff();
    if(canvas != null) {
        canvas.drawBitmap(bmp, x, y, null);
    }
}

public boolean isTouched(float x2, float y2) {
    return x2 > x && x2 < x + width && y2 > y && y2 < y + height;
}

【问题讨论】:

    标签: java android canvas bitmap


    【解决方案1】:

    这很容易。只需使用canvas.scale

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-12
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      相关资源
      最近更新 更多