Android SurfaceView填充背景与图片

时间:2012-06-18 16:30:45

标签: android background surfaceview image fill

我是初学者/中级java学生,我刚开始使用android 2D编程。我正在使用surfaceView并且想要设置背景,但问题是我不知道如何使背景填充100%的屏幕,这就是我绘制背景的方式:

public GFXSurface(Context context) {
    super(context);
    mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.roadtest);
    getHolder().addCallback(this);
    mThread = new ViewThread(this);
}
public void doDraw(Canvas canvas){
    canvas.drawBitmap(mBitmap, 0, 0, null);
}

1 个答案:

答案 0 :(得分:2)

这样的事情可以解决问题,在获得位图之后放置它

float scale = (float)mBitmap.getHeight()/(float)getHeight();
int newWidth = Math.round(mBitmap.getWidth()/scale);
int newHeight = Math.round(mBitmap.getHeight()/scale);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(mBitmap, newWidth, newHeight, true);

然后像你已经做的那样画画:)