Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to animate map padding in android

I want to animate top map padding of a view programmatically but I have no idea how to do this.

private GoogleMap map;
map.setPadding(left, top, right, bottom);

Anyone have an idea how to animate top padding value from say 0 to 100 ?

like image 776
Rakesh Avatar asked Jun 05 '14 12:06

Rakesh


3 Answers

The map padding is not a view property nor a object property. You can test it by giving a padding to the map view:

<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="10dp"/>

The result is that the map itself is padded which is different from

 map.setPadding(left, top, right, bottom);

setPadding is a method that offsets the UI controls inside the map. There is no property to animate. Luckily android offers the ValueAnimator. Your problem can be solved this way:

ValueAnimator animation = ValueAnimator.ofInt(0, paddingBottom);
animation.setDuration(150);
animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        getMap().setPadding(0, 0, 0, Integer.parseInt(valueAnimator.getAnimatedValue().toString()));
    }
});
animation.start();

This basically animates an integer from 0 to paddingBottom without any context. You create the context in the onAnimationUpdate listener where you assign the animated padding value to the map padding.

like image 60
UpCat Avatar answered Oct 20 '22 21:10

UpCat


You would use Property Animation for that.

like image 41
Rob Avatar answered Oct 20 '22 20:10

Rob


final int newTopMargin = <value>;
Animation anim = new Animation() {

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        LayoutParams params = yourView.getLayoutParams();
        params.topMargin = (int)(newTopMargin * interpolatedTime);
        yourView.setLayoutParams(params);
    }
};

yourView.startAnimation(anim);

You can achieve the animation as described above, it won't be for padding though but it will set the margin with the animation effect you want.

like image 1
CodeWarrior Avatar answered Oct 20 '22 20:10

CodeWarrior