Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 31  Custom Views and Touch Events


Rendering Inside onDraw(Canvas)


When your application is launched, all of its views are invalid. This means that they have not drawn
anything to the screen. To fix this situation, Android calls the top-level View’s draw() method. This
causes that view to draw itself, which causes its children to draw themselves. Those children’s children
then draw themselves, and so on down the hierarchy. When all the views in the hierarchy have drawn
themselves, the top-level View is no longer invalid.


To hook into this drawing, you override the following View method:


protected void onDraw(Canvas canvas)


The call to invalidate() that you make in response to ACTION_MOVE in onTouchEvent(MotionEvent)
makes the BoxDrawingView invalid again. This causes it to redraw itself and will cause
onDraw(Canvas) to be called again.


Now let’s consider the Canvas parameter. Canvas and Paint are the two main drawing classes in
Android:



  • The Canvas class has all the drawing operations you perform. The methods you call on Canvas
    determine where and what you draw – a line, a circle, a word, or a rectangle.

  • The Paint class determines how these operations are done. The methods you call on Paint
    specify characteristics – whether shapes are filled, which font text is drawn in, and what color
    lines are.


In BoxDrawingView.java, create two Paint objects in BoxDrawingView’s XML constructor.


Listing 31.8  Creating your paint (BoxDrawingView.java)


public class BoxDrawingView extends View {
private static final String TAG = "BoxDrawingView";


private Box mCurrentBox;
private List mBoxen = new ArrayList<>();
private Paint mBoxPaint;
private Paint mBackgroundPaint;
...
// Used when inflating the view from XML
public BoxDrawingView(Context context, AttributeSet attrs) {
super(context, attrs);


// Paint the boxes a nice semitransparent red (ARGB)
mBoxPaint = new Paint();
mBoxPaint.setColor(0x22ff0000);


// Paint the background off-white
mBackgroundPaint = new Paint();
mBackgroundPaint.setColor(0xfff8efe0);
}
}


Armed with paint, you can now draw your boxes to the screen.

Free download pdf