【问题标题】:Cannot scale background image (android studio)无法缩放背景图像(android studio)
【发布时间】: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


    【解决方案1】:

    你需要计算:

    /*
        WIDTH/getWidth() = 4; // if ur image width = 200px
        scaleFactorX = 4*getwidth();
        scaleFactorY = 4*getHeight();
    */
    final float scaleFactorX = WIDTH/getWidth();
    final float scaleFactorY = HEIGHT/getHeight();
    canvas.scale(scaleFactorX*getWidth(), scaleFactorY*getHeight());
    

    我希望它们是成比例的......

    1600*900 或类似的... 400*225

    【讨论】:

    • 宽高交换了吗? fon的高度不是比宽度大吗?
    • 我拥有风景中的一切。我创建了一个 480x270 尺寸的图像,设置 public static int WIDTH = 480;公共静态int高度= 270; (也尝试反转)使用您的代码和我的代码 - 不工作。现在它甚至不显示图片:(
    • 我发现了这个:Bitmap result = Bitmap.createScaledBitmap(YOURPICTURE, imageWidth, imageHeight, false);
    猜你喜欢
    • 2012-07-08
    • 1970-01-01
    • 2018-05-19
    • 2011-02-16
    • 2011-02-19
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多