Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.imperiamobile.stchealthapp.utils;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.support.v4.content.ContextCompat;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
- import com.imperiamobile.stchealthapp.R;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by Georgi Nikolov on 20.1.2017 г..
- */
- public class CustomScale extends View {
- private static int screenSize = 480;
- private static float pxmm = screenSize / 22.f;
- private int width, height, midScreenPoint;
- private float startingPoint = 0;
- private float downpoint = 0, movablePoint = 0, downPointClone = 0;
- private float mainPoint = 0, mainPointClone = 0;
- private boolean isDown = false;
- private boolean isUpward = false;
- private boolean isMove;
- private onViewUpdateListener mListener;
- private Paint rulerPaint, pinkPaint;
- private int endPoint;
- private boolean isSizeChanged = false;
- private float userStartingPoint = 0f;
- private int scaleLineMedium;
- private int scaleLineLarge;
- private boolean isFirstTime = true;
- private boolean isHorizontal = false;
- private int maxSteps;
- private float maxMainPoint = 0;
- private int min, max;
- public CustomScale(Context context, AttributeSet foo) {
- super(context, foo);
- if (!isInEditMode()) {
- init(context);
- }
- }
- /**
- * Sets the direction of the scroll and draws the ruler accordingly
- *
- * @param isHorizontal boolean scroll direction
- */
- public void setIsHorizontal(boolean isHorizontal) {
- this.isHorizontal = isHorizontal;
- }
- public interface onViewUpdateListener {
- void onViewUpdate(int result);
- }
- private void init(Context context) {
- rulerPaint = new Paint();
- rulerPaint.setStyle(Paint.Style.STROKE);
- rulerPaint.setStrokeWidth(2);
- rulerPaint.setAntiAlias(false);
- rulerPaint.setColor(Color.BLACK);
- pinkPaint = new Paint();
- pinkPaint.setStyle(Paint.Style.STROKE);
- pinkPaint.setStrokeWidth(4);
- pinkPaint.setAntiAlias(false);
- pinkPaint.setColor(ContextCompat.getColor(context, R.color.button_pink));
- scaleLineMedium = (int) getResources().getDimension(R.dimen.scale_line_medium);
- scaleLineLarge = (int) getResources().getDimension(R.dimen.scale_line_large);
- }
- private void setMaxMainPoint() {
- maxMainPoint = -(pxmm * maxSteps);
- }
- public void setUpdateListener(onViewUpdateListener onViewUpdateListener) {
- mListener = onViewUpdateListener;
- }
- @Override
- public void onSizeChanged(int w, int h, int oldW, int oldH) {
- width = w;
- height = h;
- if (isHorizontal) {
- screenSize = width;
- midScreenPoint = width / 2;
- endPoint = height - 40;
- } else {
- screenSize = height;
- midScreenPoint = height / 2;
- endPoint = width - 40;
- }
- pxmm = screenSize / 22.f;
- setMaxMainPoint();
- if (isSizeChanged) {
- isSizeChanged = false;
- mainPoint = midScreenPoint - (userStartingPoint * 10 * pxmm);
- }
- }
- @Override
- public void onDraw(Canvas canvas) {
- startingPoint = mainPoint;
- for (int i = 5; ; ++i) {
- if (startingPoint > screenSize) {
- break;
- }
- startingPoint = startingPoint + pxmm;
- if (isHorizontal) {
- int size = (i % 5 == 0) ? height - scaleLineMedium : height - scaleLineLarge;
- canvas.drawLine(startingPoint, height - size, startingPoint, height, rulerPaint);
- } else {
- int size = (i % 5 == 0) ? endPoint - scaleLineLarge : endPoint - scaleLineMedium;
- canvas.drawLine(0, startingPoint, endPoint - size, startingPoint, rulerPaint);
- }
- }
- if (isHorizontal) {
- canvas.drawLine(midScreenPoint, height, midScreenPoint, scaleLineMedium / 2, pinkPaint);
- } else {
- canvas.drawLine(0, midScreenPoint, endPoint, midScreenPoint, pinkPaint);
- }
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- mainPointClone = mainPoint;
- if (mainPoint < 0) {
- mainPointClone = -mainPoint;
- }
- if (mainPoint > userStartingPoint) {
- mainPoint = userStartingPoint;
- return true;
- }
- if (mListener != null) {
- float result = (((midScreenPoint + mainPointClone) / (pxmm * 10)) * 10 + (min - 11));
- int value = (int) (Math.round(result * 10f) / 10f);
- if (value > max) {
- value = max;
- }
- mListener.onViewUpdate(value);
- }
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- isMove = true;
- isDown = false;
- isUpward = false;
- if (isHorizontal) {
- downpoint = event.getX();
- downPointClone = event.getX();
- } else {
- downpoint = event.getY();
- downPointClone = event.getY();
- }
- break;
- case MotionEvent.ACTION_MOVE:
- if (isHorizontal) {
- movablePoint = event.getX();
- } else {
- movablePoint = event.getY();
- }
- if (downPointClone > movablePoint) {
- /**
- * if user first starts moving downward and then upwards then
- * this method makes it to move upward
- */
- if (isUpward) {
- if (isHorizontal) {
- downpoint = event.getX();
- } else {
- downpoint = event.getY();
- }
- downPointClone = downpoint;
- }
- isDown = true;
- isUpward = false;
- /**
- * make this differnce of 1, otherwise it moves very fast and
- * nothing shows clearly
- */
- if (downPointClone - movablePoint > 1) {
- mainPoint = mainPoint + (-(downPointClone - movablePoint));
- downPointClone = movablePoint;
- if (mainPoint < maxMainPoint) {
- mainPoint = maxMainPoint;
- isMove = false;
- }
- invalidate();
- }
- } else {
- // downwards
- if (isMove) {
- /**
- * if user first starts moving upward and then downwards,
- * then this method makes it to move upward
- */
- if (isDown) {
- if (isHorizontal) {
- downpoint = event.getX();
- } else {
- downpoint = event.getY();
- }
- downPointClone = downpoint;
- }
- isDown = false;
- isUpward = true;
- if (movablePoint - downpoint > 1) {
- mainPoint = mainPoint + ((movablePoint - downPointClone));
- downPointClone = movablePoint;
- if (mainPoint > 0) {
- mainPoint = 0;
- isMove = false;
- }
- invalidate();
- }
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- case MotionEvent.ACTION_CANCEL:
- if (isDown) {
- mainPoint = (mainPoint + ((movablePoint - downPointClone)));
- getClosestLine();
- } else {
- mainPoint = (mainPoint + (-(downPointClone - movablePoint)));
- getClosestLine();
- }
- default:
- break;
- }
- return true;
- }
- private void getClosestLine() {
- List<Float> lines = new ArrayList<>();
- startingPoint = mainPoint;
- while (true) {
- if (startingPoint > screenSize) {
- break;
- }
- startingPoint = startingPoint + pxmm;
- lines.add(startingPoint);
- }
- for (int i = 0; i < lines.size() - 1; i++) {
- if (lines.get(i) == midScreenPoint) {
- return;
- }
- if (lines.get(i) < midScreenPoint && lines.get(i + 1) > midScreenPoint) {
- if (midScreenPoint - lines.get(i) >= lines.get(i + 1) - midScreenPoint) {
- mainPoint = mainPoint - (lines.get(i + 1) - midScreenPoint);
- break;
- } else {
- mainPoint = mainPoint + (midScreenPoint - lines.get(i));
- break;
- }
- }
- }
- }
- public void setMinMaxValuesForTV(int min, int max) {
- this.min = min;
- this.max = max;
- maxSteps = max - min;
- setMaxMainPoint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement