Drawing and Working With Animation: By: Mitul Patel
Drawing and Working With Animation: By: Mitul Patel
Figure - activity_main.xml
String sOrc =
strings.xml getResources().getString(R.string.race_orc);
String Array
The best solutions is to define your own colors set in the colors.xml file
Android Units of Measurements
NOTE: Always use sp for font sizes and dip for everything else.
Working with Canvases and Paints
• The android.graphics.Canvas can be used to draw graphics in
android. It provides methods to draw oval, rectangle, picture,
text, line etc.
• The android.graphics.Paint class is used with canvas to draw
objects. It holds the information of color and style.
• The drawing of canvas happens in Bitmap, where we draw
the outline and then the Paint API helps to fill color and
whatever style we need.
Working with Canvases and Paints
• For 2D graphics we usually opt for any of the two following options:
1. Graphics or animation object is drawn into View object from layout.
2. We can draw graphics directly onto the canvas.
• Android Canvas class encapsulates the bitmaps used as surface. It
exposes the draw methods which can be used for designing. Let us first
clear the following terms:
• Bitmap: The surface being drawn on.
• Paint: It lets us specify how to draw the primitives on bitmap. It is also
referred to as “Brush”.
• Canvas: It supplies the draw methods used to draw primitives on
underlying bitmap.
Working with Canvases and Paints
• Each drawing object specifies a paint object to render:
1. drawArc: This draws an arc between the two angles bounded by an area of rectangle.
2. drawBitmap: It draws an bitmap on canvas.
3. drawRGB/drawARGB/drawColor: This fills the canvas with a single color.
4. drawBitmapMesh: It draws a bitmap using a mesh. It manipulates the appearance of
target by moving points on it.
5. drawCircle: This draws a circle on a specified radius centered on a given point.
6. drawLine(s):it draws a line (or series of lines) between points.
7. drawOval: it draws an oval which is bounded by the area of rectangle.
8. drawPaint: It fills the entire canvas with a specific paint.
9. drawPath: It draws a path as per specification.
10.drawPicture: It draws a picture specified on a rectangular area.
11.drawPosText: it draws a text string specifying the offset of each character.
12.drawRect: It draws a rectangle.
13.drawRoundRect: it draws a rectangle with round edges.
14.drawText: It draws a text string on canvas.
Paint class
• The Paint class consists of a paint brush and a
palette. It lets us choose how to render the primitives
drawn into canvas by draw methods.
• We can control the color, style, font, special effects
etc can be modified by modifying the paint object.
• For instance, setColor method can be used to select
the color of Paint. Paint class supports transparency
so it can be used to control variety of shades or
effects, etc.
Example
CanvasView.java
Example
Activity class
File: MainActivity.java
Working with Animation
• Animation in android apps is the process of creating
motion and shape change.
• Animations in android apps can be performed through
XML or android code
Basic ways of animation
1. Fade In Animation
2. Fade Out Animation
3. Cross Fading Animation
4. Blink Animation
5. Zoom In Animation
6. Zoom Out Animation
7. Rotate Animation
8. Move Animation
9. Slide Up Animation
10.Slide Down Animation
11.Bounce Animation
12.Sequential Animation
13.Together Animation
Android Animation Example XML
Here alpha references the opacity of an object. An object with lower alpha values is more
transparent, while an object with higher alpha values is less transparent, more opaque.
Fade in animation is nothing but increasing alpha value from 0 to 1.
Fade Out Animation
Fade out android animation is exactly opposite to fade in, where we need to decrease the
alpha value from 1 to 0.
Cross Fading Animation
Blink Animation
Here fade in and fade out are performed infinitely in reverse mode each time.
Zoom In Animation
Zoom Out Animation
Rotate Animation
Move Animation
Slide Up Animation
Slide Down Animation
Bounce Animation
Sequential Animation
Together Animation
Applying fade_in in java file
When clicked on FadeIn Button(btnFadeIn), fade_in animation starts on TextView(txtFadeIn)
Output
THANK YOU