Skip to content

Instantly share code, notes, and snippets.

@sharish
Created November 2, 2015 02:56
Show Gist options
  • Save sharish/adcb919baa09d590be5d to your computer and use it in GitHub Desktop.
Save sharish/adcb919baa09d590be5d to your computer and use it in GitHub Desktop.
package com.cooltechworks.semicircletest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
/**
* Created by sharish on 11/1/15.
*/
public class SemiCircleView extends View implements View.OnTouchListener{
private Paint firstThird, secondThird, thirdThird, bookButtonPaint;
private RectF firstThirdRect, secondThirdRect, thirdThirdRect;
private RectF bookButtonRect;
private Paint textPaint;
public SemiCircleView(Context context) {
super(context);
init();
}
public SemiCircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SemiCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void init() {
firstThird = new Paint();
firstThird.setColor(Color.RED);
firstThird.setStrokeWidth(30);
firstThird.setStyle(Paint.Style.FILL);
secondThird = new Paint();
secondThird.setColor(Color.GREEN);
secondThird.setStrokeWidth(30);
secondThird.setStyle(Paint.Style.FILL);
thirdThird = new Paint();
thirdThird.setColor(Color.BLUE);
thirdThird.setStrokeWidth(30);
thirdThird.setStyle(Paint.Style.FILL);
bookButtonPaint = new Paint();
bookButtonPaint.setColor(Color.YELLOW);
firstThirdRect = new RectF();
secondThirdRect = new RectF();
thirdThirdRect = new RectF();
bookButtonRect= new RectF();
textPaint = new Paint();
setOnTouchListener(this);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
firstThirdRect.left = 0;
firstThirdRect.right = getWidth();
firstThirdRect.bottom = getHeight()* 2;
firstThirdRect.top = 0;
secondThirdRect.left = 0;
secondThirdRect.right = getWidth();
secondThirdRect.bottom = getHeight()* 2;
secondThirdRect.top = 0;
thirdThirdRect.left = 0;
thirdThirdRect.right = getWidth();
thirdThirdRect.bottom = getHeight() * 2;
thirdThirdRect.top = 0;
bookButtonRect.left = getWidth() * 0.25f;
bookButtonRect.right = getWidth() * 0.75f;
bookButtonRect.bottom = getHeight() * 1.5f;
bookButtonRect.top = getHeight() * 0.5f;
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(30);
canvas.drawArc(firstThirdRect, 180f, 60f, true, firstThird);
canvas.drawArc(secondThirdRect, 240f, 60f, true, secondThird);
canvas.drawArc(thirdThirdRect, 300f, 60f, true, thirdThird);
canvas.drawArc(bookButtonRect, 180f, 180f, true, bookButtonPaint);
canvas.drawText("Book", getWidth() * 0.45f, getHeight() * 0.85f, textPaint);
canvas.drawText("Sedan", getWidth() * 0.42f, getHeight() * 0.30f, textPaint);
canvas.drawText("Mini", getWidth() * 0.05f, getHeight() * 0.85f, textPaint);
canvas.drawText("SUV", getWidth() * 0.8f, getHeight() * 0.85f, textPaint);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX();
float y = event.getY();
if(x > getWidth() * .25f && x < getWidth() * .75f &&y > getHeight() * 0.5f) {
Toast.makeText(getContext(), "Book button tapped", Toast.LENGTH_SHORT).show();
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment