2016-04-25 28 views
0

我有一張地圖(固定大小)的圖像作爲背景顯示,我想在其上顯示播放器圖標。我將玩家的當前座標視爲android.graphic.Point。Android:在基於座標的地圖上顯示播放器圖標

我試着用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

我解決了這樣的問題:

 //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做小...:/