diff --git a/README.md b/README.md index 0db46df..3ac4b28 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ |isVertical|boolean|false| |disabledColor|color|Color.GRAY| |showThumb|boolean|true| +|barRadius|dimension|0px| @@ -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 - + ``` 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 - + ``` ![](https://github.com/rtugeek/ColorSeekBar/blob/master/screenshot/vertical.png) diff --git a/app/src/main/java/com/rtugeek/android/colorseekbardemo/MainActivity.java b/app/src/main/java/com/rtugeek/android/colorseekbardemo/MainActivity.java index c7a553e..fb2fb09 100644 --- a/app/src/main/java/com/rtugeek/android/colorseekbardemo/MainActivity.java +++ b/app/src/main/java/com/rtugeek/android/colorseekbardemo/MainActivity.java @@ -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) { + + } + }); } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a091f98..a40d3a9 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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" /> + + + + + + + + - + diff --git a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java index 48e1bbb..6a798aa 100644 --- a/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java +++ b/colorseekbar/src/main/java/com/rtugeek/android/colorseekbar/ColorSeekBar.java @@ -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; @@ -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; @@ -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 mCachedColors = new ArrayList<>(); private int mColorsToInvoke = -1; private boolean mInit = false; @@ -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(); @@ -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); @@ -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; @@ -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); @@ -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 { @@ -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(); + } + } diff --git a/colorseekbar/src/main/res/values/attrs.xml b/colorseekbar/src/main/res/values/attrs.xml index 99df90d..163751e 100644 --- a/colorseekbar/src/main/res/values/attrs.xml +++ b/colorseekbar/src/main/res/values/attrs.xml @@ -8,6 +8,7 @@ +