Download a Libgdx game in Fragment showing a black screen on Android

The following code shows how I load the game Libgdx (MyGdxGame) in LinearLayout. on creating the game, I set up the camera and uploaded a small wav sound file. The texture is loaded after the view is loaded.

The problem is the passage of the fragment to this fragment, a noticeable black flash and delay are observed. Is it possible to reduce this somehow and improve the transition of the fragment.

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = null;
    Bundle bundle = getArguments();
    if (bundle != null) {
        this.input = bundle.getParcelable(Utils.FRAGMENT_INPUT_KEY);
    } else {
        input = new FragmentInput();
    }

    view = inflater.inflate(R.layout.solve, container, false);
    surface = (LinearLayout) view.findViewById(R.id.puzzleView);

    activity = (MainActivity) getActivity();

    com.badlogic.gdx.graphics.Color color = new com.badlogic.gdx.graphics.Color(Color.red(input.backColor) / 255f,
            Color.green(input.backColor) / 255f,
            Color.blue(input.backColor) / 255f, 1.0f);
    size = input.pieceCount;
    cols = input.columns;
    GameData gdata = new GameData();
    gdata.rows = size / cols;
    gdata.columns = cols;
    gdata.backColor = color;
    gdata.setCategory(input.category);
    if (input.rotated) {
        gameView = new MyGdxGameRotate(this, gdata);
    } else {
        gameView = new MyGdxGame(this, gdata);
    }

    if (surface != null) {
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        View game = initializeForView(gameView, config);
        surface.addView(game);
    }
    return view;
}
+4
source share
2 answers

onCreateViewcalled before the fragment is shown, doing the hard work here means that you are delaying the display of this view (“black flash”), instead you should start by transferring the hard work to onResumeor at leastonViewCreated

-, , . Android - AsyncTask, , .

public void onViewCreated() {
    new AsyncTask<Void, Void, Void>() {
        public Void doInBackground() {
            //Do your loading
        }

        public void onPostExecute(Void aVoid) {
            //Update display, play sound (on resumed fragment)
        }
    }.execute();
}

Android,

+1

Libgdx, Libgdx. , , , , Framelayout ImageView, puzzleView, , xml. onCreateView , , puzzleView, . , , onResume ( ), , , , puzzleView. - , , onResume() , () . , , , , , onCreateView, ( ) Runnable, runOnUiThread() .

+1
source

Source: https://habr.com/ru/post/1694448/


All Articles