Skip to content

Instantly share code, notes, and snippets.

@gaborvecsei
Last active July 30, 2016 12:38
Show Gist options
  • Save gaborvecsei/efac476384faefa6945cdc50c4fa0fe0 to your computer and use it in GitHub Desktop.
Save gaborvecsei/efac476384faefa6945cdc50c4fa0fe0 to your computer and use it in GitHub Desktop.
This is a simple code to create view animations (for value changes etc...). Exmaple: http://i.imgur.com/0dtTx9X.gif
<!-- Example: http://i.imgur.com/0dtTx9X.gif -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.tests.gabor.textanimation.MainActivity">
<TextView
android:id="@+id/hello_world_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<Button
android:id="@+id/animate_red_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate Red!"/>
<Button
android:id="@+id/animate_green_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Animate Green!"/>
</LinearLayout>
//Gabor Vecsei
//vecseigabor.x@gmail.com
//Example: http://i.imgur.com/0dtTx9X.gif
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView helloWorldText;
Button animateRedButton;
Button animateGreenButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helloWorldText = (TextView) findViewById(R.id.hello_world_text);
animateRedButton = (Button) findViewById(R.id.animate_red_button);
animateGreenButton = (Button) findViewById(R.id.animate_green_button);
animateRedButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeColor(helloWorldText, "backgroundColor", Color.WHITE, Color.RED, 800, 3);
}
});
animateGreenButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
changeColor(helloWorldText, "backgroundColor", Color.WHITE, Color.GREEN, 800, 3);
}
});
}
private void changeColor(View view, String property, int startColor, int stopColor, int timeInMillis, int blinkingCount){
ObjectAnimator anim = ObjectAnimator.ofInt(view, property, startColor, stopColor, startColor);
anim.setDuration(timeInMillis);
//Avoid blinking
anim.setEvaluator(new ArgbEvaluator());
anim.setRepeatCount(blinkingCount);
anim.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment