How can I prevent an ImageView from resizing to fit the screen programmatically?












2















I am making a 2D platformer type game. I made a ImageView using JAVA, that Is supposed to be about twice the size of the screen horizontally.



However, when the ImageView is displayed no matter the size of the Image, it keeps re-sizing to fit my screen . Note that I am not using XML, but I am purely using JAVA. How can I prevent this?



Here is the code:



    ConstraintLayout gameLayout;
gameLayout = findViewById(R.id.gameLayout);


ImageView map = new ImageView(this);
map.setImageResource(R.drawable.tutorial_map);
map.setX(0);
map.setY(0);
gameLayout.addView(map);


Feel free to edit further if necessary.










share|improve this question





























    2















    I am making a 2D platformer type game. I made a ImageView using JAVA, that Is supposed to be about twice the size of the screen horizontally.



    However, when the ImageView is displayed no matter the size of the Image, it keeps re-sizing to fit my screen . Note that I am not using XML, but I am purely using JAVA. How can I prevent this?



    Here is the code:



        ConstraintLayout gameLayout;
    gameLayout = findViewById(R.id.gameLayout);


    ImageView map = new ImageView(this);
    map.setImageResource(R.drawable.tutorial_map);
    map.setX(0);
    map.setY(0);
    gameLayout.addView(map);


    Feel free to edit further if necessary.










    share|improve this question



























      2












      2








      2








      I am making a 2D platformer type game. I made a ImageView using JAVA, that Is supposed to be about twice the size of the screen horizontally.



      However, when the ImageView is displayed no matter the size of the Image, it keeps re-sizing to fit my screen . Note that I am not using XML, but I am purely using JAVA. How can I prevent this?



      Here is the code:



          ConstraintLayout gameLayout;
      gameLayout = findViewById(R.id.gameLayout);


      ImageView map = new ImageView(this);
      map.setImageResource(R.drawable.tutorial_map);
      map.setX(0);
      map.setY(0);
      gameLayout.addView(map);


      Feel free to edit further if necessary.










      share|improve this question
















      I am making a 2D platformer type game. I made a ImageView using JAVA, that Is supposed to be about twice the size of the screen horizontally.



      However, when the ImageView is displayed no matter the size of the Image, it keeps re-sizing to fit my screen . Note that I am not using XML, but I am purely using JAVA. How can I prevent this?



      Here is the code:



          ConstraintLayout gameLayout;
      gameLayout = findViewById(R.id.gameLayout);


      ImageView map = new ImageView(this);
      map.setImageResource(R.drawable.tutorial_map);
      map.setX(0);
      map.setY(0);
      gameLayout.addView(map);


      Feel free to edit further if necessary.







      java android imageview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 15 at 2:58









      Cœur

      19k9113154




      19k9113154










      asked Jan 3 at 0:16









      CodingMCodingM

      172218




      172218
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Use map.setScaleType(ScaleType.FIT_XY);



          or



          You can also use map.setAdjustViewBounds(true);



          Make sure you use map.setBackgroundResource(R.drawable.tutorial_map) instead of map.setImageResource(R.drawable.tutorial_map).






          share|improve this answer


























          • None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

            – CodingM
            Jan 3 at 0:29













          • Maybe you should try with set background rather than set imager resource.

            – Prince
            Jan 3 at 0:36











          • use this map.setBackgroundResource(R.drawable.tutorial_map);

            – Prince
            Jan 3 at 0:39











          • Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

            – CodingM
            Jan 3 at 0:45











          • I have edited my question adding ScreenShots of my android device with your sollutions.

            – CodingM
            Jan 3 at 1:13



















          0














          What framework are you using to build the game. My suggestion would be to use libgdx which is super lightweight, cross platform and awesome. Trust me this question will be the least of your problems. As a developer, you need to think of how your code is going to change tomorrow.
          So, since you are using an imageview, just set as fixed the width of your window and scale your imageview as you wish. Cheers!






          share|improve this answer



















          • 1





            For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

            – CodingM
            Jan 3 at 1:40













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014845%2fhow-can-i-prevent-an-imageview-from-resizing-to-fit-the-screen-programmatically%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Use map.setScaleType(ScaleType.FIT_XY);



          or



          You can also use map.setAdjustViewBounds(true);



          Make sure you use map.setBackgroundResource(R.drawable.tutorial_map) instead of map.setImageResource(R.drawable.tutorial_map).






          share|improve this answer


























          • None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

            – CodingM
            Jan 3 at 0:29













          • Maybe you should try with set background rather than set imager resource.

            – Prince
            Jan 3 at 0:36











          • use this map.setBackgroundResource(R.drawable.tutorial_map);

            – Prince
            Jan 3 at 0:39











          • Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

            – CodingM
            Jan 3 at 0:45











          • I have edited my question adding ScreenShots of my android device with your sollutions.

            – CodingM
            Jan 3 at 1:13
















          1














          Use map.setScaleType(ScaleType.FIT_XY);



          or



          You can also use map.setAdjustViewBounds(true);



          Make sure you use map.setBackgroundResource(R.drawable.tutorial_map) instead of map.setImageResource(R.drawable.tutorial_map).






          share|improve this answer


























          • None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

            – CodingM
            Jan 3 at 0:29













          • Maybe you should try with set background rather than set imager resource.

            – Prince
            Jan 3 at 0:36











          • use this map.setBackgroundResource(R.drawable.tutorial_map);

            – Prince
            Jan 3 at 0:39











          • Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

            – CodingM
            Jan 3 at 0:45











          • I have edited my question adding ScreenShots of my android device with your sollutions.

            – CodingM
            Jan 3 at 1:13














          1












          1








          1







          Use map.setScaleType(ScaleType.FIT_XY);



          or



          You can also use map.setAdjustViewBounds(true);



          Make sure you use map.setBackgroundResource(R.drawable.tutorial_map) instead of map.setImageResource(R.drawable.tutorial_map).






          share|improve this answer















          Use map.setScaleType(ScaleType.FIT_XY);



          or



          You can also use map.setAdjustViewBounds(true);



          Make sure you use map.setBackgroundResource(R.drawable.tutorial_map) instead of map.setImageResource(R.drawable.tutorial_map).







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 6:50

























          answered Jan 3 at 0:22









          PrincePrince

          1,2611030




          1,2611030













          • None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

            – CodingM
            Jan 3 at 0:29













          • Maybe you should try with set background rather than set imager resource.

            – Prince
            Jan 3 at 0:36











          • use this map.setBackgroundResource(R.drawable.tutorial_map);

            – Prince
            Jan 3 at 0:39











          • Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

            – CodingM
            Jan 3 at 0:45











          • I have edited my question adding ScreenShots of my android device with your sollutions.

            – CodingM
            Jan 3 at 1:13



















          • None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

            – CodingM
            Jan 3 at 0:29













          • Maybe you should try with set background rather than set imager resource.

            – Prince
            Jan 3 at 0:36











          • use this map.setBackgroundResource(R.drawable.tutorial_map);

            – Prince
            Jan 3 at 0:39











          • Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

            – CodingM
            Jan 3 at 0:45











          • I have edited my question adding ScreenShots of my android device with your sollutions.

            – CodingM
            Jan 3 at 1:13

















          None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

          – CodingM
          Jan 3 at 0:29







          None of these seem to be working for me. When I try to make my map bigger (MS paint) they just would not show up at all. (Meaby this has to do with the other code I put in, so this may be correct) Edit: Would you like me to provide a screen shot?

          – CodingM
          Jan 3 at 0:29















          Maybe you should try with set background rather than set imager resource.

          – Prince
          Jan 3 at 0:36





          Maybe you should try with set background rather than set imager resource.

          – Prince
          Jan 3 at 0:36













          use this map.setBackgroundResource(R.drawable.tutorial_map);

          – Prince
          Jan 3 at 0:39





          use this map.setBackgroundResource(R.drawable.tutorial_map);

          – Prince
          Jan 3 at 0:39













          Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

          – CodingM
          Jan 3 at 0:45





          Once again, none of these work. I will do an edit showing some screenshots of the thing. Are you testing these out, or just relying on memory? Cause, if you are testing them, this meaby another unrealted problem with my code.

          – CodingM
          Jan 3 at 0:45













          I have edited my question adding ScreenShots of my android device with your sollutions.

          – CodingM
          Jan 3 at 1:13





          I have edited my question adding ScreenShots of my android device with your sollutions.

          – CodingM
          Jan 3 at 1:13













          0














          What framework are you using to build the game. My suggestion would be to use libgdx which is super lightweight, cross platform and awesome. Trust me this question will be the least of your problems. As a developer, you need to think of how your code is going to change tomorrow.
          So, since you are using an imageview, just set as fixed the width of your window and scale your imageview as you wish. Cheers!






          share|improve this answer



















          • 1





            For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

            – CodingM
            Jan 3 at 1:40


















          0














          What framework are you using to build the game. My suggestion would be to use libgdx which is super lightweight, cross platform and awesome. Trust me this question will be the least of your problems. As a developer, you need to think of how your code is going to change tomorrow.
          So, since you are using an imageview, just set as fixed the width of your window and scale your imageview as you wish. Cheers!






          share|improve this answer



















          • 1





            For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

            – CodingM
            Jan 3 at 1:40
















          0












          0








          0







          What framework are you using to build the game. My suggestion would be to use libgdx which is super lightweight, cross platform and awesome. Trust me this question will be the least of your problems. As a developer, you need to think of how your code is going to change tomorrow.
          So, since you are using an imageview, just set as fixed the width of your window and scale your imageview as you wish. Cheers!






          share|improve this answer













          What framework are you using to build the game. My suggestion would be to use libgdx which is super lightweight, cross platform and awesome. Trust me this question will be the least of your problems. As a developer, you need to think of how your code is going to change tomorrow.
          So, since you are using an imageview, just set as fixed the width of your window and scale your imageview as you wish. Cheers!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 1:35









          Rex. ARex. A

          13




          13








          • 1





            For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

            – CodingM
            Jan 3 at 1:40
















          • 1





            For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

            – CodingM
            Jan 3 at 1:40










          1




          1





          For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

          – CodingM
          Jan 3 at 1:40







          For this project, I will be only using Android and Java without any pulg-ins. Thanks for the suggestion though :)

          – CodingM
          Jan 3 at 1:40




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54014845%2fhow-can-i-prevent-an-imageview-from-resizing-to-fit-the-screen-programmatically%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Angular Downloading a file using contenturl with Basic Authentication

          Monofisismo

          Olmecas