Skip to content

Commit

Permalink
Bug fixes when setting layout params.
Browse files Browse the repository at this point in the history
  • Loading branch information
roxrook committed Oct 13, 2015
1 parent dbae568 commit a7b142e
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class RangeSliderView extends View {

private static final int DEFAULT_RANGE_COUNT = 5;

private static final int DEFAULT_HEIGHT_IN_DP = 50;

protected Paint paint;

protected Paint ripplePaint;
Expand All @@ -46,8 +48,6 @@ public class RangeSliderView extends View {

private int currentIndex;

private int height;

private float currentSlidingX;

private float currentSlidingY;
Expand Down Expand Up @@ -86,6 +86,8 @@ public class RangeSliderView extends View {

private float sliderRadiusPercent = DEFAULT_SLIDER_RADIUS_PERCENT;

private int layoutHeight;

public RangeSliderView(Context context) {
this(context, null);
}
Expand All @@ -100,7 +102,7 @@ public RangeSliderView(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RangeSliderView);
TypedArray sa = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.layout_height});
try {
height = sa.getDimensionPixelSize(
layoutHeight = sa.getLayoutDimension(
0, ViewGroup.LayoutParams.WRAP_CONTENT);
rangeCount = a.getInt(
R.styleable.RangeSliderView_rangeCount, DEFAULT_RANGE_COUNT);
Expand All @@ -127,13 +129,7 @@ public RangeSliderView(Context context, AttributeSet attrs, int defStyleAttr) {
setSlotRadiusPercent(slotRadiusPercent);
setSliderRadiusPercent(sliderRadiusPercent);

barHeight = (int) (height * barHeightPercent);
radius = height * sliderRadiusPercent;
slotRadius = height * slotRadiusPercent;
currentIndex = 0;

slotPositions = new float[rangeCount];

paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStrokeWidth(DEFAULT_PAINT_STROKE_WIDTH);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
Expand All @@ -146,10 +142,24 @@ public RangeSliderView(Context context, AttributeSet attrs, int defStyleAttr) {
@Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);

// Update radius after we got new height
updateRadius(getHeight());

// Compute drawing position again
preComputeDrawingPosition();

// Ready to draw now
return true;
}
});
currentIndex = 0;
}

private void updateRadius(int height) {
barHeight = (int) (height * barHeightPercent);
radius = height * sliderRadiusPercent;
slotRadius = height * slotRadiusPercent;
}

public int getRangeCount() {
Expand Down Expand Up @@ -300,6 +310,14 @@ private int measureHeight(int measureSpec) {
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
final int height;
if (layoutHeight == ViewGroup.LayoutParams.WRAP_CONTENT) {
height = dpToPx(getContext(), DEFAULT_HEIGHT_IN_DP);
} else if (layoutHeight == ViewGroup.LayoutParams.MATCH_PARENT) {
height = getMeasuredHeight();
} else {
height = layoutHeight;
}
result = height + getPaddingTop() + getPaddingBottom() + (2 * DEFAULT_PAINT_STROKE_WIDTH);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
Expand All @@ -321,7 +339,7 @@ private int measureWidth(int measureSpec) {
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = getSuggestedMinimumWidth() + getPaddingLeft() + getPaddingRight() + (2 * DEFAULT_PAINT_STROKE_WIDTH) + (int) (2 * radius);
result = specSize + getPaddingLeft() + getPaddingRight() + (2 * DEFAULT_PAINT_STROKE_WIDTH) + (int) (2 * radius);
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
Expand Down Expand Up @@ -549,6 +567,26 @@ public SavedState[] newArray(int size) {
};
}

/**
* Helper method to convert pixel to dp
* @param context
* @param px
* @return
*/
static int pxToDp(final Context context, final float px) {
return (int)(px / context.getResources().getDisplayMetrics().density);
}

/**
* Helper method to convert dp to pixel
* @param context
* @param dp
* @return
*/
static int dpToPx(final Context context, final float dp) {
return (int)(dp * context.getResources().getDisplayMetrics().density);
}

/**
* Interface to keep track sliding position
*/
Expand All @@ -557,7 +595,7 @@ public interface OnSlideListener {
/**
* Notify when slider change to new index position
*
* @param index
* @param index The index value of range count [0, rangeCount - 1]
*/
void onSlide(int index);
}
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/java/com/chan/rsv/sample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ public void onSlide(int index) {
smallSlider.setOnSlideListener(listener);
largeSlider.setOnSlideListener(listener);
}
}
}
53 changes: 47 additions & 6 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@

<com.github.channguyen.rsv.RangeSliderView
android:id="@+id/rsv_large"
android:layout_marginTop="100dp"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
rsv:filledColor="#FF6600"
rsv:rangeCount="10"
/>
Expand All @@ -32,7 +30,7 @@
android:id="@+id/rsv_custom"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
rsv:filledColor="#00cbff"
Expand All @@ -41,4 +39,47 @@
rsv:barHeightPercent="0.1"
/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
>

<com.github.channguyen.rsv.RangeSliderView
android:id="@+id/rsv_another"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
rsv:filledColor="#FF0000"
rsv:emptyColor="#a8aeb8"
rsv:rangeCount="5"
rsv:barHeightPercent="0.1"
android:layout_gravity="center_vertical"
android:layout_weight="1"
/>

<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Range Slider"
android:layout_gravity="center_vertical"
android:textColor="#000000"
android:layout_weight="1"
/>
</LinearLayout>

<com.github.channguyen.rsv.RangeSliderView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginTop="20dp"
rsv:filledColor="#00FF00"
rsv:emptyColor="#a8aeb8"
rsv:rangeCount="5"
rsv:barHeightPercent="0.1"
/>

</LinearLayout>

0 comments on commit a7b142e

Please sign in to comment.