Home > Mobile >  Small white request, help bosses how slowly one after another shows ten round?
Small white request, help bosses how slowly one after another shows ten round?

Time:11-09

I practice to write the code as follows, but it can only be a one-off shows 10 circle, how to change to make it can slowly one by one, thank you bosses
 public class methods like DrawCircle extends the View 
{
Paint p;
Int color;
Int x=0;
int y=0;
Int r=0;
Int n=0;

Public methods like DrawCircle (Context con, AttributeSet attr)
{
Super (con, attr);
P=new Paint, Paint. ANTI_ALIAS_FLAG);
}

@ Override
Protected void ontouch (Canvas c)
{
Super. Ontouch (c);
While (n<10)
{
Gen ();
P. etColor (color);
P. etStyle (Paint. Style. The FILL);
C.d rawCircle (x, y, r, p);
Sleep (500);
N++;
}
}

Public void gen ()
{
Color=color RGB (rand (0255), the rand (0255), the rand (0255));
X=rand (0120);
Y=rand (0120);
R=rand (50150);
}

Public int rand (int a, int b)
{
Return ((int) ((b - a + 1) * math.h random () + a));
}

Public void sleep (ms)
{
Try
{
Thread.sleep (ms).
}
Catch InterruptedException (e)
{
e.printStackTrace();
}
}
}

Below is the simpler MainActivity
 public class MainActivity extends AppCompatActivity {

Methods like DrawCircle methods like DrawCircle.

@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Methods like drawCircle=new methods like drawCircle (this, null);
The setContentView (methods like drawCircle);
}
}

CodePudding user response:

Android all drawing are in one thread, if you sleep will be stuck, does not draw, the reason is simple: single-threaded execution code behind the sleep?
The main thread delay approach is the most common handler,
Train of thought is: handler delay 500, didn't finish the Activity and n<10 calls invalid, the draw according to the number of n circle,

Say come back, novice should practice writing layout, a custom view this kind of waiting for you all sorts of layout style are tired of writing to learn,

CodePudding user response:

Considering the Thread/AsyncTask Thread count, conditional execution to count invalidate ();
Pay attention to the life cycle maintenance threads, Thread to consider using a Thread pool
  • Related