简体   繁体   English

在android游戏开发中进行缩放,如何正确执行?

[英]Scaling in android game development, how to do it properly?

So, recently me and my two teammates started on our exam project. 因此,最近,我和我的两个队友开始了我们的考试项目。 Which is a basic 2D skijumping game. 这是一个基本的2D跳高游戏。

We've done programming on android before but never in relation to games and we have the basic techniques down for creating animations using a thread and all that. 我们之前已经在android上进行过编程,但是从未涉及到游戏,我们掌握了使用线程等创建动画的基本技术。 But we seem to have a scaling issue. 但是我们似乎有一个扩展问题。

Currently we have a background image in the drawable folder in the resolution 1920x1080, we are using BitmapFactory to scale the background image to the users screen size, which seems to be working fine it fits perfectly on all our 3 different smartphones. 当前,我们在1920x1080分辨率的drawable文件夹中有一个背景图像,我们正在使用BitmapFactory将背景图像缩放到用户屏幕大小,这似乎很好用,非常适合我们所有3种不同的智能手机。

Were creating an animation of the player sprite skiing down the hill and jumping off the edge, which looks very funny and works perfectly. 正在创建一个让玩家精灵从山上滑下并跳下边缘的动画,该动画看起来非常有趣且完美。 For this I simply divided the screen width and height by 100 and used that percentage of either 1% screen width to move him at certain speeds on the x-axis and likewise on the y-axis with the 1% of the screen height. 为此,我仅将屏幕宽度和高度除以100,然后使用1%屏幕宽度的百分比在x轴上以一定速度移动他,同样在y轴上以屏幕高度的1%移动他。

It works on most phones but, for some reason it won't work on all of them? 它可以在大多数手机上使用,但是由于某种原因不能在所有手机上使用? I don't understand why not? 我不明白为什么不呢? I mean the math should always work shouldn't it? 我的意思是数学应该一直有效,对吗? On some phones he skies outside of the hill in the air and not on the proper trajectory. 在某些电话上,他在空中飞过小山,而不是沿着正确的轨迹飞翔。 I don't get what I'm missing but I'm suspecting it has something to do with the screen format or something? 我没有得到我想要的东西,但我怀疑它与屏幕格式有关吗?

Can anyone enlighten me on this? 有人可以启发我吗? Please keep in mind this is our first android game so were new at game dev. 请记住,这是我们的第一个Android游戏,因此是游戏开发人员的新手。

And thanks for all your input if you have any in advance. 如果您有任何提前的建议,感谢您的所有投入。

try this: 尝试这个:

class V extends View implements Callback {

    private Bitmap mBitmap;
    private Matrix mMatrix;
    private Handler mHandler;
    private float mX;
    private Paint mPaint;

    public V(Context context) {
        super(context);
        Resources res = getResources();
        mBitmap = BitmapFactory.decodeResource(res, R.drawable.layer0);
        mMatrix = new Matrix();
        mHandler = new Handler(this);
        mHandler.obtainMessage(0).sendToTarget();
        mPaint = new Paint();
        mPaint.setColor(0xff00ff00);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        RectF src = new RectF(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
        RectF dst = new RectF(0, 0, w, h);
        mMatrix.setRectToRect(src, dst, ScaleToFit.CENTER);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.concat(mMatrix);
        canvas.drawBitmap(mBitmap,  0,  0, null);
        canvas.drawRect(mX, 20, mX + 20, 40, mPaint);
    }

    @Override
    public boolean handleMessage(Message msg) {
        msg = mHandler.obtainMessage(0);
        if (mX < mBitmap.getWidth()) {
            mX += 1.5f;
            mHandler.sendMessageDelayed(msg, 50);
        } else {
            Log.d(TAG, "handleMessage stop");
        }
        invalidate();
        return true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM