根据屏幕模式(纵向或横向)更改绘图

发布于 2024-11-30 01:06:32 字数 2151 浏览 4 评论 0原文

我正在将游戏从 JME 移植到 Android,但发现了一个恼人的问题。下一个活动必须绘制网格,事实上确实如此。但网格仅在景观模式下显示正常。当您将手机垂直放置(纵向模式)时,它会显得奇怪地压缩。 主要代码是用于绘制单元格行的方法,(getSquareRow) 预先感谢您的任何帮助。

这是代码:

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class TraxActivity extends Activity {
    InternalView myView;
    private float shift, di;
    private float n, minim, redux, side;
    boolean first = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        myView = new InternalView(this);
        setContentView(myView);
    }

    private class InternalView extends View{
        public InternalView(Context context){
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();

            n=5;
            di=12;
            int ancho = getWidth();
            int alto = getHeight();
            minim = Math.min(getWidth(), getHeight());
            redux = minim*9/10;
            shift = (getWidth() - redux)/2;
            side = redux/n;

            //background
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLUE);
            canvas.drawPaint(paint);

            //grid
            for(int j=0; j < n; j++){
                getSquareRow( canvas, j, paint);
            } 



        }




        public void getSquareRow(Canvas g, int row, Paint p){

            p.setColor(Color.BLACK);
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(10);

            for(int i=0; i < n; i++){
                g.drawRect(shift+i*side, shift+row*side, side, side, p );
            }

        }




    }
} 

I'm porting a game from JME to Android and I found an annoying problem. The next activity must draw a grid, and in fact it does. But the grid appear OK just in landscaping mode. When you put the phone vertical (in portrait mode) it appears strangely compressed.
The main code is the method used to draw a cells row, (getSquareRow)
Thanks in advance for any help.

Here is the code:

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class TraxActivity extends Activity {
    InternalView myView;
    private float shift, di;
    private float n, minim, redux, side;
    boolean first = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        myView = new InternalView(this);
        setContentView(myView);
    }

    private class InternalView extends View{
        public InternalView(Context context){
            super(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();

            n=5;
            di=12;
            int ancho = getWidth();
            int alto = getHeight();
            minim = Math.min(getWidth(), getHeight());
            redux = minim*9/10;
            shift = (getWidth() - redux)/2;
            side = redux/n;

            //background
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLUE);
            canvas.drawPaint(paint);

            //grid
            for(int j=0; j < n; j++){
                getSquareRow( canvas, j, paint);
            } 



        }




        public void getSquareRow(Canvas g, int row, Paint p){

            p.setColor(Color.BLACK);
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(10);

            for(int i=0; i < n; i++){
                g.drawRect(shift+i*side, shift+row*side, side, side, p );
            }

        }




    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-12-07 01:06:32

谢谢大家,我将把游戏的目标定位为景观美化模式。虽然我同意alextsc,但在这种情况下,Android 平台似乎无法在纵向模式下准确地重绘。
我无法发布图像,因为我是在这里发帖的新手,但是运行此代码您可以检查异常情况,并且似乎与方法drawRect有关。

Thanks guys, I'll be targeting the game just for landascaping mode. Althought I agree with alextsc but in this case looks like Android platform doesn't redraw accurately in portrait mode.
I can't to publish an image because I'm newbie posting here, but running this code you can check the anomaly and seems be related with the method drawRect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文