Wander through map of Birmingham
This app is very specific! It wanders through a map of central Birmingham, or of Dudley (using a spinner - a drop down menu). It can easily be altered to wander through any large image. Click on the edges to move it that direction.
app/res/drawable - pictures
The maps are broken into a number of files all 150x150 pixels. Starting from top left hand corner, the Birmingham names are top row: "brum0101", "brum0201", brum0301", etc. Next row: "brum0102", "brum0202", brum0302", etc. Next row: "brum0103", "brum0203", brum0303", etc. Etc. So the numbers represent x,y co-ordinates. There are a similar set of Dudley names, starting with "dudley0101".
In app/java/com.(the project name)/MainActivity, the number of pictures is xmax and ymax and where you start is x and y. "district" is 0 for Birmingham and 1 for Dudley. Birmingham is the default map.
app/res/mipmap/ic_launcher.png/ - icon for launching app
app/res/values/strings.xml - strings
Birmingham maps
map
"brum"
"dudley"
app/res/layout/activity_main.xml - screen format
app/java/com.(the project name)/MainActivity - main code for app
package com.edkins.brummap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
int xmax = 24;
int ymax = 24;
int x = 9;
int y = 6;
int district = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.mapDistrict);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.districts, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
redo();
}
// Redo screen (with new position
public void redo() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
String screenName = getString(R.string.nameBrum);
screenName = screenName + String.format("%01d", i+1);
screenName = screenName + String.format("%01d", j+1);
String mapName;
if (district==0) {mapName = getString(R.string.nameBrum);}
else {mapName = getString(R.string.nameDudley);}
mapName = mapName + String.format("%02d", i+x);
mapName = mapName + String.format("%02d", j+y);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.brummap);
ImageView bit = (ImageView) rl.findViewWithTag(screenName);
int id = getResources().getIdentifier(mapName, "drawable", getPackageName());
bit.setImageResource(id);
}
}
}
// Move Left and Up
public void moveLeftUp(View view) {
if (x>1) {x--;}
if (y>1) {y--;}
redo();
}
// Move Up
public void moveUp(View view) {
if (y>1) {y--;}
redo();
}
// Move Right and Up
public void moveRightUp(View view) {
if (x<(xmax-2)) {x++;}
if (y>1) {y--;}
redo();
}
// Move Left
public void moveLeft(View view) {
if (x>1) {x--;}
redo();
}
// Move Right
public void moveRight(View view) {
if (x<(xmax-2)) {x++;}
redo();
}
// Move Left and Down
public void moveLeftDown(View view) {
if (x>1) {x--;}
if (y<(ymax-3)) {y++;}
redo();
}
// Move Down
public void moveDown(View view) {
if (y<(ymax-3)) {y++;}
redo();
}
// Move Right and Down
public void moveRightDown(View view) {
if (x<(xmax-2)) {x++;}
if (y<(ymax-3)) {y++;}
redo();
}
// Select a district
public void onItemSelected(AdapterView> parent, View view, int pos, long id) {
if (pos==0) {
xmax = 24; ymax = 24;
x = 9; y = 6;
district = 0;
}
else if (pos==1) {
xmax = 23; ymax = 11;
x = 7; y = 1;
district = 1;
}
redo();
}
// This code is needed, but not used
public void onNothingSelected(AdapterView> parent) {
}
}
Index to my other websites
© Jo Edkins 2016