如何使画布中绘制的图像对所有屏幕尺寸都有效?我的代码,但它不起作用,绘制的游戏屏幕对于小屏幕来说太大了,对于大屏幕来说太小了,我已经尝试了一切,但没有任何效果,我希望画布根据屏幕的大小进行缩放这是我得到的结果和期望的结果: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贡献1871条经验 获得超8个赞
您可以动态请求安卓设备屏幕的尺寸。
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
获取当前显示器上的句柄 - 请注意,如果从应用程序上下文调用,您将获得物理屏幕的大小。如果从活动调用,您将获得应用程序窗口的当前大小。
添加回答
举报
0/150
提交
取消