44 | * must call {@link #createBackgroundBitmapCanvas()} and 45 | * {@link #createForegroundBitmapCanvas()} inside this method. 46 | *
47 | */ 48 | protected abstract void updateFrontAndBackBitmaps(); 49 | 50 | private void initAttributeSet(Context context, AttributeSet attrs) { 51 | if (attrs == null) 52 | return; 53 | TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LinearGauge, 0, 0); 54 | 55 | int orientation = a.getInt(R.styleable.LinearGauge_sv_orientation, -1); 56 | if (orientation != -1) 57 | setOrientation(Orientation.values()[orientation]); 58 | a.recycle(); 59 | } 60 | 61 | @Override 62 | protected void onSizeChanged(int w, int h, int oldW, int oldH) { 63 | super.onSizeChanged(w, h, oldW, oldH); 64 | 65 | updateBackgroundBitmap(); 66 | } 67 | 68 | @Override 69 | protected void updateBackgroundBitmap() { 70 | updateFrontAndBackBitmaps(); 71 | } 72 | 73 | protected final Canvas createForegroundBitmapCanvas() { 74 | if (getWidthPa() == 0 || getHeightPa() == 0) 75 | return new Canvas(); 76 | foregroundBitmap = Bitmap.createBitmap(getWidthPa(), getHeightPa(), Bitmap.Config.ARGB_8888); 77 | return new Canvas(foregroundBitmap); 78 | } 79 | 80 | @Override 81 | protected void onDraw(Canvas canvas) { 82 | super.onDraw(canvas); 83 | 84 | if (orientation == Orientation.HORIZONTAL) 85 | rect.set(0, 0 86 | , (int)(getWidthPa() * getOffsetSpeed()), getHeightPa()); 87 | else 88 | rect.set(0, getHeightPa() - (int)(getHeightPa() * getOffsetSpeed()) 89 | , getWidthPa(), getHeightPa()); 90 | 91 | canvas.translate(getPadding(), getPadding()); 92 | canvas.drawBitmap(foregroundBitmap, rect, rect, paint); 93 | canvas.translate(-getPadding(), -getPadding()); 94 | 95 | drawSpeedUnitText(canvas); 96 | } 97 | 98 | public Orientation getOrientation() { 99 | return orientation; 100 | } 101 | 102 | /** 103 | * change fill orientation, 104 | * this will change view width and height. 105 | * @param orientation new orientation. 106 | */ 107 | public void setOrientation(Orientation orientation) { 108 | this.orientation = orientation; 109 | if (!isAttachedToWindow()) 110 | return; 111 | requestLayout(); 112 | updateBackgroundBitmap(); 113 | invalidate(); 114 | } 115 | 116 | public enum Orientation { 117 | HORIZONTAL, VERTICAL 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /speedlib/src/main/java/com/tools/speedlib/views/base/Speedometer.java: -------------------------------------------------------------------------------- 1 | package com.tools.speedlib.views.base; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Bitmap; 6 | import android.graphics.Canvas; 7 | import android.graphics.Color; 8 | import android.graphics.Paint; 9 | import android.util.AttributeSet; 10 | 11 | import com.tools.speedlib.R; 12 | import com.tools.speedlib.views.components.Indicators.ImageIndicator; 13 | import com.tools.speedlib.views.components.Indicators.Indicator; 14 | import com.tools.speedlib.views.components.Indicators.NoIndicator; 15 | import com.tools.speedlib.views.components.note.Note; 16 | 17 | import java.util.ArrayList; 18 | 19 | /** 20 | * this Library build By Anas Altair 21 | * see it on GitHub 22 | */ 23 | @SuppressWarnings("unused") 24 | public abstract class Speedometer extends Gauge { 25 | 26 | private Indicator indicator; 27 | private Paint circleBackPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 28 | private float speedometerWidth = dpTOpx(30f); 29 | 30 | private int markColor = Color.WHITE 31 | , lowSpeedColor = Color.GREEN 32 | , mediumSpeedColor = Color.YELLOW 33 | , highSpeedColor = Color.RED 34 | , backgroundCircleColor = Color.WHITE; 35 | 36 | private int startDegree = 135, endDegree = 135+270; 37 | /** to rotate indicator */ 38 | private float degree = startDegree; 39 | 40 | /** array to contain all notes that will be draw */ 41 | private ArrayListbut it well be removed when call {@code speedometer.removeAllNotes()}.
*/ 21 | public static final int INFINITE = -1; 22 | 23 | private float density; 24 | 25 | private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG) 26 | , backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 27 | private float paddingLeft, paddingTop 28 | , paddingRight, paddingBottom; 29 | private Bitmap backgroundBitmap; 30 | private Position position = Position.CenterIndicator; 31 | private Align align = Align.Top; 32 | private int noteW = 0, noteH = 0, containsW = 0, containsH = 0; 33 | private float cornersRound = 5f; 34 | /** dialog's triangle Height */ 35 | private float triangleHeight; 36 | 37 | protected Note (Context context) { 38 | this.density = context.getResources().getDisplayMetrics().density; 39 | init(); 40 | } 41 | 42 | private void init() { 43 | triangleHeight = dpTOpx(12f); 44 | backgroundPaint.setColor(Color.parseColor("#d6d7d7")); 45 | setPadding(dpTOpx(7f), dpTOpx(7f), dpTOpx(7f), dpTOpx(7f)); 46 | } 47 | 48 | public float dpTOpx(float dp) { 49 | return dp * density; 50 | } 51 | 52 | /** 53 | * draw inside note's dialog. 54 | * @param canvas canvas to draw. 55 | * @param leftX left x position to start drawing. 56 | * @param topY top y position to start drawing. 57 | */ 58 | protected abstract void drawContains(Canvas canvas, float leftX, float topY); 59 | 60 | /** 61 | * called by speedometer after create the Note.7 | * this Library build By Anas Altair 8 | * see it on GitHub 9 | */ 10 | public interface OnSectionChangeListener { 11 | /** 12 | * Notification that the section has changed. 13 | * 14 | * @param oldSection where indicator came from. 15 | * @param newSection where indicator move to. 16 | */ 17 | void onSectionChangeListener(byte oldSection, byte newSection); 18 | } 19 | -------------------------------------------------------------------------------- /speedlib/src/main/java/com/tools/speedlib/views/util/OnSpeedChangeListener.java: -------------------------------------------------------------------------------- 1 | package com.tools.speedlib.views.util; 2 | 3 | 4 | import com.tools.speedlib.views.base.Gauge; 5 | 6 | /** 7 | * A callback that notifies clients when the speed has been 8 | * changed (just when speed change in integer). 9 | *
10 | * this Library build By Anas Altair
11 | * see it on GitHub
12 | */
13 | public interface OnSpeedChangeListener {
14 | /**
15 | * Notification that the speed has changed.
16 | *
17 | * @param gauge the gauge who change.
18 | * @param isSpeedUp if speed increase.
19 | * @param isByTremble true if speed has changed by Tremble.
20 | */
21 | void onSpeedChange(Gauge gauge, boolean isSpeedUp, boolean isByTremble);
22 | }
23 |
--------------------------------------------------------------------------------
/speedlib/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |