4

I'm trying to animate the size of a marker as it is added to a map, basically I want the marker to grow. I can't see any way of getting to the actual view for the marker so I don't think I can use the standard Android animation techniques (e.g. ObjectAnimator).

The only way I can see to do this would be to implement my own animation and use the setIcon method to change the marker icon.

Is there any other and ideally better way of doing this?

I'm working in Xamarin but can port Java code if necessary.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
  • 1
    technically you can use `ObjectAnimator` with for example `"icon"` property provided you have `setIcon` method in your `target` Object and create it as follows: `ObjectAnimator.ofInt(this, "icon", 10, 100)` – pskink Sep 09 '15 at 08:58
  • @pskink You're right, this is the approach I ended up going with. Post your comment as an answer and I'll accept. – Ian Newson Sep 09 '15 at 14:03

3 Answers3

8

You may try something like this

final Marker marker = map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_temperature_kelvin_black_48dp);
final Bitmap target = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(target);
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.setDuration(500);
animator.setStartDelay(1000);
final Rect originalRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF scaledRect = new RectF();
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        float scale = (float) animation.getAnimatedValue();
        scaledRect.set(0, 0, originalRect.right * scale, originalRect.bottom * scale);
        canvas.drawBitmap(bitmap, originalRect, scaledRect, null);
        marker.setIcon(BitmapDescriptorFactory.fromBitmap(target));
    }
});
animator.start();
Evgenii Vorobei
  • 1,457
  • 18
  • 20
dtx12
  • 4,438
  • 1
  • 16
  • 12
1

Yes you are right there is not direct method to do it. Instead, you can use handler that will get called after say every 500ms and in that you can setIcon of marker.

Use this link for reference; How to animate marker in android map api V2?

Community
  • 1
  • 1
0

For vector drawables

fun Drawable.getBitmapFromVectorDrawable(): Bitmap {
val bitmap = Bitmap.createBitmap(
    intrinsicWidth,
    intrinsicHeight, Bitmap.Config.ARGB_8888
)

val canvas = Canvas(bitmap)
setBounds(0, 0, canvas.width, canvas.height)
draw(canvas)

return bitmap

}

Then do

val marker = map.addMarker(MarkerOptions().position(LatLng(latitude, longitude)))
            val bitmap = requireContext().requireDrawable(R.drawable.marker)!!
                .getBitmapFromVectorDrawable()
            ValueAnimator.ofInt(1, bitmap.width).apply {
                duration = resources.getInteger(R.integer.itr_normal_anim_time).toLong()
                interpolator = OvershootInterpolator()
                addUpdateListener { animation ->
                    val scale = animation.animatedValue as Int
                    val scaledBitmap = bitmap.scale(scale, scale)
                    marker!!.setIcon(BitmapDescriptorFactory.fromBitmap(scaledBitmap))
                }
                start()
            }