我正在尝试在背景上绘制一个级别,但我不确定该怎么做。我可以分别绘制每一块,但完全不知道如何在屏幕上绘制它们。我相信我可能不得不用不同的方法绘制背景,但我不知道这种方法。
到目前为止,这是我的代码:
public class DisplayMap extends Activity {
ImageView mapView;
String FILENAME = "World.tmx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
setContentView(new TileBackground(this));
}
public void loadWorld(String path) {
ImageView mapView;
// Start the parser, get back TMX data object
TileMapData t = TMXLoader.readTMX(FILENAME, this);
mapView = (ImageView) findViewById(R.id.MapImage);
// Create a Bitmap from the tilemap data
Bitmap mapImage = TMXLoader.createBitmap(t, this, 0, t.layers.size());
// Set the imageview to show the map, if we have one
if (mapImage != null) {
mapView.setImageBitmap(mapImage);
}
// Map loading problem, inform the user.
else {
Toast errorMessage = Toast.makeText(getApplicationContext(),
"Map could not be loaded", Toast.LENGTH_LONG);
errorMessage.show();
}
}
class TileBackground extends View {
Bitmap bg;
public TileBackground(Context context) {
super(context);
try {
AssetManager assetManager = context.getAssets();
InputStream inputStream;
inputStream = assetManager.open("Background.png");
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
bg = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onDraw(Canvas canvas) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
int iWidth = bg.getWidth();
int iHeight = bg.getHeight();
for (int x = 0; x < width; x += iWidth) {
for (int y = 0; y < height; y += iHeight)
canvas.drawBitmap(bg, x, y, null);
}
loadWorld(FILENAME);
invalidate();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.seanheiss.shovelshovel.DisplayMap.TileBackground
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/TileBackground" />
<ImageView
android:id="@+id/MapImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
通过评论一个 setContentView 而不是另一个我可以画一个,但我不知道如何画两个。我听说过一种叫做 ViewFlipper 的东西,但不确定我是否应该使用它;我不知道它是如何工作的。
非常感谢任何可以帮助我的人!