Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A logo view with an isosceles triangle. To use it, provide you distinct angle and the largest edge.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 longkai
*
* The software shall be used for good, not evil.
*/
package android.support.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.R;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
/**
* A logo view with an isosceles triangle.
*
* To use it, provide you distinct angle and the largest edge.
*
* @author longkai
*/
public class LogoView extends View {
private Drawable logo;
private int angle;
private int color;
private int edge;
private Paint mPaint;
private Path mPath;
public LogoView(Context context) {
this(context, null);
}
public LogoView(Context context, AttributeSet attrs) {
super(context, attrs);
bootstrap(context, attrs, 0, 0);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public LogoView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
bootstrap(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public LogoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
bootstrap(context, attrs, defStyleAttr, defStyleRes);
}
private void bootstrap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.LogoView, defStyleAttr, defStyleRes);
logo = array.getDrawable(R.styleable.LogoView__logo);
angle = array.getInt(R.styleable.LogoView_triangle_angle, 120);
color = array.getColor(R.styleable.LogoView_triangle_color, Color.WHITE);
// default to 10dip, may not satisfy you, provide your own
edge = array.getDimensionPixelSize(R.styleable.LogoView_triangle_edge,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics()));
array.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPath = new Path();
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
// first, draw the logo if there' s one and center it
if (logo != null) {
int width = logo.getIntrinsicWidth();
int height = logo.getIntrinsicHeight();
logo.setBounds(
(getWidth() - width) / 2,
(getHeight() - height) / 2,
(getWidth() + width) / 2,
(getHeight() + height) / 2
);
logo.draw(canvas);
}
double tan = Math.tan(Math.toRadians(angle * 1f / 2));
if (tan > 0f) {
int h = (int) (edge / 2f / tan);
int x = getWidth() / 2;
int y = getHeight() - getPaddingBottom();
// * (a)
// ***
// *****
// *******
// (b) (c)
// (x,y)
// don' t forget to reset path
mPath.reset();
mPath.setFillType(Path.FillType.EVEN_ODD);
// apply three points
mPath.moveTo(x, y - h); // a
mPath.lineTo(x - edge / 2, y); // b
mPath.lineTo(x + edge / 2, y); // c
// done
mPath.close();
mPaint.setColor(color);
// draw the triangle
canvas.drawPath(mPath, mPaint);
}
canvas.restore();
}
public int getAngle() {
return angle;
}
public void setAngle(int angle) {
this.angle = angle;
invalidate();
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
invalidate();
}
public int getEdge() {
return edge;
}
public void setEdge(int edge) {
this.edge = edge;
invalidate();
}
public Drawable getLogo() {
return logo;
}
public void setLogo(Drawable logo) {
this.logo = logo;
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment