要使屏幕兼容性含帆布帆布、兼容性、要使、屏幕

2023-09-06 16:13:01 作者:━☆丶宠坏丨

我在Android的画布上工作。我想在画布上绘制它适合于每一个屏幕的形状。  如果屏幕尺寸为 320 * 240 ,然后高度和放大器;与宽度为 90 * 90 但 如果 screen_size为> 320 * 240 那么就应该相应增加。

I am working with canvas in android. i want to draw shapes on canvas which fits in every screen. if screen size is 320 * 240 then height & width with is 90 * 90 but if screen_size is > 320 * 240 then it should increase accordingly.

假设 screen_size = 1280 * 800 那么就应该增加多少

Suppose screen_size = 1280 * 800 then how much it should increase

我不知道该怎么为这个做

I don't know what to do for this

code为如同

int fixHeight = 240, fixWidth = 320;
Paint paint;
paint = new Paint();
paint.setColor(Color.GREEN);
DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
@Override
protected void onDraw(Canvas canvas) {
     if (height <= fixHeight && width <= fixWidth) {
          canvas.drawRect(10, 10, 50, 50, paint);  
     } else {
          // Don't Know What TO Put Here
     }
}

任何一个可以帮助我

Can any one help me

推荐答案

的onDraw 方法,你可能要检查的布局高度宽度 和调整draw.height并相应draw.width。

in your onDraw method you might want to check the layout height and width of your canvas and adjust your draw.height and draw.width accordingly.

如果规模已经扩大了屏幕,然后你可以使用一个乘数因子您draw.width和draw.height常量要么乘或绘制前分裂。

If the size has increased for the screen then you can use a multiplying factor to your draw.width and draw.height constants to either multiply or divide before drawing.

编辑: code被张贴后,

After code was posted.

可以说,320 * 240是基本情况下你画高度的矩形40和广度为40即 canvas.drawRect(10,10,50,50,油漆); 所以左侧,顶部,右侧和底部可以用你的情况进行修改使用以下公式为1280×800的新决议。

Lets say 320*240 is the base case for you to draw a rectangle of height as 40 and breadth as 40 that is canvas.drawRect(10, 10, 50, 50, paint);So left, top, right and bottom can be used to be modified in your case with a new resolution of 1280*800 using the below formula.

320 --> (50 - 10)
1280 --> (x - 10)

所以 X = 10 +(50-10)*三百二十分之一千二百八十零

试着重复wdith相同forumla,你应该得到你的基本坐标。我相信你可以调整你的10,10基于同样的上述公式(这我保持恒定在这里)到新的位置。

Try to repeat the same forumla for wdith and you should get your base co-ordinates. I believe you can adjust your 10, 10(which I am keeping here constant) to a new location based on the same above formula.