0

我有一张要显示为背景的地图图像(固定大小),我想在它上面显示一个播放器图标。我将玩家的当前坐标设为 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);
4

1 回答 1

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 可能太大或太小......:/

于 2016-04-26T19:16:10.443 回答