如何使畫布中繪制的圖像對所有屏幕尺寸都有效?我的代碼,但它不起作用,繪制的游戲屏幕對于小屏幕來說太大了,對于大屏幕來說太小了,我已經(jīng)嘗試了一切,但沒有任何效果,我希望畫布根據(jù)屏幕的大小進(jìn)行縮放這是我得到的結(jié)果和期望的結(jié)果:https://imgur.com/a/bSnGfov以下是我的游戲畫布代碼: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); }
1 回答

ITMISS
TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以動(dòng)態(tài)請求安卓設(shè)備屏幕的尺寸。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
獲取當(dāng)前顯示器上的句柄 - 請注意,如果從應(yīng)用程序上下文調(diào)用,您將獲得物理屏幕的大小。如果從活動(dòng)調(diào)用,您將獲得應(yīng)用程序窗口的當(dāng)前大小。
添加回答
舉報(bào)
0/150
提交
取消