Created
October 1, 2016 21:46
-
-
Save subinkrishna/85d58e6e0a255432d391700e8f08795e to your computer and use it in GitHub Desktop.
Sample - Height animation using ObjectAnimator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.animation.ObjectAnimator; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Property; | |
import android.view.View; | |
import android.view.animation.DecelerateInterpolator; | |
import android.widget.FrameLayout; | |
public class MainActivity extends AppCompatActivity { | |
// Log tag | |
private static final String TAG = "MainActivity"; | |
boolean isExpanded = true; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
final FrameLayout headerLayout = (FrameLayout) findViewById(R.id.header); | |
headerLayout.setOnClickListener(new View.OnClickListener() { | |
@Override public void onClick(View v) { | |
animateHeightTo(headerLayout, isExpanded | |
? (v.getHeight() / 3) | |
: ((View) v.getParent()).getHeight()); | |
isExpanded = !isExpanded; | |
} | |
}); | |
} | |
private void animateHeightTo(@NonNull View view, int height) { | |
final int currentHeight = view.getHeight(); | |
ObjectAnimator animator = ObjectAnimator.ofInt(view, new HeightProperty(), currentHeight, height); | |
animator.setDuration(300); | |
animator.setInterpolator(new DecelerateInterpolator()); | |
animator.start(); | |
} | |
static class HeightProperty extends Property<View, Integer> { | |
public HeightProperty() { | |
super(Integer.class, "height"); | |
} | |
@Override public Integer get(View view) { | |
return view.getHeight(); | |
} | |
@Override public void set(View view, Integer value) { | |
view.getLayoutParams().height = value; | |
view.setLayoutParams(view.getLayoutParams()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment