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

Viewed 156

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.

2 Answers

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).

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!

Related