Android Custom View 102
In this post we talk about the drawing order in custom view.
After super.onDraw() What we have done in the previous episodes is to override the onDraw() method of the View class.
A lot of times we do not need to create our own custom view from scratch. We can simply extends an existing view, e.g. ImageView. If we want to draw something on top of the image, we can write that after the super.
In this post, we will be focusing on what canvas can do.
To demonstrate the effects, I simply created custom view that draws an image. The original verion is like this:
Clip Clipping is easy to understand, it’s like cut the view in a certain way so that only part of it is shown.
There are 2 methods: clipRect() and clipPath().
clipRect() This method will clip the view using a rectangle.
In this post, we will be focusing on the Paint.
Color The color in Paint has 3 parts: basic color, color filter and xfermode.
Basic color There are 2 ways to set color in Paint: use setColor() and use Shader.
Set color directly Two methods can be used:
paint.setColor(Color.parseColor("#B90E83"); paint.setARGB(100, 255, 0, 0); There is no difference, pick whichever you like.
Set color using Shader There are different types of Shader, let’s look them one by one.
If 101 is like a crash course, then this 102 will provide much more detail.
In this 1st part, we focus on only one thing: how to draw things.
Draw Color Draw the entire view with one color.
public class DrawColorView extends View { … @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.YELLOW); } } Draw Circle Draw a circle on the view.
Pay attention that all drawXXX() functions use pixel as unit.