Skip to content

Instantly share code, notes, and snippets.

@bng86
Last active December 14, 2016 10:28
Show Gist options
  • Save bng86/175832ecd2cd50a7ac6dfe2bc2981536 to your computer and use it in GitHub Desktop.
Save bng86/175832ecd2cd50a7ac6dfe2bc2981536 to your computer and use it in GitHub Desktop.
Animator
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="animate"/>
<View
android:id="@+id/cube"
android:background="@color/colorAccent"
android:layout_width="50dp"
android:layout_height="50dp" />
</LinearLayout>
public class AnimatorActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_animator);
Button button = (Button) findViewById(R.id.button);
final View cube = findViewById(R.id.cube);
final ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0, 1.0f);
animator.setDuration(2000);
animator.setRepeatMode(ValueAnimator.REVERSE);
// ValueAnimator.INFINITE 無限重播
// animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new AnticipateOvershootInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float value = (float) valueAnimator.getAnimatedValue();
cube.setRotationX(360 * value);
cube.setTranslationX((100 * value) - 100);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
animator.start();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment