Skip to content

Instantly share code, notes, and snippets.

@martyglaubitz
Last active August 29, 2015 14:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save martyglaubitz/37aa0268727835856061 to your computer and use it in GitHub Desktop.
Save martyglaubitz/37aa0268727835856061 to your computer and use it in GitHub Desktop.
A drawable which draws borders as background of a view
/* The MIT License (MIT)
Copyright (c) 2014, Marty Glaubitz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */
public class BorderDrawable extends Drawable {
private Paint _PaintLeft;
private Paint _PaintTop;
private Paint _PaintRight;
private Paint _PaintBottom;
public BorderDrawable(int color, int border) {
this(color, color, color, color, border, border, border, border);
}
public BorderDrawable(Integer colorLeft, Integer colorTop, Integer colorRight, Integer colorBottom, int borderWidthLeft, int borderWidthTop, int borderWidthRight, int borderWidthBottom) {
super();
if (colorLeft != null) {
_PaintLeft = new Paint();
_PaintLeft.setStrokeWidth(borderWidthLeft);
_PaintLeft.setStyle(Paint.Style.STROKE);
_PaintLeft.setColor(colorLeft);
}
if (colorTop != null) {
_PaintTop = new Paint();
_PaintTop.setStrokeWidth(borderWidthTop);
_PaintTop.setStyle(Paint.Style.STROKE);
_PaintTop.setColor(colorTop);
}
if (colorRight != null) {
_PaintRight = new Paint();
_PaintRight.setStrokeWidth(borderWidthRight);
_PaintRight.setStyle(Paint.Style.STROKE);
_PaintRight.setColor(colorRight);
}
if (colorBottom != null) {
_PaintBottom = new Paint();
_PaintBottom.setStrokeWidth(borderWidthBottom);
_PaintBottom.setStyle(Paint.Style.STROKE);
_PaintBottom.setColor(colorBottom);
}
}
@Override
public void draw(Canvas canvas) {
if (_PaintLeft != null) {
final float X = _PaintLeft.getStrokeWidth() / 2;
canvas.drawLine(X, 0, X, canvas.getHeight(), _PaintLeft);
}
if (_PaintTop != null) {
final float Y = _PaintTop.getStrokeWidth() / 2;
canvas.drawLine(0, Y, canvas.getWidth(), Y, _PaintTop);
}
if (_PaintRight != null) {
final float X = canvas.getWidth() - (_PaintRight.getStrokeWidth() / 2);
canvas.drawLine(X, 0, X, canvas.getHeight(), _PaintRight);
}
if (_PaintBottom != null) {
final float Y = canvas.getHeight() - _PaintBottom.getStrokeWidth();
canvas.drawLine(0, Y, canvas.getWidth(), Y, _PaintBottom);
}
}
@Override
public void setAlpha(int alpha) {
if (_PaintLeft != null) {
_PaintLeft.setAlpha(alpha);
}
if (_PaintTop != null) {
_PaintTop.setAlpha(alpha);
}
if (_PaintRight != null) {
_PaintRight.setAlpha(alpha);
}
if (_PaintBottom != null) {
_PaintBottom.setAlpha(alpha);
}
}
@Override
public void setColorFilter(ColorFilter cf) {
if (_PaintLeft != null) {
_PaintLeft.setColorFilter(cf);
}
if (_PaintTop != null) {
_PaintTop.setColorFilter(cf);
}
if (_PaintRight != null) {
_PaintRight.setColorFilter(cf);
}
if (_PaintBottom != null) {
_PaintBottom.setColorFilter(cf);
}
}
@Override
public int getOpacity() {
return 0;
}
}
@martyglaubitz
Copy link
Author

usage example, draws black borders ontop and at the bottom which are 3 pixels wide:

viewWhichNeedsBorders.setBackground(new BorderDrawable(null, Color.Black, null, Color.Black, 0, 3, 0, 3))

@martyglaubitz
Copy link
Author

change: now uses lines to draw borders
fix: bottom line was half to thin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment