如何设置 image 的大小?

程序中有一个splash.png,想让这张图能占据整个屏幕,就像ImageView 中的fitXY。
splash.png的大小是480x767。
如何修改下面的代码呢?

public class BitmapConfigView extends LinearLayout {
private Bitmap mImage;
private Paint mPaint = new Paint();

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

    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
    opts.inScaled = false;
    mImage = BitmapFactory.decodeResource( getResources(), R.drawable.splash, opts);

    mPaint.setDither(true);
    setWillNotDraw(false);
    setOrientation(VERTICAL);
    setGravity(Gravity.BOTTOM);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(mImage, 0.0f, 0.0f, mPaint);
}
}

为了让图像占满整个屏幕,你可以尝试按照以下步骤进行修改:


1.在 BitmapConfigView 的构造函数中,使用 getWindowManager().getDefaultDisplay().getSize(point) 来获取屏幕的宽高。point 是一个 Point 对象,它的 x 和 y 分别表示屏幕的宽和高。


2.使用下面的代码计算出缩放图像时使用的缩放比例:

float scaleX = (float) point.x / mImage.getWidth();
float scaleY = (float) point.y / mImage.getHeight();

3.使用下面的代码创建一个缩放的矩阵:

Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);

4.使用矩阵来创建一个新的缩放后的图像:

Bitmap scaledBitmap = Bitmap.createBitmap(mImage, 0, 0, mImage.getWidth(), mImage.getHeight(), matrix, true);

5.使用新的图像来替换原来的图像:

mImage = scaledBitmap;

6.在 onDraw() 方法中,使用下面的代码来绘制图像:

canvas.drawBitmap(mImage, 0.0f, 0.0f, mPaint);

这样,图像就应该占满整个屏幕了。

注意:在修改后的代码中,你需要注意图像的缩放比例,确保图像不会变形。