我有一张地图(固定大小)的图像显示为背景,我想在其上显示一个玩家图标。我正在获得玩家当前的coords为android.graphic.Point。
我尝试使用RelativeLayout设置边距,这有点奏效,但是当显示旋转时,图标显然会移动到其他地方。另外我认为这种绝对定位不适用于不同的屏幕尺寸...
有关如何按预期执行此操作的任何建议吗?
这是我的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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/mapLayout"
tools:context="cz.alois_seckar.vseadventrura.MapActivity">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/playerIcon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/player" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mapImage"
android:src="@drawable/spacemap"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back_button"
android:id="@+id/mapBackButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="goBack"/>
</RelativeLayout>
这就是我(重新)放置玩家偶像的方式
RelativeLayout mapLayout = (RelativeLayout) findViewById(R.id.mapLayout);
mapLayout.removeView(playerIcon);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
Point playerCoords = game.getWorld().getCurrentSpace().getPosition();
params.leftMargin = playerCoords.x;
params.topMargin = playerCoords.y;
mapLayout.addView(playerIcon, params);
答案 0 :(得分:0)
我解决了这个问题:
//Get bitmap to draw
Bitmap playerPic = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(game.getPlayer(), "drawable", getPackageName()));
//Create a new image bitmap and attach a brand new canvas to it
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inMutable = true;
Bitmap mapPic = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(game.getMap(), "drawable", getPackageName()), opts);
Canvas tempCanvas = new Canvas(mapPic);
//Draw the image bitmap into the cavas
Point playerCoords = game.getWorld().getCurrentSpace().getPosition();
tempCanvas.drawBitmap(playerPic, playerCoords.x * mapPic.getWidth() / 10, playerCoords.y * mapPic.getHeight() / 10, null);
//Attach the canvas to the ImageView
ImageView mapImage = (ImageView) findViewById(R.id.mapImage);
mapImage.setImageDrawable(new BitmapDrawable(getResources(), mapPic));
唯一的问题是playerPic的大小 - 可能太大或根据实际的DPI做小......:/