Download presentation
Presentation is loading. Please wait.
Published byEllen Richardson Modified over 6 years ago
1
Mobile Computing With Android ACST 4550 Intro to Android, Part 2
2
Android Resources
3
Android Resources
4
Android Resources
5
Android Resources
6
The Android Emulator You can develop Android apps in Eclipse and then run them on a Android phone, Android tablet or the Android Emulator. To run your app on a phone or tablet you will have to install the proper drivers for your device, but if you use the emulator, you don’t need a physical Android device. And all of the libraries and plug-ins necessary to run the emulator should have been installed when you installed the Android SDK (Software Developer Kit) and ADT (Android Developer Tools) in the first week of class.
7
The Android Emulator You Emulator allows you to view a virtual Android phone, and to run your Android projects on that virtual phone:
8
The Android Emulator To start up the emulator, go to the “Windows” menu and then select “Android Virtual Device Manager”. From there you create one or more AVD configurations. In each configuration, you specify an Android platform to run in the emulator and the set of hardware options and emulator skin you want to use. Then, when you launch the emulator, you specify the AVD configuration that you want to load and click on “Start” to launch the emulator. During development and testing of your application, run your application in an Android emulator that has been launched. You only have to set up each emulator you create one time. Its settings are remembered so you can reuse it any time in the future.
9
Android Projects Each Android App will be in its own Project folder with its own package name. The Project folder will include many resources for your program that we will discuss in the weeks to come. Because there are so many folders and files needed for each Android project, at times you will need to ZIP the whole Project folder and submit the ZIP file. However, at other times you will just need to submit one or two files from a project that you are modifying.
10
Android Application Lifecycle
While no application running on a modern computer is really ‘simple’, GUI applications expose the programmer to more of the details of how a program runs. Applications have a sequence of stages that they go through. We will call this the application lifecycle. An application will visit some stages only once, others will be repeated. The details of the stages will depend on the particular platform, but many concepts will apply widely.
11
Android Application Lifecycle
In the Android frameworks, the application is started by the system by creating an object of the Activity class (or a child of the Activity class) and calling that object’s onCreate() method. The onCreate() method must handle setting up the view (or views) and enabling the controller code to react to events.
12
GUI Applications GUI applications have many components. The concept of a View turns out to be very effective concept to represent the interface used to interact with the components as objects. Many frameworks for programming GUI applications are based on a collection of classes that interact with the View. The many components that make up the application must be managed, and the concept of a Controller is the metaphor for that portion of the program. And each of the components are made up of classes that are logically linked together in one or more Models.
13
The MVC Framework The Model—View—Controller architecture is widely used to help deal with the complexity of GUI applications. Each of the three terms refers to a subset of the classes and objects that make up an application. The groupings are functional. Typically, programmers will work with classes in these different groups in different ways. The most important difference is whether the classes are provided in a library or are they written by the programmer for the application. The MVC framework can be easily aligned with the Android Application Lifecyle.
14
The MVC Framework Model View Controller
The model contains classes that represent the data used by the application. This will also include methods for manipulating and computing with that data. View The view is what the user sees. This is the visible interface. Controller The controller contains code that is executed in response to user actions and some other external events. This is what is handled in the Android Activity class.
15
The MVC Framework Model classes are the kinds of classes that you have been learning to develop before now. They store data and methods that process that data. The data is usually encapsulated, and you usually have constructors to initialize the data, and accessors, mutators, and other methods to provide the user with ways to interact with the data. View classes are often in a library provided with the programming system or added externally. These libraries give you access to the physical device that the user sees. Android Views use different graphics libraries than the Views from Standard Java programs.
16
The MVC Framework Controller classes are created by the application programmer. In the Android Framework, the Controller is usually the same as the Activity class. Unlike the Model classes, the Controller class will be specific to the application. The Controller may also make heavy use of classes included with the appropriate library.
17
Frameworks We use the word framework to refer to a collection of classes that are used together in a certain pattern. In that sense, we will be working with the Android application framework. There are numerous classes in the Android framework and they must be used together in certain patterns. The View class is one of the classes in the Application framework that performs according to a particular event-driven pattern. For example, some common events in the View class that many applications use and that we will work with are: onDraw onTouchEvent onSizeChanged
18
Android Editors Editors are the tool used to modify and add elements to an application. Different editors are provided for different types of files. Java Editor This editor will be familiar to most developers who have used the Java before. As the name implies, it is used to edit Java source code. Layout GUI and XML Editor This is the editor used to edit XML views, such as activity_main.xml found in the layout folder. These views are created from descriptions in XML files. One approach to creating and changing these views is to edit the XML directly.
19
Editors XML Editor This is the editor used to edit XML code:
Resources Editor This editor is used to edit files in the ‘values’ subfolder of the ‘res’ folder, such as strings.xml or styles.xml.
20
Android Text and Graphics
Because of the nature of Android devices, there really is no Text-based mode for user interactions. It is always in graphics mode, so if you attempt to print something out with the System.out.println() method, you will not see it print to the console window. Instead it will print out in the LogCat window, which is a window the end-user will not be able to see. However, you can put debugging statements and other such statements into the LogCat intentionally, and those statements will show up in the LogCat as green info messages for the deveolper(s).
21
Standard Java Vs. Android Graphics
Many differences exist between how graphics are handled in Standard Java and how they are handled in the Android OS. For instance, the Android libraries do not have the same type of Color class as the one available in the Standard Java Awt library. The Android Color library (from the android.graphics package) consist of a set of Static methods for producing an integer value that represents a 4 byte Alpha-RGB value or ARGB value. The first byte is the alpha value (0-255), the second byte is the red value (0-255), the third byte is the green value (0-255), and the fourth byte is the blue value (0- 255).
22
The Android Canvas Class and The onDraw() Method
Working with Android graphics involves working with two main classes, the Canvas and the Paint class: The Canvas class is analogous to the Graphics class from the Standard Java packages that aren’t used on an Android device. Like the Graphics class, an object of the Canvas class is sent to the method that updates the view. That’s the onDraw() method in the Android OS, while it’s called paint() or paintComponent() in Standard Java. Like the Graphics class, the Canvas provides you with the methods and a coordinate system for drawing things in your view. A partial list of those drawing methods is provided at a point further below.
23
The Android Paint Class
The Paint class defines a paint object which specifies drawing attributes to be used by the Canvas drawing methods. When you use certain Canvas drawing methods, you will pass them a paint object that defines things like the following (among others): Drawing Color Drawing style (solid 'fill' or outline 'stroke') Stroke style attributes Text drawing font attributes Calls to methods like paint.setColor(), paint.setStyle(), paint.setTypeFace() and paint.setTextSize() will often proceed a call to a Canvas drawing method.
24
RGB Color Combinations
You can use the Color constants provided in the Color class or you can you the Color.rgb(red,green,blue) method or the Color.argb(alpha,red,green,blue) method to create a custom color value. The Alpha value gives the amount of transparency in the color. A value of 255 means no transparency, and a value of 0 means completely transparent. The other RGB values combine to create colors in the normal way, meaning that (255,255,0) will create yellow. See HappyFaceView.java
25
The Color Constants The predefined colors include the following:
Object Color.BLACK Color.BLUE Color.CYAN Color.DKGRAY Color.GRAY Color.GREEN Color.LTGRAY Color.MAGENTA Color.RED Color.TRANSPARENT Color.WHITE Color.YELLOW ARGB Value 255, 0, 0, 0 255, 0, 0, 255 255, 0, 255, 255 255, 64, 64, 64 255, 128, 128, 128 255, 0, 255, 0 255, 192, 192, 192 255, 255, 0, 255 255, 255, 0, 0 0, 255, 255, 255 255, 255, 255, 255 255, 255, 255, 0
26
Drawing Shapes with the Canvas class
For some of the drawing methods of the Canvas class, the first arguments specify the top left coordinates and the bottom right coordinates (leftX, topY, rightX, bottomY). For others, you pass a rectangle object with those values to the method. There are two rectangle object classes: Rect which takes integer arguments and Rectf which takes floating point values. The last argument sent to a drawing method specifies a Paint object that determines the drawing color, whether the object is filled (Style.FILL) or unfilled (Style.STROKE) and other optional drawing details. The paint object can be set like so: paint.setColor(Color.RED); paint.setStyle(Style.STROKE);
27
Drawing a Line canvas.drawLine (10,20,150,45,paint); or
X Y 10 150 20 45 canvas.drawLine (10,20,150,45,paint); canvas.drawLine (150,45,10,20,paint); or
28
Drawing a Rectangle canvas.drawRect (50,20,150,60,paint); 50 X 20 40
Y 50 20 40 (150, 60) 100 canvas.drawRect (50,20,150,60,paint);
29
Drawing an Oval bounding rectangle
(Note: You must create a RectF object and place the oval inside) X Y 175 20 80 (225, 100) bounding rectangle 50 Rectf ovalRect = new Rectf(175,20,225,100); canvas.drawOval (ovalRect,paint);
30
Drawing an Arc The drawArc method draws an arc.
drawArc(oval, 180, 180, false, paint); The drawArc method takes five arguments. The first argument is the bounding RectF object, like with the drawOval method. The next two arguments indicate where the arc starts, and the number of degrees through which is sweeps clockwise. The next argument determines if the center point is shown as a connected point in a unfilled (stroke) drawing. The next argument is the paint to use.
31
Specifying an Arc (Note: You must create a RectF object and place the oval inside) Rectf oval = new Rectf(); oval.set(x,y,x+Width,y+Height); canvas.drawArc(oval,0,90,false,paint); Rectf oval = new Rectf(); oval.set(x,y,x+Width,y+Height); canvas.drawArc(oval,0,-90,false,paint); The fourth argument is a flag to determine if the center is included in the arc (making it a wedge). It is false in the above examples.
32
Specifying an Arc (Note: You must create a RectF object and place the oval inside) Rectf oval = new Rectf(); oval.set(x,y,x+Width,y+Height); canvas.drawArc(oval,0,90,true,paint); Rectf oval = new Rectf(); oval.set(x,y,x+Width,y+Height); canvas.drawArc(oval,0,-90,true,paint); The fourth argument is a flag to determine if the center is included in the arc (making it a wedge). It is true in the above examples.
33
Basic Drawing Methods In the Canvas class the following methods are available for drawing shapes: canvas.drawLine(x1, y1, x2, y2, paint); canvas.drawRect(x, y, x+width, y+height, paint); canvas.drawOval(oval, paint); // oval is a RectF you set canvas.drawArc(oval, startAngle, arcAngle, paint); // oval is a RectF you set canvas.drawText(String, x, y, paint); canvas.drawColor(Color); // fills entire canvas with color, like a backgrd color And the following methods are available for manipulating the pen: paint.setColor(Color); // sets pen color paint.setStyle(Style); // Style.FILL=filled or Style.STROKE=unfilled paint.setStrokeWidth(Width); // sets stroke width, Width is an int paint.setTextSize(Size); // sets text size, Size is an int paint.setTypeface(TypeFace); // sets type face See SmilingFaceView.java and SnowmanView.java 3
34
Polygons In the Canvas class, to draw polygons the Path class is used instead of Standard Java Polygon class. For instance, a three sided shape that has vertices at the three points (x1,y1), (x2,y2) and (x3,y3) would be drawn by creating a Path object like so: Path pathShape = new Path(); pathShape.moveTo(x1,y1); pathShape.lineTo(x2,y2); pathShape.lineTo(x3,y3); pathShape.close (); canvas.drawPath(pathShape, paint); The moveTo() method starts the path, then points are added with the lineTo() command, and when you call close() a line segment to the original point is added. You can also just lineTo() the first point to end the path, or use that and close() to end the path.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.