Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
shchurov committed Aug 7, 2016
1 parent b2bcd4c commit ef3198e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.github.shchurov.horizontalwheelview;

import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

import java.util.Arrays;

Expand All @@ -14,10 +12,6 @@

class Drawer {

private static final int DEFAULT_MARKS_COUNT = 40;
private static final int DEFAULT_NORMAL_COLOR = 0xffffffff;
private static final int DEFAULT_ACTIVE_COLOR = 0xff54acf0;
private static final boolean DEFAULT_SHOW_ACTIVE_RANGE = true;
private static final int DP_CURSOR_CORNERS_RADIUS = 1;
private static final int DP_NORMAL_MARK_WIDTH = 1;
private static final int DP_ZERO_MARK_WIDTH = 2;
Expand Down Expand Up @@ -47,22 +41,11 @@ class Drawer {
private RectF cursorRect = new RectF();
private int maxVisibleMarksCount;

Drawer(HorizontalWheelView view, AttributeSet attrs) {
Drawer(HorizontalWheelView view) {
this.view = view;
readAttrs(attrs);
initDpSizes();
}

private void readAttrs(AttributeSet attrs) {
TypedArray a = view.getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalWheelView);
int marksCount = a.getInt(R.styleable.HorizontalWheelView_marksCount, DEFAULT_MARKS_COUNT);
setMarksCount(marksCount);
normalColor = a.getColor(R.styleable.HorizontalWheelView_normalColor, DEFAULT_NORMAL_COLOR);
activeColor = a.getColor(R.styleable.HorizontalWheelView_activeColor, DEFAULT_ACTIVE_COLOR);
showActiveRange = a.getBoolean(R.styleable.HorizontalWheelView_showActiveRange, DEFAULT_SHOW_ACTIVE_RANGE);
a.recycle();
}

private void initDpSizes() {
normalMarkWidth = convertToPx(DP_NORMAL_MARK_WIDTH);
zeroMarkWidth = convertToPx(DP_ZERO_MARK_WIDTH);
Expand All @@ -81,6 +64,14 @@ void setMarksCount(int marksCount) {
scales = new float[maxVisibleMarksCount];
}

void setNormalColor(int color) {
normalColor = color;
}

void setActiveColor(int color) {
activeColor = color;
}

void setShowActiveRange(boolean show) {
showActiveRange = show;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class HorizontalWheelView extends View {

private static final int DP_DEFAULT_WIDTH = 200;
private static final int DP_DEFAULT_HEIGHT = 32;
private static final int DEFAULT_MARKS_COUNT = 40;
private static final int DEFAULT_NORMAL_COLOR = 0xffffffff;
private static final int DEFAULT_ACTIVE_COLOR = 0xff54acf0;
private static final boolean DEFAULT_SHOW_ACTIVE_RANGE = true;
private static final boolean DEFAULT_SNAP_TO_MARKS = false;
private static final boolean DEFAULT_END_LOCK = false;
private static final boolean DEFAULT_ONLY_POSITIVE_VALUES = false;

Expand All @@ -30,13 +35,24 @@ public class HorizontalWheelView extends View {

public HorizontalWheelView(Context context, AttributeSet attrs) {
super(context, attrs);
drawer = new Drawer(this);
touchHandler = new TouchHandler(this);
readAttrs(attrs);
drawer = new Drawer(this, attrs);
touchHandler = new TouchHandler(this, attrs);
}

private void readAttrs(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalWheelView);
int marksCount = a.getInt(R.styleable.HorizontalWheelView_marksCount, DEFAULT_MARKS_COUNT);
drawer.setMarksCount(marksCount);
int normalColor = a.getColor(R.styleable.HorizontalWheelView_normalColor, DEFAULT_NORMAL_COLOR);
drawer.setNormalColor(normalColor);
int activeColor = a.getColor(R.styleable.HorizontalWheelView_activeColor, DEFAULT_ACTIVE_COLOR);
drawer.setActiveColor(activeColor);
boolean showActiveRange = a.getBoolean(R.styleable.HorizontalWheelView_showActiveRange,
DEFAULT_SHOW_ACTIVE_RANGE);
drawer.setShowActiveRange(showActiveRange);
boolean snapToMarks = a.getBoolean(R.styleable.HorizontalWheelView_snapToMarks, DEFAULT_SNAP_TO_MARKS);
touchHandler.setSnapToMarks(snapToMarks);
endLock = a.getBoolean(R.styleable.HorizontalWheelView_endLock, DEFAULT_END_LOCK);
onlyPositiveValues = a.getBoolean(R.styleable.HorizontalWheelView_onlyPositiveValues,
DEFAULT_ONLY_POSITIVE_VALUES);
Expand Down Expand Up @@ -122,6 +138,20 @@ public void setShowActiveRange(boolean show) {
invalidate();
}

public void setNormaColor(int color) {
drawer.setNormalColor(color);
invalidate();
}

public void setActiveColor(int color) {
drawer.setActiveColor(color);
invalidate();
}

public void setSnapToMarks(boolean snapToMarks) {
touchHandler.setSnapToMarks(snapToMarks);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
return touchHandler.onTouchEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.DecelerateInterpolator;
Expand All @@ -20,7 +18,6 @@ class TouchHandler extends GestureDetector.SimpleOnGestureListener {
private static final float SCROLL_ANGLE_MULTIPLIER = 0.002f;
private static final float FLING_ANGLE_MULTIPLIER = 0.0002f;
private static final int SETTLING_DURATION_MULTIPLIER = 1000;
private static final boolean DEFAULT_SNAP_TO_MARKS = false;
private static final Interpolator INTERPOLATOR = new DecelerateInterpolator(2.5f);

private HorizontalWheelView view;
Expand All @@ -30,22 +27,19 @@ class TouchHandler extends GestureDetector.SimpleOnGestureListener {
private boolean snapToMarks;
private int scrollState = SCROLL_STATE_IDLE;

TouchHandler(HorizontalWheelView view, AttributeSet attrs) {
TouchHandler(HorizontalWheelView view) {
this.view = view;
readAttrs(attrs);
gestureDetector = new GestureDetector(view.getContext(), this);
}

private void readAttrs(AttributeSet attrs) {
TypedArray a = view.getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalWheelView);
snapToMarks = a.getBoolean(R.styleable.HorizontalWheelView_snapToMarks, DEFAULT_SNAP_TO_MARKS);
a.recycle();
}

void setListener(HorizontalWheelView.Listener listener) {
this.listener = listener;
}

void setSnapToMarks(boolean snapToMarks) {
this.snapToMarks = snapToMarks;
}

boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
int action = event.getActionMasked();
Expand Down

0 comments on commit ef3198e

Please sign in to comment.