It is possible to move a tiled background? (infinite scroll with TileMode.REPEAT)
Asked Answered
U

1

9

I am developing a game

First of all my game will be very simple, so i would prefer not to use libraries or opengl or canvas, i would prefer to achieve this with simple imageviews or normal views, or at least by the simplest way possible. For example with android bitmap tile mode:

public static void setBackground(Bitmap bmp, ViewGroup layout){
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
    bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    layout.setBackgroundDrawable(bitmapDrawable);
}

I want to develop a simple 2D/3D hybrid spaceship game. The screen will be the vision from the spaceship cockpit, and you will move the spaceship touching a joystick that i have allready done. So the background will be a universe background, i mean a huge black image with stars, and this image must be moved to all directions with the joystick simulating that the spaceship is changing it's direction, and when you move a lot to the right for example, the background image must start from the begining to simulate that the space ship is still moving to the right without any limitation

As i have mentioned, i have the joystick now, and i know how to move a imageview with the joystick, the problem is that i dont know how to create a infinite imageview with tiles correctly, because when i move the background containing the tile imagedrawable then the image dissapears from the screen.

This is an example of an image for using with the infinite background:

enter image description here

Uxoricide answered 31/1, 2015 at 10:4 Comment(1)
have you find any solution?Compensatory
R
0

I had a similar problem and found a solution which uses only XML. In a vertical ScrollView that has a height of 50000dp, my tiled background image repeats as much as needed to fill this height. As a result, scrolling would create the desired effect of stars flying by (using your image as an example).

  • Use a ScrollView, its background does not matter. Height set to wrap_content.
  • Inside this ScrollView, place another layout (I happen to have a ConstraintLayout). This is the background that matters. Height set to wrap_content. Set the background of this to: android:background="@drawable/repeating_background"
  • Create a new drawable called repeating_background.xml with the following code:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/stars_image"
    android:tileModeY="repeat"
    />
  • where tileModeY="repeat" makes stars_image (png, jpeg, etc.) repeat only vertically. You would want to use tileMode="repeat" since that can handle X and Y. tileMode="mirror" is something you could explore since it reflects the image to get rid of the appearance of any seams if the background color changes.

That's it! If you had set this same background to the ScrollView itself, then it would simply tile your image, but this tiled background would remain static while the contents of your ScrollView scrolled.

Renovate answered 28/3, 2020 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.