Skip to content

Commit

Permalink
Fixed #21
Browse files Browse the repository at this point in the history
  • Loading branch information
rtugeek@gmail.com committed Jun 30, 2019
1 parent 4f67e33 commit 76a117c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 44 deletions.
70 changes: 35 additions & 35 deletions README.md
Expand Up @@ -19,6 +19,7 @@
|isVertical|boolean|false|
|disabledColor|color|Color.GRAY|
|showThumb|boolean|true|
|barRadius|dimension|0px|



Expand All @@ -29,61 +30,60 @@

Step 1. Add the JitPack repository in your root build.gradle at the end of repositories:
```
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
```
Step 2. Add the dependency
```
 implementation 'com.github.rtugeek:colorseekbar:1.7.4'
implementation 'com.github.rtugeek:colorseekbar:1.7.5'
```

## Usage

XML
```xml
<com.rtugeek.android.colorseekbar.ColorSeekBar
android:id="@+id/colorSlider"
android:layout_width="match_parent"
app:colorSeeds="@array/material_colors"
android:layout_height="wrap_content" />
<com.rtugeek.android.colorseekbar.ColorSeekBar
android:id="@+id/colorSlider"
android:layout_width="match_parent"
app:colorSeeds="@array/material_colors"
android:layout_height="wrap_content" />
```

JAVA
```java
 colorSeekBar.setMaxPosition(100);
colorSeekBar.setColorSeeds(R.array.material_colors); // material_colors is defalut included in res/color,just use it.
colorSeekBar.setColorBarPosition(10); //0 - maxValue
colorSeekBar.setAlphaBarPosition(10); //0 - 255
colorSeekBar.setShowAlphaBar(true);
colorSeekBar.setBarHeight(5); //5dpi
colorSeekBar.setThumbHeight(30); //30dpi
colorSeekBar.setBarMargin(10); //set the margin between colorBar and alphaBar 10dpi
colorSeekBar.setMaxPosition(100);
colorSeekBar.setColorSeeds(R.array.material_colors); // material_colors is defalut included in res/color,just use it.
colorSeekBar.setColorBarPosition(10); //0 - maxValue
colorSeekBar.setAlphaBarPosition(10); //0 - 255
colorSeekBar.setShowAlphaBar(true);
colorSeekBar.setBarHeight(5); //5dpi
colorSeekBar.setThumbHeight(30); //30dpi
colorSeekBar.setBarMargin(10); //set the margin between colorBar and alphaBar 10dpi
```

Listener
```java

colorSeekBar.setOnColorChangeListener(new ColorSeekBar.OnColorChangeListener() {
@Override
public void onColorChangeListener(int colorBarPosition, int alphaBarPosition, int color) {
textView.setTextColor(color);
//colorSeekBar.getAlphaValue();
}
});
colorSeekBar.setOnColorChangeListener(new ColorSeekBar.OnColorChangeListener() {
@Override
public void onColorChangeListener(int colorBarPosition, int alphaBarPosition, int color) {
textView.setTextColor(color);
//colorSeekBar.getAlphaValue();
}
});
```

## Vertical Bar [BETA]
## Vertical Bar
```xml
<com.rtugeek.android.colorseekbar.ColorSeekBar
android:id="@+id/colorSlider"
android:layout_width="match_parent"
app:colorSeeds="@array/material_colors"
app:isVertical="true"
android:layout_height="wrap_content" />
<com.rtugeek.android.colorseekbar.ColorSeekBar
android:id="@+id/colorSlider"
android:layout_width="match_parent"
app:colorSeeds="@array/material_colors"
app:isVertical="true"
android:layout_height="wrap_content" />
```
![](https://github.com/rtugeek/ColorSeekBar/blob/master/screenshot/vertical.png)

Expand Down
Expand Up @@ -128,6 +128,24 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mColorSeekBar.setShowThumb(isChecked);
}
});

((SeekBar)findViewById(R.id.seek_radius)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mColorSeekBar.setBarRadius(progress);
((TextView) findViewById(R.id.tv_radius)).setText(String.format("Bar Radius:%dpx",progress));
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
}


Expand Down
29 changes: 26 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -73,7 +73,7 @@
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="barHeight"
android:text="Bar Height"
android:textAppearance="?android:attr/textAppearanceMedium" />

<SeekBar
Expand All @@ -86,6 +86,29 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical">

<TextView
android:id="@+id/tv_radius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bar Radius"
android:textAppearance="?android:attr/textAppearanceMedium" />

<SeekBar
android:id="@+id/seek_radius"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox"
android:layout_centerHorizontal="true"
android:max="50" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -96,7 +119,7 @@
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="thumbHeight"
android:text="Thumb Height"
android:textAppearance="?android:attr/textAppearanceMedium" />

<SeekBar
Expand All @@ -106,8 +129,8 @@
android:layout_below="@+id/checkBox"
android:layout_centerHorizontal="true" />


</LinearLayout>


</LinearLayout>

Expand Up @@ -10,6 +10,7 @@
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.support.annotation.ArrayRes;
Expand All @@ -32,7 +33,7 @@ public class ColorSeekBar extends View {
private boolean mMovingColorBar;
private boolean mMovingAlphaBar;
private Bitmap mTransparentBitmap;
private Rect mColorRect;
private RectF mColorRect;
private int mThumbHeight = 20;
private float mThumbRadius;
private int mBarHeight = 2;
Expand All @@ -41,13 +42,14 @@ public class ColorSeekBar extends View {
private int realRight;
private int mBarWidth;
private int mMaxPosition;
private Rect mAlphaRect;
private RectF mAlphaRect;
private int mColorBarPosition;
private int mAlphaBarPosition;
private int mDisabledColor;
private int mBarMargin = 5;
private int mAlphaMinPosition = 0;
private int mAlphaMaxPosition = 255;
private int mBarRadius;
private List<Integer> mCachedColors = new ArrayList<>();
private int mColorsToInvoke = -1;
private boolean mInit = false;
Expand Down Expand Up @@ -137,6 +139,7 @@ protected void applyStyle(Context context, AttributeSet attrs, int defStyleAttr,
mShowThumb = a.getBoolean(R.styleable.ColorSeekBar_showAlphaBar, true);
int backgroundColor = a.getColor(R.styleable.ColorSeekBar_bgColor, Color.TRANSPARENT);
mBarHeight = (int) a.getDimension(R.styleable.ColorSeekBar_barHeight, (float) dp2px(2));
mBarRadius = (int) a.getDimension(R.styleable.ColorSeekBar_barRadius, 0);
mThumbHeight = (int) a.getDimension(R.styleable.ColorSeekBar_thumbHeight, (float) dp2px(30));
mBarMargin = (int) a.getDimension(R.styleable.ColorSeekBar_barMargin, (float) dp2px(5));
a.recycle();
Expand Down Expand Up @@ -189,7 +192,7 @@ private void init() {
mBarWidth = realRight - realLeft;

//init rect
mColorRect = new Rect(realLeft, realTop, realRight, realTop + mBarHeight);
mColorRect = new RectF(realLeft, realTop, realRight, realTop + mBarHeight);

//init paint
LinearGradient mColorGradient = new LinearGradient(0, 0, mColorRect.width(), 0, mColorSeeds, null, Shader.TileMode.CLAMP);
Expand Down Expand Up @@ -251,7 +254,7 @@ protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mTransparentBitmap, 0, 0, null);

//draw color bar
canvas.drawRect(mColorRect, isEnabled() ? mColorRectPaint : mDisabledPaint);
canvas.drawRoundRect(mColorRect,mBarRadius,mBarRadius, isEnabled() ? mColorRectPaint : mDisabledPaint);
//draw color bar thumb
if(mShowThumb){
float thumbX = colorPosition + realLeft;
Expand All @@ -269,7 +272,7 @@ protected void onDraw(Canvas canvas) {
if (mIsShowAlphaBar) {
//init rect
int top = (int) (mThumbHeight + mThumbRadius + mBarHeight + mBarMargin);
mAlphaRect = new Rect(realLeft, top, realRight, top + mBarHeight);
mAlphaRect = new RectF(realLeft, top, realRight, top + mBarHeight);
//draw alpha bar
alphaBarPaint.setAntiAlias(true);
LinearGradient alphaBarShader = new LinearGradient(0, 0, mAlphaRect.width(), 0, toAlpha, null, Shader.TileMode.CLAMP);
Expand Down Expand Up @@ -411,7 +414,7 @@ public int getAlphaMinPosition() {
* @param y
* @return whether MotionEvent is performing on bar or not
*/
private boolean isOnBar(Rect r, float x, float y) {
private boolean isOnBar(RectF r, float x, float y) {
if (r.left - mThumbRadius < x && x < r.right + mThumbRadius && r.top - mThumbRadius < y && y < r.bottom + mThumbRadius) {
return true;
} else {
Expand Down Expand Up @@ -750,4 +753,18 @@ public void setShowThumb(boolean showThumb) {
this.mShowThumb = showThumb;
invalidate();
}

public int getBarRadius() {
return mBarRadius;
}

/**
* Set bar radius with px unit
* @param barRadiusInPx
*/
public void setBarRadius(int barRadiusInPx) {
this.mBarRadius = barRadiusInPx;
invalidate();
}

}
1 change: 1 addition & 0 deletions colorseekbar/src/main/res/values/attrs.xml
Expand Up @@ -8,6 +8,7 @@
<attr name="alphaBarPosition" format="integer" />
<attr name="colorBarPosition" format="integer" />
<attr name="bgColor" format="color"/>
<attr name="barRadius" format="dimension"/>
<attr name="barMargin" format="dimension"/>
<attr name="maxPosition" format="integer"/>
<attr name="isVertical" format="boolean"/>
Expand Down

0 comments on commit 76a117c

Please sign in to comment.