如何在不使用可绘制文件的情况下在自定义图像标记周围添加环以指向地图

时间:2016-09-12 19:37:24

标签: android google-maps google-maps-markers google-maps-android-api-2 android-custom-view

want marker like in the image. i have made a custom marker with a circular image but unable to add the ring around the circular image pointing down programmatically.

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

}


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap)
{
    mMap = googleMap;

    Bitmap scaled = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),
            R.drawable.ab), 120, 120, true);

    Bitmap image=getCircularBitmap(scaled);

//修改画布     // canvas1.drawBitmap(缩放,0,0,颜色);

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(sydney);
    markerOptions.icon(BitmapDescriptorFactory.fromBitmap(image));

    mMap.addMarker(markerOptions);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

public Bitmap getCircularBitmap(Bitmap bitmap)
{
    Bitmap output;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();

    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output

1 个答案:

答案 0 :(得分:0)

开发人员必须使用将在documentation中指示的BitmapDescriptorFactory传递的drawable。图像周围的环应该是图像本身的一部分,如果它是多色的(如提供的图像上),您可以使用多个模板作为标记。

希望这有帮助!