2016-04-25 16 views
0

マップの画像(固定サイズ)を背景として表示し、その上にプレーヤーのアイコンを表示したい。私はandroid.graphic.Pointとしてプレーヤーの現在の座標を取得しています。Android:地図上にアイコンでプレイヤーのアイコンを表示

マージンを設定してRelativeLayoutを試しましたが、これはうまく機能しましたが、ディスプレイを回転させるとアイコンは明らかに別の場所に移動します。また、私はこの絶対的な配置は、異なる画面サイズで動作することはできませんと思う...

どのようにこれを意図して行うにはどのような提案?

これは

<?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> 

私のXMLであり、これは私がどのように(再)プレーヤーのアイコン

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に基づいて大きすぎるか小さすぎる可能性があります:/

関連する問題