|
@@ -0,0 +1,107 @@
|
|
|
|
|
+package hr.tacitus.componentpack;
|
|
|
|
|
+
|
|
|
|
|
+import android.content.Context;
|
|
|
|
|
+import android.content.pm.PackageManager;
|
|
|
|
|
+import android.content.res.TypedArray;
|
|
|
|
|
+import android.graphics.Canvas;
|
|
|
|
|
+import android.graphics.Color;
|
|
|
|
|
+import android.graphics.LinearGradient;
|
|
|
|
|
+import android.graphics.Paint;
|
|
|
|
|
+import android.graphics.RectF;
|
|
|
|
|
+import android.graphics.Shader;
|
|
|
|
|
+import android.util.AttributeSet;
|
|
|
|
|
+import android.view.View;
|
|
|
|
|
+import android.view.ViewTreeObserver;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * Created by Bero on 6.7.2017..
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+public class ArcView extends View {
|
|
|
|
|
+
|
|
|
|
|
+ private int beginColor = Color.GREEN;
|
|
|
|
|
+ private int endColor = Color.RED;
|
|
|
|
|
+ private int backgroundColor = Color.BLACK;
|
|
|
|
|
+ private float arcWidthInPixels = 32;
|
|
|
|
|
+
|
|
|
|
|
+ private Paint arcPaint;
|
|
|
|
|
+ private Paint canvasPaint;
|
|
|
|
|
+ private RectF arcRect, subArcRect;
|
|
|
|
|
+
|
|
|
|
|
+ public ArcView(Context context) {
|
|
|
|
|
+ super(context);
|
|
|
|
|
+ init(context, null);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public ArcView(Context context, AttributeSet attrs) {
|
|
|
|
|
+ super(context, attrs);
|
|
|
|
|
+ init(context, attrs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public ArcView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
+ super(context, attrs, defStyleAttr);
|
|
|
|
|
+ init(context, attrs);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ private void readAttributes(AttributeSet attributeSet) {
|
|
|
|
|
+ TypedArray a = getContext().obtainStyledAttributes(attributeSet, R.styleable.ArcView);
|
|
|
|
|
+ if (a != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ beginColor = a.getColor(R.styleable.ArcView_startColor, beginColor);
|
|
|
|
|
+ endColor = a.getColor(R.styleable.ArcView_endColor, endColor);
|
|
|
|
|
+ backgroundColor = a.getColor(R.styleable.ArcView_backgroundColor, backgroundColor);
|
|
|
|
|
+ arcWidthInPixels = a.getDimension(R.styleable.ArcView_arcWidth, arcWidthInPixels);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ a.recycle();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void init(Context context, AttributeSet attributeSet) {
|
|
|
|
|
+ if (attributeSet != null) {
|
|
|
|
|
+ readAttributes(attributeSet);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
|
+
|
|
|
|
|
+ canvasPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
|
+ canvasPaint.setColor(backgroundColor);
|
|
|
|
|
+
|
|
|
|
|
+ getRootView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onGlobalLayout() {
|
|
|
|
|
+ getRootView().getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
|
|
|
|
+
|
|
|
|
|
+ float left = getPaddingLeft();
|
|
|
|
|
+ float top = getPaddingTop();
|
|
|
|
|
+ float right = getWidth() - getPaddingRight();
|
|
|
|
|
+ float height = (getHeight() - (getPaddingTop() + getPaddingBottom())) * 2;
|
|
|
|
|
+ float bottom = top + height;
|
|
|
|
|
+
|
|
|
|
|
+ arcRect = new RectF(left, top, right, bottom);
|
|
|
|
|
+
|
|
|
|
|
+ Shader shader = new LinearGradient(left, 0, right, 0, beginColor, endColor,
|
|
|
|
|
+ Shader.TileMode.CLAMP);
|
|
|
|
|
+ arcPaint.setShader(shader);
|
|
|
|
|
+
|
|
|
|
|
+ left += arcWidthInPixels;
|
|
|
|
|
+ top += arcWidthInPixels;
|
|
|
|
|
+ right -= arcWidthInPixels;
|
|
|
|
|
+ bottom -= arcWidthInPixels;
|
|
|
|
|
+
|
|
|
|
|
+ subArcRect = new RectF(left, top, right, bottom);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onDraw(Canvas canvas) {
|
|
|
|
|
+ canvas.drawPaint(canvasPaint);
|
|
|
|
|
+
|
|
|
|
|
+ if (arcRect != null) {
|
|
|
|
|
+ canvas.drawArc(arcRect, 180, 180, true, arcPaint);
|
|
|
|
|
+ canvas.drawArc(subArcRect, 180, 180, true, canvasPaint);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|