Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.48 KB | None | 0 0
  1. package com.imperiamobile.stchealthapp.utils;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.support.v4.content.ContextCompat;
  9. import android.util.AttributeSet;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12.  
  13. import com.imperiamobile.stchealthapp.R;
  14.  
  15. import java.util.ArrayList;
  16. import java.util.List;
  17.  
  18. /**
  19.  * Created by Georgi Nikolov on 20.1.2017 г..
  20.  */
  21.  
  22. public class CustomScale extends View {
  23.     private static int screenSize = 480;
  24.     private static float pxmm = screenSize / 22.f;
  25.     private int width, height, midScreenPoint;
  26.     private float startingPoint = 0;
  27.     private float downpoint = 0, movablePoint = 0, downPointClone = 0;
  28.     private float mainPoint = 0, mainPointClone = 0;
  29.     private boolean isDown = false;
  30.     private boolean isUpward = false;
  31.     private boolean isMove;
  32.     private onViewUpdateListener mListener;
  33.     private Paint rulerPaint, pinkPaint;
  34.     private int endPoint;
  35.     private boolean isSizeChanged = false;
  36.     private float userStartingPoint = 0f;
  37.     private int scaleLineMedium;
  38.     private int scaleLineLarge;
  39.     private boolean isFirstTime = true;
  40.     private boolean isHorizontal = false;
  41.     private int maxSteps;
  42.     private float maxMainPoint = 0;
  43.     private int min, max;
  44.  
  45.     public CustomScale(Context context, AttributeSet foo) {
  46.  
  47.         super(context, foo);
  48.         if (!isInEditMode()) {
  49.             init(context);
  50.         }
  51.     }
  52.  
  53.     /**
  54.      * Sets the direction of the scroll and draws the ruler accordingly
  55.      *
  56.      * @param isHorizontal boolean scroll direction
  57.      */
  58.     public void setIsHorizontal(boolean isHorizontal) {
  59.  
  60.         this.isHorizontal = isHorizontal;
  61.     }
  62.  
  63.     public interface onViewUpdateListener {
  64.         void onViewUpdate(int result);
  65.     }
  66.  
  67.  
  68.     private void init(Context context) {
  69.  
  70.         rulerPaint = new Paint();
  71.         rulerPaint.setStyle(Paint.Style.STROKE);
  72.         rulerPaint.setStrokeWidth(2);
  73.         rulerPaint.setAntiAlias(false);
  74.         rulerPaint.setColor(Color.BLACK);
  75.         pinkPaint = new Paint();
  76.         pinkPaint.setStyle(Paint.Style.STROKE);
  77.         pinkPaint.setStrokeWidth(4);
  78.         pinkPaint.setAntiAlias(false);
  79.         pinkPaint.setColor(ContextCompat.getColor(context, R.color.button_pink));
  80.         scaleLineMedium = (int) getResources().getDimension(R.dimen.scale_line_medium);
  81.         scaleLineLarge = (int) getResources().getDimension(R.dimen.scale_line_large);
  82.     }
  83.  
  84.     private void setMaxMainPoint() {
  85.  
  86.         maxMainPoint = -(pxmm * maxSteps);
  87.     }
  88.  
  89.     public void setUpdateListener(onViewUpdateListener onViewUpdateListener) {
  90.  
  91.         mListener = onViewUpdateListener;
  92.     }
  93.  
  94.     @Override
  95.     public void onSizeChanged(int w, int h, int oldW, int oldH) {
  96.  
  97.         width = w;
  98.         height = h;
  99.         if (isHorizontal) {
  100.             screenSize = width;
  101.             midScreenPoint = width / 2;
  102.             endPoint = height - 40;
  103.         } else {
  104.             screenSize = height;
  105.             midScreenPoint = height / 2;
  106.             endPoint = width - 40;
  107.         }
  108.         pxmm = screenSize / 22.f;
  109.         setMaxMainPoint();
  110.         if (isSizeChanged) {
  111.             isSizeChanged = false;
  112.             mainPoint = midScreenPoint - (userStartingPoint * 10 * pxmm);
  113.         }
  114.     }
  115.  
  116.     @Override
  117.     public void onDraw(Canvas canvas) {
  118.  
  119.         startingPoint = mainPoint;
  120.         for (int i = 5; ; ++i) {
  121.             if (startingPoint > screenSize) {
  122.                 break;
  123.             }
  124.             startingPoint = startingPoint + pxmm;
  125.             if (isHorizontal) {
  126.                 int size = (i % 5 == 0) ? height - scaleLineMedium : height - scaleLineLarge;
  127.                 canvas.drawLine(startingPoint, height - size, startingPoint, height, rulerPaint);
  128.             } else {
  129.                 int size = (i % 5 == 0) ? endPoint - scaleLineLarge : endPoint - scaleLineMedium;
  130.                 canvas.drawLine(0, startingPoint, endPoint - size, startingPoint, rulerPaint);
  131.             }
  132.         }
  133.         if (isHorizontal) {
  134.             canvas.drawLine(midScreenPoint, height, midScreenPoint, scaleLineMedium / 2, pinkPaint);
  135.         } else {
  136.             canvas.drawLine(0, midScreenPoint, endPoint, midScreenPoint, pinkPaint);
  137.         }
  138.     }
  139.  
  140.     @Override
  141.     public boolean onTouchEvent(MotionEvent event) {
  142.  
  143.         mainPointClone = mainPoint;
  144.         if (mainPoint < 0) {
  145.             mainPointClone = -mainPoint;
  146.         }
  147.         if (mainPoint > userStartingPoint) {
  148.             mainPoint = userStartingPoint;
  149.             return true;
  150.         }
  151.         if (mListener != null) {
  152.             float result = (((midScreenPoint + mainPointClone) / (pxmm * 10)) * 10 + (min - 11));
  153.             int value = (int) (Math.round(result * 10f) / 10f);
  154.             if (value > max) {
  155.                 value = max;
  156.             }
  157.             mListener.onViewUpdate(value);
  158.         }
  159.  
  160.         switch (event.getAction()) {
  161.             case MotionEvent.ACTION_DOWN:
  162.                 isMove = true;
  163.                 isDown = false;
  164.                 isUpward = false;
  165.                 if (isHorizontal) {
  166.                     downpoint = event.getX();
  167.                     downPointClone = event.getX();
  168.                 } else {
  169.                     downpoint = event.getY();
  170.                     downPointClone = event.getY();
  171.                 }
  172.  
  173.                 break;
  174.             case MotionEvent.ACTION_MOVE:
  175.                 if (isHorizontal) {
  176.                     movablePoint = event.getX();
  177.                 } else {
  178.                     movablePoint = event.getY();
  179.                 }
  180.  
  181.                 if (downPointClone > movablePoint) {
  182. /**
  183.  * if user first starts moving downward and then upwards then
  184.  * this method makes it to move upward
  185.  */
  186.                     if (isUpward) {
  187.                         if (isHorizontal) {
  188.                             downpoint = event.getX();
  189.                         } else {
  190.                             downpoint = event.getY();
  191.                         }
  192.  
  193.                         downPointClone = downpoint;
  194.                     }
  195.                     isDown = true;
  196.                     isUpward = false;
  197. /**
  198.  * make this differnce of 1, otherwise it moves very fast and
  199.  * nothing shows clearly
  200.  */
  201.                     if (downPointClone - movablePoint > 1) {
  202.                         mainPoint = mainPoint + (-(downPointClone - movablePoint));
  203.                         downPointClone = movablePoint;
  204.                         if (mainPoint < maxMainPoint) {
  205.                             mainPoint = maxMainPoint;
  206.                             isMove = false;
  207.                         }
  208.                         invalidate();
  209.                     }
  210.                 } else {
  211. // downwards
  212.                     if (isMove) {
  213. /**
  214.  * if user first starts moving upward and then downwards,
  215.  * then this method makes it to move upward
  216.  */
  217.                         if (isDown) {
  218.                             if (isHorizontal) {
  219.                                 downpoint = event.getX();
  220.                             } else {
  221.                                 downpoint = event.getY();
  222.                             }
  223.                             downPointClone = downpoint;
  224.                         }
  225.                         isDown = false;
  226.                         isUpward = true;
  227.                         if (movablePoint - downpoint > 1) {
  228.                             mainPoint = mainPoint + ((movablePoint - downPointClone));
  229.                             downPointClone = movablePoint;
  230.                             if (mainPoint > 0) {
  231.                                 mainPoint = 0;
  232.                                 isMove = false;
  233.                             }
  234.                             invalidate();
  235.                         }
  236.                     }
  237.                 }
  238.                 break;
  239.             case MotionEvent.ACTION_UP:
  240.             case MotionEvent.ACTION_CANCEL:
  241.                 if (isDown) {
  242.                     mainPoint = (mainPoint + ((movablePoint - downPointClone)));
  243.                     getClosestLine();
  244.                 } else {
  245.                     mainPoint = (mainPoint + (-(downPointClone - movablePoint)));
  246.                     getClosestLine();
  247.                 }
  248.             default:
  249.                 break;
  250.         }
  251.         return true;
  252.     }
  253.  
  254.     private void getClosestLine() {
  255.  
  256.         List<Float> lines = new ArrayList<>();
  257.         startingPoint = mainPoint;
  258.  
  259.         while (true) {
  260.             if (startingPoint > screenSize) {
  261.                 break;
  262.             }
  263.             startingPoint = startingPoint + pxmm;
  264.             lines.add(startingPoint);
  265.         }
  266.  
  267.         for (int i = 0; i < lines.size() - 1; i++) {
  268.             if (lines.get(i) == midScreenPoint) {
  269.                 return;
  270.             }
  271.             if (lines.get(i) < midScreenPoint && lines.get(i + 1) > midScreenPoint) {
  272.                 if (midScreenPoint - lines.get(i) >= lines.get(i + 1) - midScreenPoint) {
  273.                     mainPoint = mainPoint - (lines.get(i + 1) - midScreenPoint);
  274.                     break;
  275.                 } else {
  276.                     mainPoint = mainPoint + (midScreenPoint - lines.get(i));
  277.                     break;
  278.                 }
  279.             }
  280.         }
  281.     }
  282.  
  283.     public void setMinMaxValuesForTV(int min, int max) {
  284.  
  285.         this.min = min;
  286.         this.max = max;
  287.         maxSteps = max - min;
  288.         setMaxMainPoint();
  289.     }
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement