I am trying to make a rainbow colored title (which changes with time), here is the onCreate:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
new Thread(TitleColorRunnable).start();
}
And the corrosponding runnable:
Runnable TitleColorRunnable = new Runnable()
{
TextView title;
int titleLength;
Spannable spannable;
int i;
float mainHue = 15;
float hue;
int color;
@Override
public void run()
{
title = findViewById(R.id.titleTextView);
title.setText("TITLE EXAMPLE", TextView.BufferType.SPANNABLE);
titleLength = title.length();
spannable = (Spannable) title.getText();
while (true)
{
for (i = 0; i < titleLength - 1; i++)
{
hue = (mainHue - i) % 360;
color = Color.HSVToColor(new float[]{hue, 1, 1});
title.post(new Runnable()
{
@Override
public void run()
{
spannable.setSpan(new ForegroundColorSpan(color), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
});
}
mainHue++;
if (mainHue == 360)
{
mainHue = 0;
}
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
};
This thread slows down over time, and starts to slowly bug down the entire UI until everything stops completely.
Is it possible that the line
spannable.setSpan(new ForegroundColorSpan(color), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
keep saving new ForegroundColorSpan variables to memory?
Please help, thank you!