Android graphics
- Android provides a huge set of 2D-drawing APIs that allow you to create graphics.
- Android has got visually appealing graphics and mind blowing animations.
- The Android framework provides a rich set of powerful APIS for applying animation to UI elements and graphics as well as drawing custom 2D and 3D graphics.
Following are the three animation systems used in Android applications:
1. Property Animation
2. View Animation
3. Drawable Animation
1. Property Animation
- Property animation is the preferred method of animation in Android.
- This animation is the robust framework which lets you animate any properties of any objects, view or non-view objects.
- The android.animation provides classes which handle property animation.
2. View Animation
- View Animation is also called Tween Animation.
- The android.view.animation provides classes which handle view animation.
- This animation can be used to animate the content of a view.
- It is limited to simple transformation such as moving, resizing and rotation, but not its background colour.
3. Drawable Animation
- Drawable animation is implemented using the AnimationDrawable class.
- This animation works by displaying a running sequence of ‘Drawable’ resources that is images, frame by frame inside a view object.
Canvas
- Android graphics provides low level graphics tools such as canvases, colour, filters, points and rectangles which handle drawing to the screen directly.
- The Android framework provides a set of 2D-DRAWING APIs which allows users to provide their own custom graphics onto a canvas or to modify existing views to customise their look and feel.
There are two ways to draw 2D graphics,
1. Draw your animation into a View object from your layout.
2. Draw your animation directly to a Canvas.
Some of the important methods of Canvas Class are as follows
i) drawText()
ii) drawRoundRect()
iii) drawCircle()
iv) drawRect()
v) drawBitmap()
vi) drawARGB()
- You can use these methods in the onDraw() method to create your own custom user interface.
- Drawing an animation with a View is the best option to draw simple graphics that do not need to change dynamically and are not a part of a performance-intensive game. It is used when a user wants to display a static graphic or predefined animation.
- Drawing an animation with a Canvas is a better option when your application needs to redraw itself regularly. For example video games should be drawing to the Canvas on its own.
File name: MyView.java
public class MyView extends View
{
public MyView(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int radius;
radius = 50;
Paint paint = newPaint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.parseColor(“#CD5C5C”));
canvas.drawCircle(150,200, radius, paint);
canvas.drawRoundRect(newRectF(20,20,100,100), 20, 20, paint);
canvas.rotate(-45);
canvas.drawText(“TutorialRide”, 40, 180, paint);
canvas.restore();
}
}
Output
