Skip to content

Instantly share code, notes, and snippets.

@benvd
Created January 28, 2013 21:34
Show Gist options
  • Save benvd/4659278 to your computer and use it in GitHub Desktop.
Save benvd/4659278 to your computer and use it in GitHub Desktop.
UNICORN
package be.benvd.unicorn;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int[] COLORS = {0xffff0000, 0xffff7f00, 0xffffff00, 0xff00ff00, 0xff0000ff, 0xff6600ff, 0xff8b00ff};
private static int iteration = 0;
private Handler mHandler;
private Runnable mSetTextRunnable;
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getActionBar().hide();
mTextView = (TextView) findViewById(R.id.myText);
mHandler = new Handler();
mSetTextRunnable = new Runnable() {
@Override
public void run() {
setText();
}
};
setText();
}
private void setText() {
SpannableStringBuilder builder = new SpannableStringBuilder("UNICORN");
for (int i = 0; i < COLORS.length; i++) {
builder.setSpan(new ForegroundColorSpan(COLORS[(iteration + i) % COLORS.length]), i, i + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
mTextView.setText(builder);
iteration = (iteration + 1) % COLORS.length;
mHandler.postDelayed(mSetTextRunnable, 50);
}
}
@vbsteven
Copy link

There is some strange behavior when changing orientation multiple times. the frequency of the color changes seem to increase with every orientation change until at one point it seems to stop and the colors remain static.

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