2017-04-14 41 views
0

我使用Xamarin构建Android应用程序,我需要能够在地图上放置一些点。所述地图是使用ImageView显示的图像(直接从xml添加它)。 我尝试制作一个位图和画布,它显示一个红色的点(如想),但实际的地图消失。这是我的代码:通过Android中的ImageView绘制点(使用Xamarin)

[Activity(Label = "Map")] 
public class Map : Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Map); 

     ImageView map = FindViewById<ImageView>(Resource.Id.imageView1); 
     Bitmap bitmap = Bitmap.CreateBitmap(500, 500, Bitmap.Config.Rgb565); 
     Paint paint = new Paint(); 
     paint.SetARGB(255, 255, 0, 0); 
     Canvas canvas = new Canvas(bitmap); 

     //Draw the image bitmap into the canvas 
     canvas.DrawBitmap(bitmap, 5, 5, paint); 

     //Draw everything you want into the canvas 
     canvas.DrawCircle(50, 50, 10, paint); 

     //Attach the canvas to the ImageView 
     map.SetImageDrawable((new BitmapDrawable(bitmap))); 

     Button item = FindViewById<Button>(Resource.Id.button1); //Declare item button 
     item.Click += delegate 
     { 
      StartActivity(typeof(Item)); 
     }; 
    } 
} 

有没有人知道问题是什么?

回答

0

我试着做一个位图和画布,它显示一个红色的点(如想),但实际的地图消失。

通过map.SetImageDrawable((new BitmapDrawable(bitmap)));您正在用您创建的bitmap取代地图。

作为速战速决,您可以设置地图的图像作为布局的背景,而不是ImageView来源:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/yourMapImage" 
    tools:context="com.example.winffee.scrollviewdemo.MainActivity"> 
    <ImageView 
     android:id="@+id/imgMap" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
</LinearLayout>