Skip to content

Instantly share code, notes, and snippets.

@gokselpirnal
Last active July 19, 2016 05:13
Show Gist options
  • Save gokselpirnal/fc5d687122a0653c7613cb9329d87430 to your computer and use it in GitHub Desktop.
Save gokselpirnal/fc5d687122a0653c7613cb9329d87430 to your computer and use it in GitHub Desktop.
React logo with Android Canvas
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
// Göksel Pırnal
// result screenshot: http://i.hizliresim.com/jn7YY9.jpg
public class ReactLogoView extends View {
private Paint backgroundPaint;
private Paint orbitPaint;
private Paint dotPaint;
private int circleX;
private int circleY;
private int backgroundColor;
private int orbitColor;
public ReactLogoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public ReactLogoView(Context context, AttributeSet attrs) {
super(context, attrs);
backgroundColor = Color.parseColor("#222222");
orbitColor = Color.parseColor("#00d8ff");
init(context);
}
public ReactLogoView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
backgroundPaint = new Paint();
backgroundPaint.setAntiAlias(true);
backgroundPaint.setColor(backgroundColor);
backgroundPaint.setStyle(Paint.Style.FILL);
orbitPaint = new Paint();
orbitPaint.setAntiAlias(true);
orbitPaint.setColor(orbitColor);
orbitPaint.setStyle(Paint.Style.STROKE);
orbitPaint.setStrokeWidth((float) (0.08 * getMeasuredWidth()));
dotPaint = new Paint();
dotPaint.setAntiAlias(true);
dotPaint.setColor(orbitColor);
dotPaint.setStyle(Paint.Style.FILL);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension((widthMeasureSpec), (widthMeasureSpec));
circleX = getMeasuredWidth() / 2;
circleY = getMeasuredHeight() / 2;
orbitPaint.setStrokeWidth((float) (0.08 * circleX));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(circleX, circleX, getMeasuredWidth(), backgroundPaint);
RectF rect1 = new RectF((float) (0.08 * circleX), (circleX / 5) * 3.3f, getMeasuredWidth() - (float) (0.08 * circleX), (circleX / 5) * 6.7f);
canvas.drawArc(rect1, 0, 360, true, orbitPaint);
canvas.rotate(60, circleX, circleX);
canvas.drawArc(rect1, 0, 360, true, orbitPaint);
canvas.rotate(60, circleX, circleX);
canvas.drawArc(rect1, 0, 360, true, orbitPaint);
canvas.drawCircle(circleX, circleX, circleX / 6, dotPaint);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment