高德地圖--水波雷達動畫

Rose-發表於2017-03-14

高德地圖–水波動畫效果

思路:在地圖上繪製三個半徑不同的圓,動態改變他們的半徑實現動畫效果。

    private AMap aMap;
    private int minRadius = 100;
    private int maxRadius = 250;
    private int radius = minRadius;
    private int gap = 8;
    private int duration = 16;
    private List<Circle> listCircle = new ArrayList<>();

    private boolean isMapLongCLick;
    private Latlng longClickLatlng ;
    //長按地圖
    private void onMapLongClick(Latlng latlng){
        isMapLongCLick = true;
        longClickLatlng = latlng;
        showRadar();
    }

    /*雷達*/
    private void showRadar() {
        if (longClickLatlng != null) {

            clearCircle();
            // 繪製不同半徑的圓,新增到地圖上
            final Circle circle = aMap.addCircle(new CircleOptions().center(longClickLatlng)
                    .radius(radius).strokeColor(Color.argb(100, 100, 149, 237))
                    .fillColor(Color.argb(100, 135, 206, 250)).strokeWidth(3));

            final Circle circle1 = aMap.addCircle(new CircleOptions().center(longClickLatlng)
                    .radius(radius-50).strokeColor(Color.argb(100, 100, 149, 237))
                    .fillColor(Color.argb(0, 135, 206, 250)).strokeWidth(3));

            final Circle circle2 = aMap.addCircle(new CircleOptions().center(longClickLatlng)
                    .radius(radius-100).strokeColor(Color.argb(100, 100, 149, 237))
                    .fillColor(Color.argb(0, 135, 206, 250)).strokeWidth(3));

            listCircle.add(circle);
            listCircle.add(circle1);
            listCircle.add(circle2);

            //通過Handler定時重新整理Circle的半徑,達到動畫效果
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (radius < maxRadius) {
                        radius = radius + gap;
                    } else {
                        radius = minRadius;
                    }
                    circle.setRadius(radius);
                    circle1.setRadius(radius-50);
                    circle2.setRadius(radius-100);
                    aMap.invalidate();

                    if (isMapLongCLick)//某種條件下,展示動畫
                        handler.postDelayed(this, duration);
                    else//清除CIrcle
                        clearCircle();
                }
            }, duration);

        }
    }

    private void clearCircle() {
        for (int i = 0; i < listCircle.size(); i++) {
            listCircle.get(i).remove();
            listCircle.remove(i);
        }
    }

相關文章