Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 476
CodingM Avatar asked Dec 06 '25 03:12

CodingM


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

like image 85
realpranav Avatar answered Dec 08 '25 17:12

realpranav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!