// TurningArrowView.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2016-03-04 File created. // 2016-03-04 Last modification. /* The custom View for the TurningArrow app. */ package turning.arrow ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.View ; import android.content.Context ; import android.util.AttributeSet ; final class TurningArrowView extends View { int view_center_point_x = 0 ; int view_center_point_y = 0 ; int arrow_angle = 0 ; Paint filling_paint = new Paint() ; Paint outline_paint = new Paint() ; Path upwards_arrow = new Path() ; // The following constructor is needed when TurningArrowView object is // specified in an XML file, and is thus created automatically. public TurningArrowView( Context context, AttributeSet attributes ) { super( context, attributes ) ; filling_paint.setStyle( Paint.Style.FILL ) ; filling_paint.setColor( Color.MAGENTA ) ; outline_paint.setStyle( Paint.Style.STROKE ) ; outline_paint.setColor( Color.RED ) ; outline_paint.setStrokeWidth( 2 ) ; // The background color of this view is set in the XML file //setBackgroundColor( 0xFFFFFFE0 ) ; // Light Yellow // The arrow coordinates are selected so that Point (0, 0) // is in the middle of the arrow, and the arrow points upwards. upwards_arrow.moveTo( 0, -192 ) ; // to the tip of the arrow upwards_arrow.lineTo( 128, 0 ) ; upwards_arrow.lineTo( 64, 0 ) ; upwards_arrow.lineTo( 64, 192 ) ; upwards_arrow.lineTo( -64, 192 ) ; // make the bottom line upwards_arrow.lineTo( -64, 0 ) ; upwards_arrow.lineTo( -128, 0 ) ; upwards_arrow.close() ; } public void onSizeChanged( int current_width_of_this_view, int current_height_of_this_view, int old_width_of_this_view, int old_height_of_this_view ) { view_center_point_x = current_width_of_this_view / 2 ; view_center_point_y = current_height_of_this_view / 2 ; } public void set_arrow_angle( int new_arrow_angle ) { arrow_angle = new_arrow_angle ; invalidate() ; } public void set_outline_stroke_width( int new_stroke_width ) { outline_paint.setStrokeWidth( new_stroke_width ) ; invalidate() ; } protected void onDraw( Canvas canvas ) { canvas.translate( view_center_point_x, view_center_point_y ) ; canvas.rotate( arrow_angle ) ; canvas.drawPath( upwards_arrow, filling_paint ) ; // draw solid arrow canvas.drawPath( upwards_arrow, outline_paint ) ; // draw arrow outline } }