Created
December 19, 2015 21:15
-
-
Save Fshamri/405a54bf51ebe641c579 to your computer and use it in GitHub Desktop.
Triangle Shape View with drawable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<declare-styleable name="TriangleShapeView"> | |
<attr name="img" format="reference" /> | |
<attr name="imgPadding" format="dimension" /> | |
<attr name="triangleBackground" format="color" /> | |
</declare-styleable> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TriangleShapeView extends View { | |
Paint paint; | |
Path trianglePath; | |
Drawable drawable; | |
int imgPadding; | |
int backgroundColor; | |
public TriangleShapeView(Context context) { | |
super(context); | |
} | |
public TriangleShapeView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public TriangleShapeView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TriangleShapeView); | |
drawable = a.getDrawable(R.styleable.TriangleShapeView_img); | |
imgPadding = a.getDimensionPixelSize(R.styleable.TriangleShapeView_imgPadding, 5); | |
backgroundColor = a.getColor(R.styleable.TriangleShapeView_triangleBackground, Color.TRANSPARENT); | |
a.recycle(); | |
trianglePath = new Path(); | |
paint = new Paint(); | |
paint.setColor(backgroundColor); | |
} | |
public void setBackgroundColor(int backgroundColor) { | |
this.backgroundColor = backgroundColor; | |
invalidate(); | |
} | |
public void setDrawable(Drawable drawable) { | |
this.drawable = drawable; | |
invalidate(); | |
} | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
int size = Math.max(getWidth(), getHeight()) / 2 + imgPadding; | |
trianglePath.lineTo(size, 0); | |
trianglePath.lineTo(0, size); | |
trianglePath.lineTo(0, 0); | |
trianglePath.close(); | |
canvas.drawPath(trianglePath, paint); | |
drawable.draw(canvas); | |
paint.setColor(backgroundColor); | |
} | |
@Override | |
protected boolean verifyDrawable(Drawable who) { | |
return super.verifyDrawable(who) || who == drawable; | |
} | |
@Override | |
public void jumpDrawablesToCurrentState() { | |
super.jumpDrawablesToCurrentState(); | |
if (drawable != null) drawable.jumpToCurrentState(); | |
} | |
@Override | |
protected void drawableStateChanged() { | |
super.drawableStateChanged(); | |
if (drawable != null && drawable.isStateful()) { | |
drawable.setState(getDrawableState()); | |
} | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
if (drawable != null) { | |
drawable.setBounds(imgPadding, imgPadding, drawable.getIntrinsicWidth() + imgPadding, drawable.getIntrinsicHeight() + imgPadding); | |
invalidate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment