Animated Transparent Circle on Google Maps v2 is N

2019-04-28 11:45发布

问题:

I was able to animate a basic circle on Android Google Maps v2, but I wanted to take it a step forward. I wanted the animated circle to be similar to Tinders animated circles, is that possible?

Example: http://jsfiddle.net/Y3r36/9/

Right now, I am getting a fidgety animation that is very unacceptable. Here is my code:

public void onMapReady(GoogleMap map) {
        double latitude = 33.750587;
        double longitude = -84.4199173;

        LatLng latLng = new LatLng(latitude,longitude);

        map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        map.animateCamera(CameraUpdateFactory.zoomTo(13));
        map.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title("Marker"));


        final Circle circle = mMap.addCircle(new CircleOptions()
                .center(new LatLng(latitude,longitude))
                .radius(50).fillColor(0x5500ff00).strokeWidth(0)
        );
        ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setIntValues(0, 100);
        valueAnimator.setDuration(1000);
        valueAnimator.setEvaluator(new IntEvaluator());
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float animatedFraction = valueAnimator.getAnimatedFraction();
                Log.e("", "" + animatedFraction);
                circle.setRadius(animatedFraction * 100);
            }
        });
        valueAnimator.start();

I'm open to any suggestions. Thank you

回答1:

The fidgety animation is a known issue. To work around this problem you could use a GroundOverlay instead of the circle.

Here's a sample to obtain the effect you mentioned:

private static final int DURATION = 3000;
private void showRipples(LatLng latLng) {
    GradientDrawable d = new GradientDrawable();
    d.setShape(GradientDrawable.OVAL);
    d.setSize(500,500);
    d.setColor(0x5500ff00);
    d.setStroke(0, Color.TRANSPARENT);

    Bitmap bitmap = Bitmap.createBitmap(d.getIntrinsicWidth()
            , d.getIntrinsicHeight()
            , Bitmap.Config.ARGB_8888);

    // Convert the drawable to bitmap
    Canvas canvas = new Canvas(bitmap);
    d.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    d.draw(canvas);

    // Radius of the circle
    final int radius = 50;

    // Add the circle to the map
    final GroundOverlay circle = map.addGroundOverlay(new GroundOverlayOptions()
            .position(latLng, 2 * radius).image(BitmapDescriptorFactory.fromBitmap(bitmap)));

    // Prep the animator   
    PropertyValuesHolder radiusHolder = PropertyValuesHolder.ofFloat("radius", 0, radius);
    PropertyValuesHolder transparencyHolder = PropertyValuesHolder.ofFloat("transparency", 0, 1);

    ValueAnimator valueAnimator = new ValueAnimator();
    valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
    valueAnimator.setRepeatMode(ValueAnimator.RESTART);
    valueAnimator.setValues(radiusHolder, transparencyHolder);
    valueAnimator.setDuration(DURATION);
    valueAnimator.setEvaluator(new FloatEvaluator());
    valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            float animatedRadius = (float) valueAnimator.getAnimatedValue("radius");
            float animatedAlpha = (float) valueAnimator.getAnimatedValue("transparency");
            circle.setDimensions(animatedRadius * 2);
            circle.setTransparency(animatedAlpha);
        }
    });

    // start the animation
    valueAnimator.start();
}

Now for the ripple effect :

showRipples(latLng);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            showRipples(latLng);
        }
    }, DURATION - 500);

UPDATE : This issue has been fixed



回答2:

Apparently there are two github libraries that can be used to create this ripple effect on markers, if used correctly:

https://github.com/skyfishjy/android-ripple-background

https://github.com/markushi/android-circlebutton



回答3:

Im using this library:

See the marker

Just:

private void initRadar(Location location){
    mapRadar = new MapRadar(map, new LatLng(location.getLatitude(),location.getLongitude()),getContext());
    mapRadar.withDistance(2000);
    mapRadar.withClockwiseAnticlockwiseDuration(2);
    //mapRadar.withOuterCircleFillColor(Color.parseColor("#12000000"));
    mapRadar.withOuterCircleStrokeColor(ContextCompat.getColor(getContext(),R.color.primary));
    //mapRadar.withRadarColors(Color.parseColor("#00000000"), Color.parseColor("#ff000000"));  //starts from transparent to fuly black
    mapRadar.withRadarColors(ContextCompat.getColor(getContext(),R.color.blue_light), ContextCompat.getColor(getContext(),R.color.blue_dark));  //starts from transparent to fuly black
    //mapRadar.withOuterCircleStrokewidth(7);
    //mapRadar.withRadarSpeed(5);
    mapRadar.withOuterCircleTransparency(0.5f);
    mapRadar.withRadarTransparency(0.5f);
    mapRadar.startRadarAnimation();

}


回答4:

its simple guys

first u need to GroundOverlay object and radius object at out of class

 GroundOverlay circle;
 int radius = 100000;

then make valueanimator object and initiate it

 final ValueAnimator valueAnimator = new ValueAnimator();
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.setIntValues(0, radius);
        valueAnimator.setDuration(3000);
        valueAnimator.setEvaluator(new IntEvaluator());
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());

and then use following code to show animatino

circle = mMap.addGroundOverlay(new GroundOverlayOptions()
                        .position(marker.getPosition(), 2 * radius).image(BitmapDescriptorFactory.fromBitmap(drawcircle())));



                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float animatedFraction = valueAnimator.getAnimatedFraction();
                        circle.setDimensions(animatedFraction * radius * 2);
                    }
                });

                valueAnimator.start();


回答5:

according to miss neha's problem

yes thats possible to use with marker

just follow as above answer and

write the animation code in to the OnMarkerClick event

like

 if(circle!=null)
                {
                    circle.remove();
                }


                circle = mMap.addGroundOverlay(new GroundOverlayOptions()
                        .position(marker.getPosition(), 2 * radius).image(BitmapDescriptorFactory.fromBitmap(drawcircle())));



                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator valueAnimator) {
                        float animatedFraction = valueAnimator.getAnimatedFraction();
                        circle.setDimensions(animatedFraction * radius * 2);
                    }
                });

                valueAnimator.start();