【发布时间】:2015-07-29 01:52:50
【问题描述】:
public class GameView extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private int x = 0;
private int y = 0;
private int xSpeed = 1;
public static int WIDTH = 800;
public static int HEIGHT = 450;
private Background bg;
public GameView(Context context) {
super(context);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.tuo));
bg.setVector(-5);
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic);
// tutaj dorysować tło
}
@Override
protected void onDraw(Canvas canvas) {
if (x == getWidth() - bmp.getWidth()) {
xSpeed = -4;
}
if (x == 0) {
xSpeed = 4;
}
x = x + xSpeed;
final float scaleFactorX = getWidth()/WIDTH;
final float scaleFactorY = getHeight()/HEIGHT;
if(canvas!=null) {
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);
bg.draw(canvas);
canvas.restoreToCount(savedState);
}
canvas.drawBitmap(bmp, x , 10, null);
}
public void update()
{
bg.update();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
x=x+10;
return super.onTouchEvent(event);
}
}
我无法缩放图像 - 它应该通过除以 getWidth/WIDTH(和高度)来进行缩放 - WIDTH 和 HEIGHT 是图像尺寸。
它只是打印图像的左上角,就像完全没有缩放一样;(
如果有人有任何想法,我将不胜感激:)
【问题讨论】:
标签: java android image background