简体   繁体   English

如何使画布适合所有屏幕尺寸?

[英]How to make painted canvas fit all screen sizes?

How to make the images painted in canvas valid for all screen sizes?如何使画布中绘制的图像适用于所有屏幕尺寸? my code but it doesn't work, the painted game screen is too large for small screens and too small for large screens, I have tried everything and nothing works still, I want the canvas to scale according to the screen's size我的代码但它不起作用,绘制的游戏屏幕对于小屏幕来说太大而对于大屏幕来说太小,我已经尝试了一切,但没有任何效果,我希望画布根据屏幕大小进行缩放

this is the result I get and the desired result: https://imgur.com/a/bSnGfov这是我得到的结果和想要的结果: https : //imgur.com/a/bSnGfov

Here's my code for the game canvas:这是我的游戏画布代码:

public class CanvasView extends View {
    Me me;
    public int width;
    public int height;
    public int fx,fy,zx,zy;
    int[][]lines;
    private Bitmap pme, wall,finish,redlock,redkey,bluelock,bluekey,greenlock,greenkey ,mBitmap,white,c1,c2,c3,c4,c5,c6,c7
            ,c8,c9,c10,c11,c12;
    private Canvas mCanvas;
    private Path mPath;
    Context context;
    private Paint p;
    private int mx, my,character;private int dir;
    private static final float TOLERANCE = 5;float bb;string bbb; int sz;int fz;
    public CanvasView(Context c,int[][] l,Me m,int mxx,int myy,int chara) {
        super(c);
        lines=l;
        mx = mxx;
        my = myy;
        context = c;
        fx=6;fy=6;zx=0;zy=0;
        me = m;
        character=chara;
        mPath = new Path();

        p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.BLACK);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeJoin(Paint.Join.ROUND);
        p.setStrokeWidth(4f);
    }

    // override onSizeChanged
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        // your Canvas will draw onto the defined Bitmap
        mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    // override onDraw
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = mx;
        int height = my;float calc = getResources().getDisplayMetrics().density;
        canvas.drawBitmap(pme,calc*( (me.GetCenterX() - 24)+zx),calc*( (me.GetCenterY() - 24)+zy), p);
        int px = (me.GetCenterX() - 24) / 48;
        int py = (me.GetCenterY() - 24) / 48;
        for (int j = 0; j < fx; j++)
        {
            for (int i = 0; i < fy; i++)
                if (i < height)
                {

                    float tileX = ((j * 48) + zx) * calc;
                    float tileY = ((i * 48) + zy) * calc;
                    int type = lines[i][j];
                    switch (type)
                    {
                        case 1:
                            canvas.drawBitmap(wall, tileX,tileY, p);
                            break;
                        case 3:
                            canvas.drawBitmap(finish, tileX,tileY, p);
                            break;
                        case 4:
                            canvas.drawBitmap(bluelock, tileX,tileY, p);
                            break;
                        case 5:
                            canvas.drawBitmap(bluekey, tileX,tileY, p);
                            break;
                        case 6:
                            canvas.drawBitmap(redlock, tileX,tileY, p);
                            break;
                        case 7:
                            canvas.drawBitmap(redkey, tileX,tileY, p);
                            break;
                        case 8:
                            canvas.drawBitmap(greenlock, tileX,tileY, p);
                            break;
                        case 9:
                            canvas.drawBitmap(greenkey, tileX,tileY, p);
                            break;
                    }
                }

            }
        }

        for (int i = 0; i <= 6;i++ )
        {
            canvas.drawBitmap(white, 6*48* calc,i*48* calc, p);
        }
        for (int j = 0; j <= 5; j++)
        {
            canvas.drawBitmap(white, j*48* calc,6*48* calc, p);
        }

    }


}

You can dynamically request the dimensions of your Android device's screen.您可以动态请求 Android 设备屏幕的尺寸。

 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();
 display.getSize(size);
 int width = size.x;
 int height = size.y;

Get a handle on the current display - note that if you call from the application context, you will get the size of your physical screen.获取当前显示的句柄 - 请注意,如果您从应用程序上下文调用,您将获得物理屏幕的大小。 If you call from the activity, you will get the current size of the app window.如果您从活动中调用,您将获得应用程序窗口的当前大小。

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

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