【问题标题】:AndEngine - Sprites are not getting scaled on different devicesAndEngine - 精灵不会在不同的设备上缩放
【发布时间】:2013-04-04 19:38:56
【问题描述】:

代码:

这就是我初始化相机的方式。
我',试图获得设备的分辨率并基于此设置相机的宽度和高度。

     public class GameActivity extends SimpleBaseGameActivity {

        private SmoothCamera mCamera;
        private DisplayMetrics dM; 

        private int CAMERA_WIDTH, CAMERA_HEIGHT;

        private double  ScreenWidth,
                        ScreenHeight,
                        resolutionRatio;


        public EngineOptions onCreateEngineOptions() {

             //set Camera
            getDeviceResolution();
            setCamera();


        EngineOptions options =  new EngineOptions(true,
            ScreenOrientation.LANDSCAPE_FIXED, 
            new RatioResolutionPolicy((int)this.getCameraWidth(),(int)this.getCameraHeight()),
            //new FillResolutionPolicy(),
            mCamera);
          return options;

        }

      private void getDeviceResolution() {
            dM = new DisplayMetrics();
            this.getWindowManager().getDefaultDisplay().getMetrics(dM);
            this.ScreenWidth    = dM.widthPixels;
            this.ScreenHeight   = dM.heightPixels;
            this.resolutionRatio = this.ScreenWidth/this.ScreenHeight;

                resolutionRatio = (double)Math.round(resolutionRatio * 100) / 100;

            Log.d("Resolution","ScrennWidth: "+this.ScreenWidth );
            Log.d("Resolution","ScrennHeight: "+this.ScreenHeight );
                    Log.d("Resolution","Resolution Ratio: " +   this.resolutionRatio );
        }



    private void setCamera() {

        if(resolutionRatio == 1.66){
            this.setCameraHeight(340);
            this.setCameraWidth(400);
        }else if(resolutionRatio == 2.13){
            this.setCameraHeight(480);
            this.setCameraWidth(1024);
        }else if(resolutionRatio == 1.77){
            this.setCameraHeight(720);
            this.setCameraWidth(1280);
        }else if(resolutionRatio == 1.5){
            this.setCameraHeight(320);
            this.setCameraWidth(480);
        }else if(resolutionRatio == 1.67){
            this.setCameraHeight(480);
            this.setCameraWidth(800);
        }else {
            this.setCameraHeight((int) this.ScreenHeight);
            this.setCameraWidth((int) this.ScreenWidth);
        }

        // Create a Camera
        this.mCamera = new SmoothCamera(0,0,(int)getCameraWidth(),(int)getCameraHeight(),100,100,1.0f);
            mCamera.setZoomFactor(1.0f);

            mCamera.setBoundsEnabled(true);
            mCamera.setBounds(0, 0, mCamera.getWidth(), mCamera.getHeight());   
        }



        }

问题是。

我在分辨率为 480x320 的设备上玩过游戏;

当我在分辨率为 800X480 的设备上尝试相同的代码时

我认为精灵没有在更高分辨率的设备上缩放。
据我所知,引擎可以原生缩放相机和精灵。

那么为什么精灵在这种情况下没有得到缩放?

我应该怎么做才能根据分辨率放大精灵?

我也尝试过 FillResolutionPolicy。一样的。

我正在使用 andengine 和 SpriteSheets 的 TexturePackerExtension。

【问题讨论】:

    标签: android andengine resolution android-resolution


    【解决方案1】:

    您需要一个Camera 和一个固定的width 和一个由设备屏幕纵横比计算的height。将其与 FillResolutionPolicy 配对,您将得到您想要的。

    注意:

    AndEngine 不会为您缩放 Sprites,但您设置了Camera 以便整个Scene 显示为缩放。

    【讨论】:

      【解决方案2】:

      即使在您的示例中,AndEngine 也会扩展您的 Sprites。结果不是你预期的原因是你手动设置了相机的widthheight(根据分辨率)。

      如果您真的希望每个精灵在每个显示器上都以相同的方式缩放,那么您应该选择一个固定分辨率来创建游戏。 例如,您可以将每个精灵和每个位置设置为 960x640 的分辨率。

       // skip the whole if(resolutionRatio == 1.66){..  part
      this.mCamera = new SmoothCamera(0, 0, 960f, 640f, 100, 100, 1.0f);
      

      AndEngine 然后将缩放相机,使其尽可能多地填充显示屏。

       public EngineOptions onCreateEngineOptions() {
            EngineOptions options = new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(1.5f), camera;
            return options;
       }
      

      1.5f 是适合 960x640 分辨率的纵横比。 现在您不必再关心不同的显示尺寸了。

      【讨论】:

      • 这并不能解决问题。在不同分辨率的不同设备上尝试时,我在相机的左右两侧出现了白色条纹。
      • 那些条纹是正常的,AndEngine 会尝试填充大部分显示而不拉伸图像(通过保持固定的纵横比)。无论如何,如果您想使用不同的分辨率(就像您在示例中通过检测显示指标所做的那样......),您需要手动缩放 Sprite。在您的 800x480 示例中,卡片看起来比 480x320 示例中的小,因为相机更大,但卡片保持原始大小。但是,如果您不想缩放和定位每个精灵,请使用一个固定的相机尺寸,让 AndEngine 完成剩下的工作。
      • 另外 - 由于白色条纹通常不受欢迎,您可以使用应用程序的 Theme 属性将它们更改为您选择的任何颜色,例如黑色。 stackoverflow.com/questions/11779679/…。更改 ratioResolutionPolicy 也会消除条形图,但根据您的布局可能会导致其他显示问题,因为在一台设备上可见的某些屏幕在另一台设备上不可见。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-09
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多