kstanoev
4/7/2014 - 11:06 AM

Updating the position of the needle

Updating the position of the needle

public class Needle extends View {

    public Needle(Context context, float offsetRight) {
        super(context);

        this.needlePaint = new Paint();
        this.needlePaint.setColor(Color.RED);
        this.needlePaint.setStyle(Paint.Style.FILL);

        this.needleShape = new Path();
    }

    public void updatePosition(float y) {
        invalidate();
        this.currentY = y;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 20 is the max acceleration, 30 is the max value for the vertical axis, so 2/3 of 30 is 66.6%
        float y = (float) (this.halfHeight + (((this.halfHeight * (this.currentY / MAX_ACCELERATION)) * .666) * -1));

        this.needleShape.reset();
        this.needleShape.moveTo(this.width, y - this.halfNeedleHeight);
        this.needleShape.lineTo(this.pointerLeft, y);
        this.needleShape.lineTo(this.width, y + this.halfNeedleHeight);
        this.needleShape.close();

        canvas.drawPath(this.needleShape, this.needlePaint);
    }
}