Skip to content

Commit

Permalink
change indicatorWidth to width, indicatorColor to color.
Browse files Browse the repository at this point in the history
Signed-off-by: Anas Altair <[email protected]>
  • Loading branch information
anastr committed Dec 28, 2019
1 parent 51148c0 commit b643b94
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void setSpeedTextSize(View view) {
public void setIndicatorColor(View view) {
EditText indicatorColor = findViewById(R.id.indicatorColor);
try{
speedView.getIndicator().setIndicatorColor(Color.parseColor(indicatorColor.getText().toString()));
speedView.getIndicator().setColor(Color.parseColor(indicatorColor.getText().toString()));
} catch (Exception e) {
indicatorColor.setError(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void addRandomSpeedometer(View view) {
case 6:
speedometer = new ImageSpeedometer(this);
speedometer.setIndicator(Indicator.Indicators.HalfLineIndicator);
speedometer.getIndicator().setIndicatorWidth(speedometer.dpTOpx(5f));
speedometer.getIndicator().setWidth(speedometer.dpTOpx(5f));
speedometer.setSpeedTextColor(Color.WHITE);
speedometer.setUnitTextColor(Color.WHITE);
((ImageSpeedometer)speedometer).setImageSpeedometer(R.drawable.for_image_speedometer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected void onCreate(Bundle savedInstanceState) {
seekBarWidth.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int width, boolean b) {
speedometer.getIndicator().setIndicatorWidth(speedometer.dpTOpx(width));
speedometer.getIndicator().setWidth(speedometer.dpTOpx(width));
textWidth.setText(String.format(Locale.getDefault(), "%ddp", width));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class AwesomeSpeedometer @JvmOverloads constructor(context: Context, attrs: Attr
override fun defaultSpeedometerValues() {
indicator = TriangleIndicator(context)
indicator.apply {
indicatorWidth = dpTOpx(25f)
indicatorColor = -0xff191a
width = dpTOpx(25f)
color = -0xff191a
}
super.setStartEndDegree(135, 135 + 320)
super.speedometerWidth = dpTOpx(60f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class DeluxeSpeedView @JvmOverloads constructor(context: Context, attrs: Attribu

override fun defaultSpeedometerValues() {
indicator = NormalSmallIndicator(context)
indicator.indicatorColor = -0xff0014
indicator.color = -0xff0014
super.backgroundCircleColor = -0xdededf
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ class PointerSpeedometer @JvmOverloads constructor(context: Context, attrs: Attr
override fun defaultSpeedometerValues() {
indicator = SpindleIndicator(context)
indicator.apply {
indicatorWidth = dpTOpx(16f)
indicatorColor = -0x1
width = dpTOpx(16f)
color = -0x1
}
super.backgroundCircleColor = -0xb73317
super.speedometerWidth = dpTOpx(10f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ abstract class Speedometer @JvmOverloads constructor(context: Context, attrs: At
speedometerWidth = a.getDimension(R.styleable.Speedometer_sv_speedometerWidth, speedometerWidth)
startDegree = a.getInt(R.styleable.Speedometer_sv_startDegree, startDegree)
endDegree = a.getInt(R.styleable.Speedometer_sv_endDegree, endDegree)
indicator.indicatorWidth = a.getDimension(R.styleable.Speedometer_sv_indicatorWidth, indicator.indicatorWidth)
indicator.width = a.getDimension(R.styleable.Speedometer_sv_indicatorWidth, indicator.width)
cutPadding = a.getDimension(R.styleable.Speedometer_sv_cutPadding, cutPadding.toFloat()).toInt()
tickNumber = a.getInteger(R.styleable.Speedometer_sv_tickNumber, ticks.size)
tickRotation = a.getBoolean(R.styleable.Speedometer_sv_tickRotation, tickRotation)
tickPadding = a.getDimension(R.styleable.Speedometer_sv_tickPadding, tickPadding.toFloat()).toInt()
indicator.indicatorColor = a.getColor(R.styleable.Speedometer_sv_indicatorColor, indicator.indicatorColor)
indicator.color = a.getColor(R.styleable.Speedometer_sv_indicatorColor, indicator.color)
isWithIndicatorLight = a.getBoolean(R.styleable.Speedometer_sv_withIndicatorLight, isWithIndicatorLight)
indicatorLightColor = a.getColor(R.styleable.Speedometer_sv_indicatorLightColor, indicatorLightColor)
degree = startDegree.toFloat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ import android.graphics.RectF
*/
class ImageIndicator
/**
* create indicator from bitmap, the indicator direction must be up.<br></br>
* create indicator from bitmap, the indicator direction must be up.
*
* center indicator position will be center of speedometer.
* @param context you can use `getApplicationContext()`.
* @param bitmapIndicator the indicator.
* @param width the custom width of the indicator.
* @param height the custom height of the indicator.
* @param imageWidth the custom width of the indicator.
* @param imageHeight the custom height of the indicator.
* @throws IllegalArgumentException if `width <= 0 OR height <= 0`.
*/
@JvmOverloads constructor(context: Context, private val bitmapIndicator: Bitmap, private val width: Int = bitmapIndicator.width, private val height: Int = bitmapIndicator.height) : Indicator<ImageIndicator>(context) {
@JvmOverloads constructor(context: Context, private val bitmapIndicator: Bitmap, private val imageWidth: Int = bitmapIndicator.width, private val imageHeight: Int = bitmapIndicator.height) : Indicator<ImageIndicator>(context) {
private val bitmapRect = RectF()

override val defaultIndicatorWidth: Float
get() = 0f

/**
* create indicator from resources, the indicator direction must be up.<br></br>
* create indicator from resources, the indicator direction must be up.
*
* center indicator position will be center of speedometer.
* @param context you can use `getApplicationContext()`.
* @param resource the image id.
*/
constructor(context: Context, resource: Int) : this(context, BitmapFactory.decodeResource(context.resources, resource))

/**
* create indicator from resources, the indicator direction must be up.<br></br>
* create indicator from resources, the indicator direction must be up.
*
* center indicator position will be center of speedometer.
* @param context you can use `getApplicationContext()`.
* @param resource the image id.
Expand All @@ -46,14 +46,14 @@ class ImageIndicator
constructor(context: Context, resource: Int, width: Int, height: Int) : this(context, BitmapFactory.decodeResource(context.resources, resource), width, height)

init {
require(width > 0) { "width must be bigger than 0" }
require(height > 0) { "height must be bigger than 0" }
require(imageWidth > 0) { "imageWidth must be bigger than 0" }
require(imageHeight > 0) { "imageHeight must be bigger than 0" }
}

override fun draw(canvas: Canvas, degree: Float) {
canvas.save()
canvas.rotate(90f + degree, getCenterX(), getCenterY())
bitmapRect.set(getCenterX() - width / 2f, getCenterY() - height / 2f, getCenterX() + width / 2f, getCenterY() + height / 2f)
bitmapRect.set(getCenterX() - imageWidth / 2f, getCenterY() - imageHeight / 2f, getCenterX() + imageWidth / 2f, getCenterY() + imageHeight / 2f)
canvas.drawBitmap(bitmapIndicator, null, bitmapRect, indicatorPaint)
canvas.restore()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ abstract class Indicator<I : Indicator<I>> (context: Context): Observable() {

protected var indicatorPaint = Paint(Paint.ANTI_ALIAS_FLAG)
private val density: Float = context.resources.displayMetrics.density
protected abstract val defaultIndicatorWidth: Float

protected var speedometer :Speedometer? = null

Expand All @@ -24,7 +23,7 @@ abstract class Indicator<I : Indicator<I>> (context: Context): Observable() {
* between [Indicator.Indicators], it will be ignored
* when using [ImageIndicator].
*/
var indicatorWidth: Float = 0f
var width: Float = 0f
set(indicatorWidth) {
field = indicatorWidth
speedometer?.let { updateIndicator() }
Expand All @@ -36,7 +35,7 @@ abstract class Indicator<I : Indicator<I>> (context: Context): Observable() {
* change indicator's color,
* this option will be ignored when using [ImageIndicator].
*/
var indicatorColor = -0xde690d
var color = -0xde690d
set(indicatorColor) {
field = indicatorColor
speedometer?.let { updateIndicator() }
Expand Down Expand Up @@ -68,8 +67,7 @@ abstract class Indicator<I : Indicator<I>> (context: Context): Observable() {
fun getCenterY(): Float = if (speedometer != null) speedometer!!.size / 2f else 0f

init {
indicatorPaint.color = indicatorColor
indicatorWidth = defaultIndicatorWidth
indicatorPaint.color = color
}

abstract fun draw(canvas: Canvas, degree: Float)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class KiteIndicator(context: Context) : Indicator<KiteIndicator>(context) {
private val indicatorPath = Path()
private var bottomY: Float = 0.toFloat()

override val defaultIndicatorWidth: Float
get() = dpTOpx(12f)
init {
width = dpTOpx(12f)
}

override fun getBottom(): Float {
return bottomY
Expand All @@ -33,11 +34,11 @@ class KiteIndicator(context: Context) : Indicator<KiteIndicator>(context) {
indicatorPath.reset()
indicatorPath.moveTo(getCenterX(), speedometer!!.padding.toFloat())
bottomY = getViewSize() * .5f + speedometer!!.padding
indicatorPath.lineTo(getCenterX() - indicatorWidth, bottomY)
indicatorPath.lineTo(getCenterX(), bottomY + indicatorWidth)
indicatorPath.lineTo(getCenterX() + indicatorWidth, bottomY)
indicatorPath.lineTo(getCenterX() - width, bottomY)
indicatorPath.lineTo(getCenterX(), bottomY + width)
indicatorPath.lineTo(getCenterX() + width, bottomY)

indicatorPaint.color = indicatorColor
indicatorPaint.color = color
}

override fun setWithEffects(withEffects: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ class LineIndicator(context: Context, private val mode: Float) : Indicator<LineI

private val indicatorPath = Path()

override val defaultIndicatorWidth: Float
get() = dpTOpx(8f)
init {
width = dpTOpx(8f)
}

override fun getBottom(): Float {
return getCenterY() * mode
Expand All @@ -34,8 +35,8 @@ class LineIndicator(context: Context, private val mode: Float) : Indicator<LineI
indicatorPath.lineTo(getCenterX(), getCenterY() * mode)

indicatorPaint.style = Paint.Style.STROKE
indicatorPaint.strokeWidth = indicatorWidth
indicatorPaint.color = indicatorColor
indicatorPaint.strokeWidth = width
indicatorPaint.color = color
}

override fun setWithEffects(withEffects: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class NeedleIndicator(context: Context) : Indicator<NeedleIndicator>(context) {
private val circlePaint = Paint(Paint.ANTI_ALIAS_FLAG)
private var bottomY: Float = 0.toFloat()

override val defaultIndicatorWidth: Float
get() = dpTOpx(12f)

init {
width = dpTOpx(12f)
circlePaint.style = Paint.Style.STROKE
}

Expand All @@ -40,17 +38,17 @@ class NeedleIndicator(context: Context) : Indicator<NeedleIndicator>(context) {
indicatorPath.reset()
circlePath.reset()
indicatorPath.moveTo(getCenterX(), speedometer!!.padding.toFloat())
bottomY = (indicatorWidth * sin(Math.toRadians(260.0))).toFloat() + getViewSize() * .5f + speedometer!!.padding.toFloat()
val xLeft = (indicatorWidth * cos(Math.toRadians(260.0))).toFloat() + getViewSize() * .5f + speedometer!!.padding.toFloat()
bottomY = (width * sin(Math.toRadians(260.0))).toFloat() + getViewSize() * .5f + speedometer!!.padding.toFloat()
val xLeft = (width * cos(Math.toRadians(260.0))).toFloat() + getViewSize() * .5f + speedometer!!.padding.toFloat()
indicatorPath.lineTo(xLeft, bottomY)
val rectF = RectF(getCenterX() - indicatorWidth, getCenterY() - indicatorWidth, getCenterX() + indicatorWidth, getCenterY() + indicatorWidth)
val rectF = RectF(getCenterX() - width, getCenterY() - width, getCenterX() + width, getCenterY() + width)
indicatorPath.arcTo(rectF, 260f, 20f)

val circleWidth = indicatorWidth * .25f
circlePath.addCircle(getCenterX(), getCenterY(), indicatorWidth - circleWidth * .5f + .6f, Path.Direction.CW)
val circleWidth = width * .25f
circlePath.addCircle(getCenterX(), getCenterY(), width - circleWidth * .5f + .6f, Path.Direction.CW)

indicatorPaint.color = indicatorColor
circlePaint.color = indicatorColor
indicatorPaint.color = color
circlePaint.color = color
circlePaint.strokeWidth = circleWidth
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import android.graphics.Canvas
*/
class NoIndicator(context: Context) : Indicator<NoIndicator>(context) {

override val defaultIndicatorWidth: Float
get() = 0f

override fun draw(canvas: Canvas, degree: Float) {}

override fun updateIndicator() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class NormalIndicator(context: Context) : Indicator<NormalIndicator>(context) {
private val indicatorPath = Path()
private var bottomY: Float = 0.toFloat()

override val defaultIndicatorWidth: Float
get() = dpTOpx(12f)
init {
width = dpTOpx(12f)
}

override fun getBottom(): Float {
return bottomY
Expand All @@ -33,12 +34,12 @@ class NormalIndicator(context: Context) : Indicator<NormalIndicator>(context) {
indicatorPath.reset()
indicatorPath.moveTo(getCenterX(), speedometer!!.padding.toFloat())
bottomY = getViewSize() * 2f / 3f + speedometer!!.padding
indicatorPath.lineTo(getCenterX() - indicatorWidth, bottomY)
indicatorPath.lineTo(getCenterX() + indicatorWidth, bottomY)
val rectF = RectF(getCenterX() - indicatorWidth, bottomY - indicatorWidth, getCenterX() + indicatorWidth, bottomY + indicatorWidth)
indicatorPath.lineTo(getCenterX() - width, bottomY)
indicatorPath.lineTo(getCenterX() + width, bottomY)
val rectF = RectF(getCenterX() - width, bottomY - width, getCenterX() + width, bottomY + width)
indicatorPath.addArc(rectF, 0f, 180f)

indicatorPaint.color = indicatorColor
indicatorPaint.color = color
}

override fun setWithEffects(withEffects: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class NormalSmallIndicator(context: Context) : Indicator<NormalSmallIndicator>(c
private val indicatorPath = Path()
private var bottomY: Float = 0.toFloat()

override val defaultIndicatorWidth: Float
get() = dpTOpx(12f)
init {
width = dpTOpx(12f)
}

override fun getTop(): Float {
return getViewSize() / 5f + speedometer!!.padding
Expand All @@ -37,12 +38,12 @@ class NormalSmallIndicator(context: Context) : Indicator<NormalSmallIndicator>(c
indicatorPath.reset()
indicatorPath.moveTo(getCenterX(), getViewSize() / 5f + speedometer!!.padding)
bottomY = getViewSize() * 3f / 5f + speedometer!!.padding
indicatorPath.lineTo(getCenterX() - indicatorWidth, bottomY)
indicatorPath.lineTo(getCenterX() + indicatorWidth, bottomY)
val rectF = RectF(getCenterX() - indicatorWidth, bottomY - indicatorWidth, getCenterX() + indicatorWidth, bottomY + indicatorWidth)
indicatorPath.lineTo(getCenterX() - width, bottomY)
indicatorPath.lineTo(getCenterX() + width, bottomY)
val rectF = RectF(getCenterX() - width, bottomY - width, getCenterX() + width, bottomY + width)
indicatorPath.addArc(rectF, 0f, 180f)

indicatorPaint.color = indicatorColor
indicatorPaint.color = color
}

override fun setWithEffects(withEffects: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ class SpindleIndicator(context: Context) : Indicator<SpindleIndicator>(context)

private val indicatorPath = Path()

override val defaultIndicatorWidth: Float
get() = dpTOpx(16f)
init {
width = dpTOpx(16f)
}

override fun getTop(): Float {
return getViewSize() * .18f + speedometer!!.padding
Expand All @@ -30,10 +31,10 @@ class SpindleIndicator(context: Context) : Indicator<SpindleIndicator>(context)
override fun updateIndicator() {
indicatorPath.reset()
indicatorPath.moveTo(getCenterX(), getCenterY())
indicatorPath.quadTo(getCenterX() - indicatorWidth, getViewSize() * .34f + speedometer!!.padding, getCenterX(), getViewSize() * .18f + speedometer!!.padding)
indicatorPath.quadTo(getCenterX() + indicatorWidth, getViewSize() * .34f + speedometer!!.padding, getCenterX(), getCenterY())
indicatorPath.quadTo(getCenterX() - width, getViewSize() * .34f + speedometer!!.padding, getCenterX(), getViewSize() * .18f + speedometer!!.padding)
indicatorPath.quadTo(getCenterX() + width, getViewSize() * .34f + speedometer!!.padding, getCenterX(), getCenterY())

indicatorPaint.color = indicatorColor
indicatorPaint.color = color
}

override fun setWithEffects(withEffects: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class TriangleIndicator(context: Context) : Indicator<TriangleIndicator>(context
private var indicatorPath = Path()
private var indicatorTop = 0f

override val defaultIndicatorWidth: Float
get() = dpTOpx(25f)
init {
width = dpTOpx(25f)
}

override fun getTop(): Float {
return indicatorTop
}

override fun getBottom(): Float {
return indicatorTop + indicatorWidth
return indicatorTop + width
}

override fun draw(canvas: Canvas, degree: Float) {
Expand All @@ -34,12 +35,12 @@ class TriangleIndicator(context: Context) : Indicator<TriangleIndicator>(context
indicatorPath = Path()
indicatorTop = speedometer!!.padding.toFloat() + speedometer!!.speedometerWidth + dpTOpx(5f)
indicatorPath.moveTo(getCenterX(), indicatorTop)
indicatorPath.lineTo(getCenterX() - indicatorWidth, indicatorTop + indicatorWidth)
indicatorPath.lineTo(getCenterX() + indicatorWidth, indicatorTop + indicatorWidth)
indicatorPath.lineTo(getCenterX() - width, indicatorTop + width)
indicatorPath.lineTo(getCenterX() + width, indicatorTop + width)
indicatorPath.moveTo(0f, 0f)

val endColor = Color.argb(0, Color.red(indicatorColor), Color.green(indicatorColor), Color.blue(indicatorColor))
val linearGradient = LinearGradient(getCenterX(), indicatorTop, getCenterX(), indicatorTop + indicatorWidth, indicatorColor, endColor, Shader.TileMode.CLAMP)
val endColor = Color.argb(0, Color.red(color), Color.green(color), Color.blue(color))
val linearGradient = LinearGradient(getCenterX(), indicatorTop, getCenterX(), indicatorTop + width, color, endColor, Shader.TileMode.CLAMP)
indicatorPaint.shader = linearGradient
}

Expand Down

0 comments on commit b643b94

Please sign in to comment.