ArrowButton |
![]() |
Кнопка со стрелкой.
/** * This class draws simple arrow buttons. */ final class ArrowButton extends Button { static final int NORTH = 0; static final int UP = 0; static final int WEST = 1; static final int RIGHT = 1; static final int SOUTH = 2; static final int DOWN = 2; static final int EAST = 3; static final int LEFT = 3;
/** The default size of the Arrow buttons. */ private static final float DEFAULT_SIZE = 9f;
/** The Polygon that points up. */ private static final float[] UP_POLY_X = new float[] { 0, 4, 8 }; private static final float[] UP_POLY_Y = new float[] { 6, 2, 6 };
/** The Polygon that points down. */ private static final float[] DOWN_POLY_X = new float[] { 0, 4, 8 }; private static final float[] DOWN_POLY_Y = new float[] { 2, 6, 2 };
/** The Polygon that points left. */ private static final float[] LEFT_POLY_X = new float[] { 6, 2, 6 }; private static final float[] LEFT_POLY_Y = new float[] { 0, 4, 8 };
/** The Polygon that points right. */ private static final float[] RIGHT_POLY_X = new float[] { 2, 6, 2 }; private static final float[] RIGHT_POLY_Y = new float[] { 0, 4, 8 };
/** The direction to point in. */ private int mDirection;
ArrowButton(Context context, int direction) { super( context ); setDirection( direction ); }
/** * This method returns the direction of the arrow. * * @return The direction of the arrow. */ final int getDirection() { return mDirection; }
/** * This method changes the direction of the arrow. * * @param dir The new direction of the arrow. */ final void setDirection(final int dir) { mDirection = dir; }
@Override protected final void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure( widthMeasureSpec, heightMeasureSpec ); final int h = getMeasuredHeight(); setMeasuredDimension( (h << 1) / 3, h ); }
@Override protected final void onDraw(Canvas canvas) { super.onDraw( canvas ); float[] xpts = DOWN_POLY_X; float[] ypts = DOWN_POLY_Y; switch( mDirection ) { case UP: xpts = UP_POLY_X; ypts = UP_POLY_Y; break; case RIGHT: xpts = RIGHT_POLY_X; ypts = RIGHT_POLY_Y; break; case LEFT: xpts = LEFT_POLY_X; ypts = LEFT_POLY_Y; break; } final int w = getWidth(); final int h = getHeight(); final float size = (float)(Math.min( w, h ) >> 1); final float[] xp = new float[xpts.length]; final float[] yp = new float[ypts.length]; final float scale = size / DEFAULT_SIZE; for( int i = 0, len = xp.length; i < len; i++ ) { xp[i] = xpts[i] * scale; yp[i] = ypts[i] * scale; } xpts = xp; ypts = yp; final float x = ((float)w - size) * 0.5f; final float y = ((float)h - size) * 0.5f; canvas.translate( x, y ); // final Paint paint = new Paint(); paint.setColor( getTextColors().getColorForState( getDrawableState(), Color.TRANSPARENT ) ); paint.setStyle( Style.FILL ); paint.setAntiAlias( true ); // final Path path = new Path(); path.moveTo( xpts[0], ypts[0] ); for( int i = 0, len = xpts.length; i < len; i++ ) { path.lineTo( xpts[i], ypts[i] ); } path.close(); // canvas.drawPath( path, paint ); canvas.translate( -x, -y ); } }
|