字幕列表 影片播放 列印英文字幕 Hello, I'm Edu from Edu4Java and this is tutorial number 2 on Android game programming in this tutorial we are going to learn to draw an image in a SurfaceView this is a View of low level which gives us a lot of flexibility and is very efficient which is important for games so that everything happens quickly I am going to make this tutorial following a tutorial which I have in my website edu4java.com so that if anyone get's lost in the way here you have the guide. Let's create a new Android project called killThemall-Training. KillThemall is the name I gave to the small application we are developing this small game ok, here I choose the name of the project Here you can choose what ever you want. We are going to choose the 8. For the application name we are going to name it Killthemall. For the package we are going to put this. Here we are going to write Main And here 7... for compatibility. 7 is for 2.1. With this we can select finish and the project is generated. If you've seen my tutorials in Android you know that a main is generated and also a layout is generated we are not going to use this layout because we want a total control of the application. We are going to interact directly on the view This view instead of coming out of a layout is going to be created by us we create a new gameView I think it was called... we create the class create GameView finish... we have to include the constructors we can choose... we select this one.. we are obliged to choose one because of this we have to put this... the view needs a reference to the activity. Here we have the reference to the activity that would be the Context. Let's help us with the page. In the main we don't have to do anything else. In the View we have the context, let's create a bmp un bitmap ehhh let´s put this here create a bitmap we haven't got the bitmap declared so let's first import the other one. Shift+ctrl+o to import and this one let's create a field there it is ctrl+s to save let's go back to the page because I have already forgot what we were doing.Here we created the bitmap afterwards we have to override a method called onDraw this method is going to be called shift+ctrl+o for the import this method is going to be called automatically from the view in fact I don't know if it is a view or a holder.A view is inside a holder. It is like a container this holder manages the view. It tells the view what to do. the holder in the some way runs a code in the view, inside the one we extend and this code must run this onDraw and here is were we can write in the application.This canvas object here is like the blackboard were we could write. Imagine we were at school and we had a blackboard and we could write with a chalk we write through a lot of methods of this canvas. Here we can see it has a lot the first thing I do is tell him to make the background black and afterwards I tell him to print the bitmap this one I had already created in the position 10, 10 we will talk about this parameter null later on when I get the bitmap I use this static method from the BitmapFactory and I pass him the resources, which is mainly a view method and this, if you remember from other tutorials, is an icon which comes with the application this icon here this one we are going to use this one to not complicate things. Later on we will use others let's go here run as Android application. You are going to have to wait but I had it already loaded Here it is. As you can see It opened and it printed. This is the bar I didn't wan't. We can take it out with the line this one in the activity if we add this here ctrl+shift+o to import ctrl+s to save and now we run again here it goes again Can you see it disappeared? ok, until now we have been printing with a view, let's try it with a SurfaceView what we have to do is to replace this view with a SurfaceView if we run this we are going to see something wrong that the image doesn't print, because SurfaceView is a lower level class so all that code we were talking about which makes the call to the onDraw we haven't got it now we are going to have to call the method ourselves to call the method we need to know when to call it and also we use the canvas object to call it to do this we should first get the holder and afterwards do a holder callback here we are going to put the holder. There is a holder method in SurfaceView which gives us the holder let's create a field because we are going to use it in the future and here we are going to include a call.. we are going to register a... a class which is going to be a listener of the events of the callback of the Holder. Let's ask for help... it gives us the possibility to create an anonymous class which implements the interface SurfaceHolder.callback here there is an error because we have to import the interface this one here it is. This is an inner class This is the same as creating a new class. The only difference is that everything is here altogether so that the object is a listener of the SurfaceHolder we need ehh that the anonymous class implements the methods of the interface callback ehh this methods are destroy which is going to tell us when the view is destroyed, when everything is finished when it is created and when it has a change what we are interested in now, is this one this one... this method is going to be run when the list is prepared to make prints in this method we are going to put this code which what it does is ask the Holder(which comes as a parameter) we also have it as a property but in this case it comes as a parameter It asks the holder through the method lockCanvas. This null is not necessary... it asks for the canvas. Why lockCanvas? this method makes a lock to the Canvas. It reserves it so that nobody else uses it when we have c, we can call the onDraw method with the Canvas after calling the onDraw we have to tell the Holder to unlock it this is important because the locks work against the performance of the application it is very important in games because we are talking of real time let's run this here it comes. Now is does print. We had to do it by hand ok, this is everything for this tutorial I 'll wait for you in the next one!
A2 初級 安卓遊戲2 Java編程教程 (Android Game 2 Java Programming tutorial) 66 7 howshin 發佈於 2021 年 01 月 14 日 更多分享 分享 收藏 回報 影片單字