2016-01-20 64 views
4

我正在向android地图视图添加自定义标记。这里是我使用它的图像。它看起来像更多的标记放置说约100整个应用程序变得缓慢。这是我正在使用的图像。在android中使用XML绘制自定义形状

enter image description here

而不是使用这个形象,我打算画这个图像作为XML的形状。怎么做?

此外,我正在关注this tutorial以显示自定义标记。这是自定义绘图延迟应用程序?

最好的办法是做什么?

+0

这是否需要自动调整? – Yorrd

+0

不,我不想调整 – Zach

回答

3

这是可能的,但看起来没有意义。因为Google地图只需要位图。所以你需要创建位图并在画布的帮助下绘制形状。

marker.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 

<item 
    android:width="20dp" 
    android:height="20dp" 
    android:top="10dp"> 
    <rotate 
     android:fromDegrees="45" 
     android:pivotX="75%" 
     android:pivotY="40%"> 
     <shape> 
      <solid android:color="#fe696d" /> 
     </shape> 
    </rotate> 
</item> 

<item 
    android:width="@dimen/marker_width" 
    android:height="@dimen/marker_height" 
    android:bottom="8dp"> 
    <shape> 
     <solid android:color="#4b4b4b" /> 
     <corners 
      android:topLeftRadius="@dimen/marker_corner_radius" 
      android:topRightRadius="4dp" /> 
    </shape> 

</item> 


<item 
    android:width="32dp" 
    android:height="26dp" 
    android:bottom="4dp" 
    android:top="16dp"> 
    <shape> 
     <solid android:color="#fe696d" /> 
     <corners 
      android:bottomLeftRadius="@dimen/marker_corner_radius" 
      android:bottomRightRadius="@dimen/marker_corner_radius" /> 
    </shape> 

</item> 

的代码形状转换成位图(从与地图片段/活性)

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 
    Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.marker, null); 
    Canvas canvas = new Canvas(); 
    int width = getResources().getDimensionPixelOffset(R.dimen.marker_width); 
    int height = getResources().getDimensionPixelOffset(R.dimen.marker_height); 
    drawable.setBounds(0, 0, width, height); 
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    canvas.setBitmap(bitmap); 
    drawable.draw(canvas); 
    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(bitmap); 
    LatLng sydney = new LatLng(-34, 151); 
    mMap.addMarker(new MarkerOptions().icon(bitmapDescriptor).position(sydney).title("Marker in Sydney")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 

dimen.xml

<resources> 
    <dimen name="marker_corner_radius">4dp</dimen> 
    <dimen name="marker_height">40dp</dimen> 
    <dimen name="marker_width">32dp</dimen> 
</resources> 

部分

结果将是未来

enter image description here

+0

以节省时间其他开发人员,谁将试图找出这 – gio

+1

在Android Studio预览不会正确显示此形状,只有建立和运行 – gio

1

最简单的方法就是包含一个小的PNG或JPG标记。绘制速度会更快,并且不会占用太多的空间。我不认为你可以在xml中获得这种形状 - 底部的小三角形使它变硬,形状非常有限。你可能会做到部分XML和部分图像,但为什么要打扰XML呢?

相关问题