package com.ludomade.test; import android.animation.ArgbEvaluator; import android.animation.ObjectAnimator; import android.app.Activity; import android.graphics.Color; import android.graphics.PorterDuff; import android.os.Bundle; import android.view.View; import android.widget.ImageView; /** * Click a button, change the background color of a view with animation * Created by John on 2/3/2015. */ public class SingleButtonTestActivity extends Activity { private static final int STARTING_COLOR = Color.RED; ImageView colorView; ObjectAnimator colorAnimator; ArgbEvaluator colorEvaluator; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_single_button); colorView = (ImageView) findViewById(R.id.color_icon); colorView.getDrawable().setColorFilter(STARTING_COLOR, PorterDuff.Mode.MULTIPLY); View colorRedButton = findViewById(R.id.button_red); View colorBlueButton = findViewById(R.id.button_blue); colorEvaluator = new ArgbEvaluator(); //Doesn't matter what the last two colors are, since we are going to overwrite them //when the button is pressed colorAnimator = ObjectAnimator.ofObject(colorView, "colorFilter", colorEvaluator, 0, 0); colorRedButton.setOnClickListener(mOnButtonClickListener); colorBlueButton.setOnClickListener(mOnButtonClickListener); } private final View.OnClickListener mOnButtonClickListener = new View.OnClickListener() { @Override public void onClick(View v) { //Get the current color we have animated to //if it exists int currentColor = STARTING_COLOR; if (colorAnimator.getAnimatedValue() != null) { currentColor = (Integer) colorAnimator.getAnimatedValue(); } switch (v.getId()) { case R.id.button_red: colorAnimator.setObjectValues(currentColor, Color.RED); break; case R.id.button_blue: colorAnimator.setObjectValues(currentColor, Color.BLUE); break; } colorAnimator.setDuration(2000); colorAnimator.start(); } }; }