Android OnDraw ()

I am new to Android and it’s hard for me to understand the concept, so basically this is what I want to do to understand it better.

  • I created a DrawShape class that extends the view. In this class in OnDraw (), I create a circle and fill it with color.

  • From the action, I call the application. / / Until that moment, everything is fine with me.

Now I need to re-draw the circle several times (blue, red, yellow, etc.)

So I read, and the best way is to use Threads. I also read that you need to use postInvalidate () to redraw (I still don’t understand where I should call it) is it called from Activity ?, or inside OnDraw () ?.

I hope you understand what I want to achieve, it is only that I havent found a good tutorial that shows how to repaint something x times. When I do Thread.sleep () everything stops, then it shows my app .. but now I understand why, because I play with the main Thread.

Please help me figure this out.

thank

+3
source share
3 answers

I did something like this

animcolor()
{
Timer timer = new Timer();
int delay =  ...;
int period = ...;
timer.schedule(new TimerTask(){
   run() { 
         setColor( randomint() ); ) 
         customview.postInvalidate();
   }
}, delay, period);

subject? no need to create them; Timers work well on concurrency.

... and the code will be somewhat similar ....

res/layout/file.xml

<org.customviewlayout a:id="@+id/customlayout"/>



src/org.MyActivity.java


class MyActivity 
{
  onCreate()
  {
    customlayout = findViewById(R.id.customlayout);
    customlayout.animcolor();
   }
}

src/org.customlayout.java

import org.customview;
class customlayout
{
  customview;

  customlayout(context, attrs)
  {
    customview = new customview();
    addview(customview);  // so it onDraw() method will be called
  }

  onlayout(...)
  {
    customview.layout(...);
  }

  animcolor()
  {
    Timer timer = new Timer();
    int delay =  ...;
    int period = ...;
    timer.schedule(new TimerTask(){
       run() { 
           setColor( randomint() ); ) 
           customview.postInvalidate();
       }
    }, delay, period);

  }

  setcolor(int)
  {
    ....
  }
}
+3
source

I think you can do this using a timer and a timer in your activity. TimerTask works with the delay you specify, and when you run all you need to do is yourDrawShapeInstance.postInvalidate ();

, ui, postInvalidate(), invalidate ui, ui- , , onDraw DrawShape .

( , )

TimerTask task = new TimerTask(){
  public void run(){
    myDrawShapeInstance.postInvalidate();
  }
}
0

( - ). , , , . , .

void drawCircleToCanvas(int color)
{
final Handler handler = new Handler() {
           public void handleMessage(Message msg) {
              myDrawShapeInstance.postInvalidate();
              }
           };
        Thread updateUI = new Thread() {  
           public void run() {

           //************draw something here***************

              handler.sendEmptyMessage(0);
              }
           };
          updateUI.start();
}
0

Source: https://habr.com/ru/post/1789987/


All Articles