Skip to content

Commit

Permalink
setters added
Browse files Browse the repository at this point in the history
  • Loading branch information
pawlo2102 committed May 14, 2018
1 parent a8ff2cb commit c861c35
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 55 deletions.
3 changes: 3 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion app/src/main/java/net/vrgsoft/slider/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package net.vrgsoft.slider;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onPointClick(int position){
Log.d(LOG_TAG, String.valueOf(position));
}
}
65 changes: 61 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,71 @@
tools:context="net.vrgsoft.slider.MainActivity">

<net.vrgsoft.library.Slider
app:pointsCount="8"
android:id="@+id/slider"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:animationDuration="1000"
app:pointPulseColor="#66ff4444"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:onPointClick="onPointClick"
app:pointInnerColor="@color/colorAccent"
app:pointOuterColor="@android:color/holo_purple"
android:orientation="horizontal"
app:pointPulseColor="#66ff4444"
app:lineInnerColor="@color/colorPrimaryDark"
app:lineStrokeWidth="2dp"
app:lineOuterColor="@color/colorAccent"
app:pointsCount="8" />

<net.vrgsoft.library.Slider
android:id="@+id/slider1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:animationDuration="1000"
app:layout_constraintTop_toBottomOf="@id/slider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:pointInnerColor="@android:color/holo_green_dark"
app:pointOuterColor="@android:color/holo_orange_dark"
app:pointPulseColor="@android:color/white"
app:lineInnerColor="@android:color/holo_red_dark"
app:lineStrokeWidth="10dp"
app:lineOuterColor="@color/defaultInnerLineColor"
app:pointsCount="6" />

<net.vrgsoft.library.Slider
android:id="@+id/slider2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:animationDuration="1000"
app:layout_constraintTop_toBottomOf="@id/slider1"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="wrap_content"/>
app:layout_constraintStart_toStartOf="parent"
app:pointInnerColor="@android:color/holo_blue_light"
app:pointOuterColor="@android:color/holo_purple"
app:pointPulseColor="@android:color/holo_blue_light"
app:lineInnerColor="@color/colorPrimaryDark"
app:lineStrokeWidth="15dp"
app:lineOuterColor="@color/bgColor"
app:pointsCount="5" />

<net.vrgsoft.library.Slider
android:id="@+id/slider3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:animationDuration="1000"
app:layout_constraintTop_toBottomOf="@id/slider2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:pointInnerColor="@android:color/holo_green_dark"
app:pointOuterColor="@android:color/holo_purple"
app:pointPulseColor="#66ff4444"
app:lineInnerColor="@color/colorAccent"
app:lineStrokeWidth="20dp"
app:lineOuterColor="@color/colorAccent"
app:pointsCount="3" />
</android.support.constraint.ConstraintLayout>
224 changes: 175 additions & 49 deletions library/src/main/java/net/vrgsoft/library/Slider.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.TypedArray;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
Expand Down Expand Up @@ -95,37 +99,20 @@ private void initAttrs(Context context, AttributeSet attrs) {
}
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}

private void initBgLine(Context context, AttributeSet attrs) {
mBgLine = new SliderBgLine(context, attrs);
mBgLine.setPointsCount(mPointsCount);
mBgLine.setId(START_INDEX - 1);
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mLineStrokeWidth);
params.topToTop = PARENT_ID;
params.bottomToBottom = PARENT_ID;
params.endToEnd = PARENT_ID;
params.startToStart = PARENT_ID;
if (mOrientation == HORIZONTAL) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = mLineStrokeWidth;
params.leftMargin = mPointSize / 2;
params.rightMargin = mPointSize / 2;
} else {
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
params.width = mLineStrokeWidth;
params.topMargin = mPointSize / 2;
params.bottomMargin = mPointSize / 2;
}
mBgLine.setLayoutParams(params);
initLineLayoutParams();
addView(mBgLine);
}

private void initPoints(Context context, AttributeSet attrs) {
for (int i = START_INDEX; i < START_INDEX + mPointsCount; i++) {
SliderPoint point = new SliderPoint(context, attrs);
point.setId(i);
mPoints.put(i, point);
}

private void initPointsLayoutParams() {
float bias = 1.0f / (mPointsCount - 1);
float currentBias = 0;

Expand All @@ -139,21 +126,8 @@ private void initPoints(Context context, AttributeSet attrs) {
params.startToStart = PARENT_ID;
params.endToEnd = PARENT_ID;
params.horizontalBias = currentBias;
// params.horizontalChainStyle = CHAIN_SPREAD_INSIDE;
// if (i == START_INDEX) {
// params.startToStart = PARENT_ID;
// params.endToStart = mPoints.get(i + 1).getId();
// } else if (i == START_INDEX + mPointsCount - 1) {
// params.startToEnd = mPoints.get(i - 1).getId();
// params.endToEnd = PARENT_ID;
// } else {
// params.startToEnd = mPoints.get(i - 1).getId();
// params.endToStart = mPoints.get(i + 1).getId();
// }

point.setLayoutParams(params);
addView(point);

currentBias += bias;
}
} else {
Expand All @@ -166,25 +140,43 @@ private void initPoints(Context context, AttributeSet attrs) {
params.startToStart = PARENT_ID;
params.endToEnd = PARENT_ID;
params.verticalBias = currentBias;
// params.verticalChainStyle = CHAIN_SPREAD_INSIDE;
// if (i == START_INDEX) {
// params.topToTop = PARENT_ID;
// params.bottomToTop = mPoints.get(i + 1).getId();
// } else if (i == START_INDEX + mPointsCount - 1) {
// params.topToBottom = mPoints.get(i - 1).getId();
// params.bottomToBottom = PARENT_ID;
// } else {
// params.topToBottom = mPoints.get(i - 1).getId();
// params.bottomToTop = mPoints.get(i + 1).getId();
// }

point.setLayoutParams(params);
addView(point);
currentBias += bias;
}
}
}

private void initLineLayoutParams() {
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mLineStrokeWidth);
params.topToTop = PARENT_ID;
params.bottomToBottom = PARENT_ID;
params.endToEnd = PARENT_ID;
params.startToStart = PARENT_ID;
if (mOrientation == HORIZONTAL) {
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = mLineStrokeWidth;
params.leftMargin = mPointSize / 2;
params.rightMargin = mPointSize / 2;
} else {
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
params.width = mLineStrokeWidth;
params.topMargin = mPointSize / 2;
params.bottomMargin = mPointSize / 2;
}
mBgLine.setLayoutParams(params);
}

private void initPoints(Context context, AttributeSet attrs) {
for (int i = START_INDEX; i < START_INDEX + mPointsCount; i++) {
SliderPoint point = new SliderPoint(context, attrs);
point.setId(i);
mPoints.put(i, point);
addView(point);
}
initPointsLayoutParams();
}

private void initClickListeners() {
for (SliderPoint point : mPoints.values()) {
point.setOnClickListener(mClickHandler);
Expand Down Expand Up @@ -327,4 +319,138 @@ private void resolveMethod(@Nullable Context context) {
+ "attribute defined on view " + mHostView.getClass() + idText);
}
}

@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.setPosition(mCurrentPosition);
return ss;
}

@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
setCurrentPosition(ss.getPosition());
super.onRestoreInstanceState(ss.getSuperState());
}

/**
* @param pointSize point width and height
*/
public void setPointSize(int pointSize) {
mPointSize = pointSize;
initPointsLayoutParams();
}

/**
* @param lineStrokeWidth guide line stroke width
*/
public void setLineStrokeWidth(int lineStrokeWidth) {
mLineStrokeWidth = lineStrokeWidth;
initLineLayoutParams();
}

/**
* @param orientation LinearLayout.HORIZONTAL or LinearLayout.VERTICAL
*/
public void setOrientation(@IntRange(from = LinearLayout.HORIZONTAL, to = LinearLayout.VERTICAL) int orientation) {
mOrientation = orientation;
mBgLine.setOrientation(orientation);
initLineLayoutParams();
initPointsLayoutParams();
}

/**
* @param duration animation duration in ms
*/
public void setDuration(long duration) {
mDuration = duration;
mBgLine.setDuration(duration);
for(SliderPoint point : mPoints.values()){
point.setDuration(duration);
}
}

/**
* @param outerLineColor color to be set
*/
public void setOuterLineColor(@ColorInt int outerLineColor) {
mBgLine.setOuterLineColor(outerLineColor);
}

/**
* @param innerLineColor color to be set
*/
public void setInnerLineColor(@ColorInt int innerLineColor) {
mBgLine.setInnerLineColor(innerLineColor);
}

/**
* @param pulseColor color to be set
*/
public void setPointPulseColor(int pulseColor) {
for(SliderPoint point : mPoints.values()){
point.setPulseColor(pulseColor);
}
}

/**
* @param outerColor color to be set
*/
public void setPointOuterColor(int outerColor) {
for(SliderPoint point : mPoints.values()){
point.setOuterColor(outerColor);
}
}

/**
* @param innerColor color to be set
*/
public void setPointInnerColor(int innerColor) {
for(SliderPoint point : mPoints.values()){
point.setInnerColor(innerColor);
}
}

private static final class SavedState extends BaseSavedState {
private int mPosition;

SavedState(Parcelable superState) {
super(superState);
}

private SavedState(Parcel in) {
super(in);
mPosition = in.readInt();
}

@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(mPosition);
}

public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
@Override
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}

@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};

public int getPosition() {
return mPosition;
}

public void setPosition(int position) {
mPosition = position;
}

}
}
Loading

0 comments on commit c861c35

Please sign in to comment.