Skip to content

Instantly share code, notes, and snippets.

@Jawnnypoo
Last active May 13, 2020 16:08
Show Gist options
  • Save Jawnnypoo/f2b85568b6a034590f24 to your computer and use it in GitHub Desktop.
Save Jawnnypoo/f2b85568b6a034590f24 to your computer and use it in GitHub Desktop.
Use ObjectAnimator to change color of an icon dynamically
<?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="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/button_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Change To Red" />
<Button
android:id="@+id/button_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Change To Blue" />
<ImageView
android:id="@+id/color_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="@null"
android:src="@drawable/ic_android" />
</LinearLayout>
Generate Asset here:
http://romannurik.github.io/AndroidAssetStudio/icons-generic.html#source.type=clipart&source.space.trim=0&source.space.pad=0&source.clipart=res%2Fclipart%2Ficons%2Fnotification_adb.svg&size=54&padding=8&color=fff%2C100&name=ic_android
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();
}
};
}
@ali-bagheri
Copy link

Very nice,tanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment