strong textThere is an interface component View (simple rectangle) let's call it "my_view". ...
View myView = (View) findViewById(R.id.my_view);
... I want to programmatically create linear gradient color background for a myView. The two colors of the gradient are set by variables that will change frequently. So I need to find an optimized way. I tried to do this:
myView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
@Override
public boolean onPreDraw()
{
myView.getViewTreeObserver().removeOnPreDrawListener(this);
int view_height = myView.getHeight();
ShapeDrawable myDrawable = new ShapeDrawable(new RectShape());
myDrawable.getPaint().setShader(new LinearGradient(0, 0, 0, view_height, color1, color2, Shader.TileMode.CLAMP));
myView.setBackgroundDrawable(myDrawable);
return false;
}
});
Everything is working. This code should be executed each time when the progress of the Seekbar changes. After testing, I realized that this is a very bad method.Everything works as it should, but lags are noticeable.
Upd: The problem was solved! The best method is to create a Custom Drawable! Thanks to all forum members who gave me tips, especially pskink, who gave the best advice.
