mathmaniage
mathmaniage

Reputation: 319

drawing directly to canvas and creating layout using java code

I was following the series on canvas drawing , and at 1:27 , the author quotes: "A class responsible for managing and drawing the layout has to extend the SurfaceView and also needs to implement a thread, so we're going to implement the Runnable interface"

The author is implementing a direct draw to canvas without using xml files using java code only and uses a java class instead of xml. He defines the layout in java as:

public class Activity_Animation002_Layout extends SurfaceView implements Runnable{

    Thread thread = null;

    public Activity_Animation002_Layout(Context context){
        super(context);
    }

    @Override
    public void run(){
        while(CanDraw){
        //TODO- carry out drawing....
     }
    }
    //some more code to follow
}  

The associated activity file has code like this:

public class AnimationActivity002 extends Activity{
    Activity_Animation002_Layout animation002_LayoutView;

    @Override
    protected void onCreate(Bundle savedInstance){
        super.onCreate(savedInstanceState);
        animation002_LayoutView = new Activity_Animation002_Layout(this);
        setContentView(animation002_LayoutView);
    }
}  

Is this drawing technique outdated? I mean I have search high and low, and tried to google, checked the pages like: Custom Drawing, Canvas Drawing, Layouts, Layout Resource , SurfaceView but none of them so much as even extend a surfaceView. Where can I find this in the documentation? But if this is an outdated technique or something, then what is the new way of doing the same thing?

Upvotes: 2

Views: 150

Answers (1)

Mwasz
Mwasz

Reputation: 186

  1. You can do this easier, but the thing is about what you really want to achieve here. You can pretty much do the drawing simply in a View class in its onDraw(Canvas).
  2. SurfaceView is a special kind of View. With a SurfaceView you can draw on another Thread (not the UI thread). Thats why your class has to implement Runnable - you have to pass it to a Thread. An example:

    • View draws on UI Thread so if you try to animate something in that View and do some stuff on UI Thread (for example scroll through a layout) it will be laggy
    • SurfaceView draws on another Thread so you can freely animate something and also scroll, click things etc with no lags.
  3. SurfaceView might be also used for games (where a lot is happening on the screen), but i wouldn't say it is heavily used for that nowadays. But a fact is that it is possible to make some dodgy games based on SurfaceView, or something like flappy bird

To summarize:

  • I don't think it is an outdated technique, more like its not commonly used, because you would use it in a specific situation, where you need to draw something (e.g simple animation) without freezing the UI Thread
  • You can also draw in a simple View class, but its ok only if you're not planning to do other actions with the UI. (or if the drawing isn't long). Things from the video you mentioned could be done in a simple View, because there is not much happening there - you just draw the images once and thats it. I even remember i've done a card game entirely with a View class, where you dragged cards to the middle to play.

Upvotes: 3

Related Questions