Reputation: 1
I'm doing a project in android studio. I just want to do a flashing light point using onDraw()
and invalidate()
but something is wrong.
This is the first class
public class flashingPoint extends View {
private ShapeDrawable mParteDibujable;
public flashingPoint(Context context){
super(context);
final Handler bridge = new Handler();
Thread time = new Thread() {
public void run() {
bridge.postDelayed(this, 1000);
invalidate();
}
};
time.start();
}
@Override
protected void onDraw(Canvas canvas) {
mParteDibujable = new ShapeDrawable(new OvalShape());
mParteDibujable.getPaint().setColor(0xff74AC23);
mParteDibujable.setBounds(10, 20, 80, 80);
mParteDibujable.draw(canvas);
//invalidate();
}
And then the main class:
public class MainActivity extends AppCompatActivity {
private ShapeDrawable mParteDibujable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mLinearLayout = new LinearLayout(this);
flashingPoint myView = new flashingpoint(this);
mLinearLayout.addView(myView);
setContentView(mLinearLayout);
}
}
Upvotes: 0
Views: 636
Reputation: 93551
If you have the timer, you neither need nor want the invalidate in onDraw. Invalidating in onDraw is both logically weird and will lead to poor results- it would either be ignored, or it would cause an immediate redraw. Neither is desired.
Also, you can't invalidate on a Thread, you'd need to use postInvalidate. And your thread is wrong- either don't use a thread, use a Runnable and post it to the Handler, or the thread should infinitely loop, not return. Preferably the first, there's no reason to have a thread here at all.
Upvotes: 3