SurfaceView canvas bitmap tile

I am having a problem with a Bitmap tile. I want Bitmap draw the coordinates defined in a 2D Array .

I would like to be able to draw let say "grass" to specific coordinates, and "water, etc." - to other coordinates.

I spent days trying to figure it out, and really appreciate it. I can only get Canvas to draw 1 Bitmap grass, so I feel like I have an error in the for loop. I looked here and here , among many others, and I do not want each tile to be the same. Here is my code:

MapLoader.java

 public class MapLoader extends SurfaceView implements SurfaceHolder.Callback, Runnable { SurfaceHolder holder; Thread thread; Bitmap grass = BitmapFactory.decodeResource(getResources(), R.drawable.grass); boolean running = false; int[][] grassCoords = new int[][] { { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 } }; public MapLoader(Context context) { super(context); holder = getHolder(); holder.addCallback(this); } public MapLoader(Context context, AttributeSet attrs) { super(context, attrs); holder = getHolder(); holder.addCallback(this); } public MapLoader(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); holder = getHolder(); holder.addCallback(this); } public void pause() { running = false; while (running) { try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } break; } thread = null; } public void resume() { running = true; thread = new Thread(this); thread.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { running = true; thread = new Thread(this); thread.start(); } @Override public void surfaceCreated(SurfaceHolder holder) { Canvas c = holder.lockCanvas(); draw(c); holder.unlockCanvasAndPost(c); } @Override public void surfaceDestroyed(SurfaceHolder holder) { } @Override public void run() { while (running == true) { // performs drawing to the canvas if (!holder.getSurface().isValid()) { continue; } Canvas c = holder.lockCanvas(); int x = 0; int y = 0; for (x = 0; x < grassCoords.length; x += grass.getWidth()) { for (y = 0; y < grassCoords.length; y += grass.getHeight()) { c.drawBitmap(grass, x, y, null); } } holder.unlockCanvasAndPost(c); } } } 

ActivityClass.java

 public class Test extends Activity { MapLoader mapLoader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mapLoader = new MapLoader(this); setContentView(mapLoader); } } 

Any help or suggestions (even a link to an effective method) will be greatly appreciated!

Thanks,

Matt

+4
source share
2 answers

It’s not easy to understand what you are trying to do ...

How do you encode the coordinates into this grassCoords array? As the current form, it has 5x5 elements.

 int[][] grassCoords = new int[][] { { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 }, { 0, 16, 32, 48, 64 } }; 

Since he has grass in his name, I assume that you want to draw only grass, then you can define it like this

 int[][] grassCoords = new int[][] { {0, 0}, {16, 16}, {32, 32} }; 

Above each element, such as {0, 0} , there would be one coordinate for the grass tile.

The second problem is related to your loop, you do not read any data from grassCoords except the length of the arrays, and when you increase the index, you increase it with grass.getWidth() , which does not really make sense.

  int x = 0; int y = 0; for (x = 0; x < grassCoords.length; x += grass.getWidth()) { for (y = 0; y < grassCoords.length; y += grass.getHeight()) { c.drawBitmap(grass, x, y, null); } } 

You must mass iterate correctly and extract data from it.

  int x = 0; for (x = 0; x < grassCoords.length; x++) { c.drawBitmap(grass, grassCoords[x][0], grassCoords[x][1], null); } 

If I were you, I would at least once study the related parts of the Java tutorial .

+3
source
 for (x = 0; x < grassCoords.length; x += grass.getWidth()) { for (y = 0; y < grassCoords.length; y += grass.getHeight()) { c.drawBitmap(grass, x, y, null); } } 

The reason he only draws is this bit right here. grassCoords.length - 5 . When you add grass width to x after the first stallion, it exceeds 5 and the outline ends. You need to use a separate variable for these two. The same goes for y . There are other problems associated with this, as Auslen emphasizes, but this is the reason she is just starting from the beginning.

However, you can completely eliminate the array of coordinates if you want to seamlessly draw a rectangle with one bitmap. You do not even need to know how many tiles are width / height. This is especially useful if you use a base layer of grass below other tiles, if you apply a pattern to a background image, etc. You can do it like this:

 for(int x = startX; x < endX; x += grass.getWidth()){ for(int y = startY; y < endY; y += grass.getHeight()){ c.drawBitmap(grass, x, y, null); } } 

Remember that if you just want to fill the tiles at regular intervals, there is no need to determine the coordinates. All of them will be multiple, therefore, the definition of an array of coordinates does not make much sense at all. Just consider each tile spot as a grid point and multiply by the height / width of the tile.

+1
source

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


All Articles