将Android Google Maps v2与自定义View或ImageView一起使用 [英] Using Android Google Maps v2 with custom View or ImageView

查看:69
本文介绍了将Android Google Maps v2与自定义View或ImageView一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我希望能够使用Google Maps v2来 在地图上绘制雷达图像,效果很好 性能,并且没有位图图像的闪烁或延迟.

Basically I want to be able to use Google Maps v2 to draw RADAR images over the Map, with good performance and no flickering or delay of the Bitmap images.

我目前正在使用Maps v1执行此操作,效果很好,但是Maps v2 不太适合这样做.

I'm currently using Maps v1 to do this, and it works great, but Maps v2 is not well suited to do that.

您会认为GroundOverlay或TileOverlay可能有效, 但从我从尝试使用它们的人那里读到的内容来看, 您必须先删除()上一张图片,然后再绘制下一张图片, 或做一些尝试将位图图像绘制为标记的操作 或者其他的东西.它必须具有良好的性能.

You would think that GroundOverlay or TileOverlay might work, but from what I've read from people who have tried using them, you have to remove() the previous image before you draw the next one, or do some kludge of trying to draw the Bitmap image as a Marker or something. It has to have good performance.

因此,我希望能够使用自定义视图或ImageView 并使用它的onDraw()绘制位图并使它在地图上可见 并随之移动.

So I'd like to be able to use either a custom View, or an ImageView and use it's onDraw() to draw the Bitmap and have it be visible on the Map and move with it.

赞:

<Outer Container>

    <fragment .. />

    <com.my.customView .. />

    or

    <ImageView .. />

</Outer Container>

我在网上到处都在寻找可能的例子 Maps v2如何执行类似操作的详细信息,但已经发现 什么都没有.

I've looked everywhere over the web for possible examples of the details of how to do something like that, with Maps v2, but have found nothing.

我在当前应用程序中使用了几个自定义视图 所以我很熟悉这些,而且我有位图的边界框详细信息 而且我很确定我可以使用投影信息 帮助绘制它们.

I use several custom Views in my current application so I'm familiar with those, and I have the Bounding Box details of the Bitmaps and I'm pretty sure that I can use the Projection information to help draw them.

另一个问题是如何调用invalidate() onDraw()将被调用.

Another issue is how to call invalidate() so that the onDraw() will be called.

我只需要一些示例代码来展示如何使用onDraw() 从某种基于View的对象中,我可以尝试查看它是否有效, 但是不尝试使用"GroundOverlay","TileOverlay"或"Marker"的广告, 我真的很感激!

I just need some example code that would show how to use the onDraw() from some kind of a View based object that I could try to see if it works, but one that doesn't try to use 'GroundOverlay', 'TileOverlay' or "Marker', I'd really appreciate it!

谢谢!

推荐答案

如果您熟悉 MapView 类,用于完全控制视图画布上的绘制.但是由于MapView扩展了FrameLayoutViewGroup,因此您应该覆盖 dispatchDraw() 方法,而不是onDraw()方法,并在其中实现雷达绘图.像这样的东西:

If you familiar with implementing custom view, you can create custom view which extends MapView class for full control of drawing on view canvas. But because MapView extends FrameLayout which is ViewGroup, you should override dispatchDraw() method, not onDraw() and implement within it radar drawing. Something like that:

@Override
public void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    canvas.save();
    drawRadarOverTheMap(canvas);
    canvas.restore();
}

,并且您需要在每次地图移动/缩放/旋转时通过invalidate()调用它.要检测地图的移动/缩放/旋转,您需要 GoogleMap (恰好

and you need to call it via invalidate() on every map move/zoom/rotate. For detect map move/zoom/rotate you need GoogleMap (exactly GoogleMap.setOnCameraMoveListener() method). You can declare GoogleMap object inside your custom MapView class and set it via setter, but better if custom MapView class will implement OnMapReadyCallback interface and get it in onMapReady() callback.

基于MapView的自定义视图的完整源代码(例如RadarMapView)可以像这样:

Full source code of MapView-based custom view (e.g. RadarMapView) can be like that:

public class RadarMapView extends MapView implements OnMapReadyCallback {

    private OnMapReadyCallback mMapReadyCallback;
    private GoogleMap mGoogleMap;
    private Marker mMarker;
    private Paint mPaintRadar;

    public RadarMapView(@NonNull Context context) {
        super(context);
        init();
    }

    public RadarMapView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RadarMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public RadarMapView(@NonNull Context context, @Nullable GoogleMapOptions options) {
        super(context, options);
        init();
    }

    @Override
    public void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        drawRadarOverTheMap(canvas);
        canvas.restore();
    }

    private void drawRadarOverTheMap(Canvas canvas) {
        if (mGoogleMap == null) {
            return;
        }

        final float centerX = getX() + getWidth() / 2;
        final float centerY = getY() + getHeight() / 2;

        canvas.drawCircle(centerX, centerY, 150, mPaintRadar);
        canvas.drawCircle(centerX, centerY, 300, mPaintRadar);
        canvas.drawCircle(centerX, centerY, 450, mPaintRadar);
    }

    private void init() {
        setWillNotDraw(false);

        mPaintRadar = new Paint();
        mPaintRadar.setColor(Color.GREEN);
        mPaintRadar.setStyle(Paint.Style.STROKE);
        mPaintRadar.setStrokeWidth(10);
    }

    @Override
    public void getMapAsync(OnMapReadyCallback callback) {
        mMapReadyCallback = callback;
        super.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        mGoogleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                invalidate();
            }
        });
        if (mMapReadyCallback != null) {
            mMapReadyCallback.onMapReady(googleMap);
        }
    }

}

您可以在.xml文件中使用它:

And you can use it in .xml file:

<?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"
    tools:context="{your_package_name}.MainActivity">

    <{your_package_name}.RadarMapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</RelativeLayout>

对于MainActivity,例如:

public class MainActivity extends AppCompatActivity {

    private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";

    private GoogleMap mGoogleMap;
    private RadarMapView mMapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
        }

        mMapView = (RadarMapView) findViewById(R.id.mapview);
        mMapView.onCreate(mapViewBundle);
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mGoogleMap = googleMap;
            }
        });

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAP_VIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAP_VIEW_BUNDLE_KEY, mapViewBundle);
        }

        mMapView.onSaveInstanceState(mapViewBundle);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mMapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mMapView.onStop();
    }
    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

}

您应该得到类似的东西:

and you should get something like that:

您还可以通过 canvas.drawBitmap() 方法.

Also you can draw bitmap Radar image on custom view canvas via canvas.drawBitmap() method.

您还可以将ImageView放在MapViewMapFragment上(例如,在RelativeLayout内)

And also you can place ImageView over MapView or MapFragment (e.g. within RelativeLayout)

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment"
    />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/radar"
    />

</RelativeLayout>

并在ImageView上绘制(甚至设置)雷达图像.

and draw (or even just set) Radar images on ImageView.

这篇关于将Android Google Maps v2与自定义View或ImageView一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆