Back to project page SpunkyCharts.
The source code is released under:
GNU General Public License
If you think the Android project SpunkyCharts listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.jogden.spunkycharts.misc; /* // w w w . j a v a2s .com Copyright (C) 2014 Jonathon Ogden < jeog.dev@gmail.com > This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses. */ import com.jogden.spunkycharts.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.TextView; /** Used to create simple borders, in or out of the parents padding */ public class BorderOverlay extends TextView { @SuppressWarnings("serial") public static class BorderOverlayException extends RuntimeException { public BorderOverlayException(String msg){ super(msg); } } private int _width = 0; private int _height = 0; private int _wMode = MeasureSpec.UNSPECIFIED; private int _hMode = MeasureSpec.UNSPECIFIED; private int _borderSz = 0; private int _borderColorId = 0; private boolean _inPaddedParent = false; private float _padAdj = 0; private Paint brush = new Paint(Paint.ANTI_ALIAS_FLAG); public BorderOverlay( Context context, int borderSize, int borderColorId, boolean inPaddedParent ){ super(context); this._borderSz = borderSize; this._borderColorId = borderColorId; this._inPaddedParent = inPaddedParent; this._setup(); } public BorderOverlay(Context context, AttributeSet attrs ) { super(context, attrs); _extractAttr(context,attrs); this._setup(); } public BorderOverlay( Context context, AttributeSet attrs, int steez ){ super(context, attrs, steez); _extractAttr(context,attrs); this._setup(); } public void setBorderSize(int borderSize) { _borderSz = borderSize; brush.setStrokeWidth(borderSize); try{ this.invalidate(); }catch(Exception e){ this.postInvalidate(); } } public void setBorderColor(int borderColorId) { _borderColorId = borderColorId; brush.setColor(borderColorId); try{ this.invalidate(); }catch(Exception e){ this.postInvalidate(); } } public int getBorderSize() { return _borderSz; } public int getBorderColor() { return _borderColorId; } @Override protected void onMeasure(int wSpec, int hSpec ) { super.onMeasure(wSpec, hSpec); _width = MeasureSpec.getSize(wSpec); _height = MeasureSpec.getSize(hSpec); _wMode = MeasureSpec.getMode(wSpec); _hMode = MeasureSpec.getMode(hSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(_width == 0 || _height == 0 || _wMode != MeasureSpec.EXACTLY || _hMode != MeasureSpec.EXACTLY ){ _width = getMeasuredWidth(); _height = getMeasuredHeight(); if(_width == 0 || _height == 0 ) throw new BorderOverlayException( "invalid dimensions" ); } if(_inPaddedParent) try{ /* if we need our parent to let us draw in padded space */ ViewGroup parentLayout = (ViewGroup)this.getParent(); parentLayout.setClipToPadding(false); }catch(ClassCastException e){ throw new BorderOverlayException( "BorderOverlay's parent must be a ViewGroup " + "when inPaddedParent is true" ); } if (_borderSz == 0) { /* we can't set strokeWidth to 0, that draws 'hair-line' */ this.setBackgroundColor(Color.TRANSPARENT); return; } /*adjust by half to move rect 'inside' its dimens; don't take a change on precision, just tighten the edges half a pix*/ canvas.drawRect( (float)(0-_borderSz/_padAdj), (float)(0-_borderSz/_padAdj), (float)(_width+_borderSz/_padAdj), (float)(_height + _borderSz/_padAdj), brush ); } private void _extractAttr(Context c, AttributeSet attrs) { TypedArray ta = c.obtainStyledAttributes( attrs,R.styleable.CustomSpunky ); this._borderSz = (int)ta.getDimension( R.styleable.CustomSpunky_border_size, 1 ); this._borderColorId = ta.getColor( R.styleable.CustomSpunky_border_color, Color.BLACK ); this._inPaddedParent = ta.getBoolean( R.styleable.CustomSpunky_padded_parent, false ); ta.recycle(); } private void _setup() { this.setBackgroundColor(Color.TRANSPARENT); this.brush.setStyle(Style.STROKE); this.brush.setStrokeWidth(_borderSz); this.brush.setColor(_borderColorId); this._padAdj = _inPaddedParent ? 2f : -2f; } }