Skip to content

Commit

Permalink
Add method setPosition() , setShowColorBar() , getIsShowColorBar()
Browse files Browse the repository at this point in the history
  • Loading branch information
rtugeek@gmail.com committed Mar 31, 2020
1 parent 1099f75 commit 43579fe
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 50 deletions.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -16,6 +16,7 @@
|barMargin|dimension|5dp|
|thumbHeight|dimension|30dp|
|showAlphaBar|boolean|false|
|showColorBar|boolean|true|
|isVertical|boolean|false|
|disabledColor|color|Color.GRAY|
|showThumb|boolean|true|
Expand All @@ -39,7 +40,7 @@ allprojects {
```
Step 2. Add the dependency
```
implementation 'com.github.rtugeek:colorseekbar:1.7.5'
implementation 'com.github.rtugeek:colorseekbar:1.7.6'
```

## Usage
Expand All @@ -59,6 +60,8 @@ 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.setPosition(10,10); // An easier way to set ColorBar and AlphaBar

colorSeekBar.setShowAlphaBar(true);
colorSeekBar.setBarHeight(5); //5dpi
colorSeekBar.setThumbHeight(30); //30dpi
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
@@ -1,6 +1,6 @@
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.rtugeek.android.colorseekbardemo"
Expand All @@ -22,5 +22,5 @@ android {
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':colorseekbar')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
}
Expand Up @@ -3,7 +3,7 @@
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
Expand Down Expand Up @@ -44,6 +44,10 @@ protected void onCreate(Bundle savedInstanceState) {
mColorSeekBar.setThumbHeight(30);
mColorSeekBar.setDisabledColor(Color.GRAY);

int colorBarPosition = sp.getInt("colorBarPosition",0);
int alphaBarPosition = sp.getInt("alphaBarPosition",0);
mColorSeekBar.setPosition(colorBarPosition,alphaBarPosition);

mColorSeekBar.setOnInitDoneListener(new ColorSeekBar.OnInitDoneListener() {
@Override
public void done() {
Expand Down Expand Up @@ -129,6 +133,13 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});

((CheckBox)findViewById(R.id.chk_show_color_bar)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mColorSeekBar.setShowColorBar(isChecked);
}
});

((SeekBar)findViewById(R.id.seek_radius)).setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Expand All @@ -146,13 +157,17 @@ public void onStopTrackingTouch(SeekBar seekBar) {

}
});

}


@Override
protected void onStop() {
super.onStop();
spe.putInt("color", mColorSeekBar.getColor());
spe.putInt("colorBarPosition", mColorSeekBar.getColorBarPosition());
spe.putInt("alphaBarPosition", mColorSeekBar.getAlphaBarPosition());
spe.putInt("color", mColorSeekBar.getColor());
spe.putBoolean("showAlpha", mColorSeekBar.isShowAlphaBar());
spe.commit();
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Expand Up @@ -63,6 +63,18 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/chk_show_color_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:checked="true"
android:text="Show Color Bar" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Expand Up @@ -4,6 +4,7 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
Expand All @@ -19,6 +20,4 @@ allprojects {
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

8 changes: 4 additions & 4 deletions colorseekbar/build.gradle
Expand Up @@ -6,8 +6,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode 7
versionName "1.7.4"
versionCode 8
versionName "1.7.6"

}
buildTypes {
Expand All @@ -22,6 +22,6 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:support-annotations:28.0.0'
testImplementation 'junit:junit:4.13'
implementation 'androidx.annotation:annotation:1.1.0'
}
Expand Up @@ -13,7 +13,9 @@
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.support.annotation.ArrayRes;

import androidx.annotation.ArrayRes;

import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -29,6 +31,7 @@ public class ColorSeekBar extends View {
private OnColorChangeListener mOnColorChangeLister;
private Context mContext;
private boolean mIsShowAlphaBar = false;
private boolean mIsShowColorBar = true;
private boolean mIsVertical;
private boolean mMovingColorBar;
private boolean mMovingAlphaBar;
Expand Down Expand Up @@ -102,8 +105,8 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSpeMode = MeasureSpec.getMode(widthMeasureSpec);
int heightSpeMode = MeasureSpec.getMode(heightMeasureSpec);

int barHeight = mIsShowAlphaBar ? mBarHeight * 2 : mBarHeight;
int thumbHeight = mIsShowAlphaBar ? mThumbHeight * 2 : mThumbHeight;
int barHeight = (mIsShowAlphaBar && mIsShowColorBar) ? mBarHeight * 2 : mBarHeight;
int thumbHeight = (mIsShowAlphaBar && mIsShowColorBar) ? mThumbHeight * 2 : mThumbHeight;

Logger.i("widthSpeMode:");
Logger.spec(widthSpeMode);
Expand Down Expand Up @@ -136,7 +139,8 @@ protected void applyStyle(Context context, AttributeSet attrs, int defStyleAttr,
mDisabledColor = a.getInteger(R.styleable.ColorSeekBar_disabledColor, Color.GRAY);
mIsVertical = a.getBoolean(R.styleable.ColorSeekBar_isVertical, false);
mIsShowAlphaBar = a.getBoolean(R.styleable.ColorSeekBar_showAlphaBar, false);
mShowThumb = a.getBoolean(R.styleable.ColorSeekBar_showAlphaBar, true);
mIsShowColorBar = a.getBoolean(R.styleable.ColorSeekBar_showColorBar, true);
mShowThumb = a.getBoolean(R.styleable.ColorSeekBar_showThumb, 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);
Expand Down Expand Up @@ -242,45 +246,54 @@ protected void onDraw(Canvas canvas) {
canvas.scale(-1, 1, getHeight() / 2, getWidth() / 2);
}

float colorPosition = (float) mColorBarPosition / mMaxPosition * mBarWidth;

colorPaint.setAntiAlias(true);
int color = isEnabled() ? getColor(false) : mDisabledColor;

int colorStartTransparent = Color.argb(mAlphaMaxPosition, Color.red(color), Color.green(color), Color.blue(color));
int colorEndTransparent = Color.argb(mAlphaMinPosition, Color.red(color), Color.green(color), Color.blue(color));
colorPaint.setColor(color);
int[] toAlpha = new int[]{colorStartTransparent, colorEndTransparent};
//clear
canvas.drawBitmap(mTransparentBitmap, 0, 0, null);

//draw color bar
canvas.drawRoundRect(mColorRect,mBarRadius,mBarRadius, isEnabled() ? mColorRectPaint : mDisabledPaint);
//draw color bar thumb
if(mShowThumb){
float thumbX = colorPosition + realLeft;
float thumbY = mColorRect.top + mColorRect.height() / 2;
canvas.drawCircle(thumbX, thumbY, mBarHeight / 2 + 5, colorPaint);

//draw color bar thumb radial gradient shader
RadialGradient thumbShader = new RadialGradient(thumbX, thumbY, mThumbRadius, toAlpha, null, Shader.TileMode.MIRROR);
thumbGradientPaint.setAntiAlias(true);
thumbGradientPaint.setShader(thumbShader);
canvas.drawCircle(thumbX, thumbY, mThumbHeight / 2, thumbGradientPaint);

if (mIsShowColorBar) {
float colorPosition = (float) mColorBarPosition / mMaxPosition * mBarWidth;
colorPaint.setAntiAlias(true);
colorPaint.setColor(color);
//clear
canvas.drawBitmap(mTransparentBitmap, 0, 0, null);

//draw color bar
canvas.drawRoundRect(mColorRect, mBarRadius, mBarRadius, isEnabled() ? mColorRectPaint : mDisabledPaint);
//draw color bar thumb
if (mShowThumb) {
float thumbX = colorPosition + realLeft;
float thumbY = mColorRect.top + mColorRect.height() / 2;
canvas.drawCircle(thumbX, thumbY, mBarHeight / 2 + 5, colorPaint);

//draw color bar thumb radial gradient shader
RadialGradient thumbShader = new RadialGradient(thumbX, thumbY, mThumbRadius, toAlpha, null, Shader.TileMode.MIRROR);
thumbGradientPaint.setAntiAlias(true);
thumbGradientPaint.setShader(thumbShader);
canvas.drawCircle(thumbX, thumbY, mThumbHeight / 2, thumbGradientPaint);
}
}


if (mIsShowAlphaBar) {
//init rect
int top = (int) (mThumbHeight + mThumbRadius + mBarHeight + mBarMargin);
mAlphaRect = new RectF(realLeft, top, realRight, top + mBarHeight);
if (mIsShowColorBar) {
int top = mIsShowColorBar ? (int) (mThumbHeight + mThumbRadius + mBarHeight + mBarMargin) :
(int) (mThumbHeight + mThumbRadius + mBarMargin);
mAlphaRect = new RectF(realLeft, top, realRight, top + mBarHeight);
} else {
mAlphaRect = new RectF(mColorRect);
}

//draw alpha bar
alphaBarPaint.setAntiAlias(true);
LinearGradient alphaBarShader = new LinearGradient(0, 0, mAlphaRect.width(), 0, toAlpha, null, Shader.TileMode.CLAMP);
alphaBarPaint.setShader(alphaBarShader);
canvas.drawRect(mAlphaRect, alphaBarPaint);

//draw alpha bar thumb
if(mShowThumb){
if (mShowThumb) {
float alphaPosition = (float) (mAlphaBarPosition - mAlphaMinPosition) / (mAlphaMaxPosition - mAlphaMinPosition) * mBarWidth;
float alphaThumbX = alphaPosition + realLeft;
float alphaThumbY = mAlphaRect.top + mAlphaRect.height() / 2;
Expand Down Expand Up @@ -318,14 +331,12 @@ public boolean onTouchEvent(MotionEvent event) {
float y = mIsVertical ? event.getX() : event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isOnBar(mColorRect, x, y)) {
if (mIsShowColorBar && isOnBar(mColorRect, x, y)) {
mMovingColorBar = true;
float value = (x - realLeft) / mBarWidth * mMaxPosition;
setColorBarPosition((int) value);
} else if (mIsShowAlphaBar) {
if (isOnBar(mAlphaRect, x, y)) {
mMovingAlphaBar = true;
}
} else if (mIsShowAlphaBar && isOnBar(mAlphaRect, x, y)) {
mMovingAlphaBar = true;
}
break;
case MotionEvent.ACTION_MOVE:
Expand Down Expand Up @@ -613,10 +624,8 @@ private void setAlphaValue() {
mAlpha = 255 - mAlphaBarPosition;
}

public void setAlphaBarPosition(int value) {
this.mAlphaBarPosition = value;
setAlphaValue();
invalidate();
public void setAlphaBarPosition(int position) {
setPosition(mColorBarPosition,position);
}

public int getMaxValue() {
Expand Down Expand Up @@ -658,15 +667,20 @@ public void setBarMarginPx(int mBarMargin) {
* @param value
*/
public void setColorBarPosition(int value) {
this.mColorBarPosition = value;
setPosition(value,mAlphaBarPosition);
}

public void setPosition(int colorBarPosition,int alphaBarPosition) {
this.mColorBarPosition = colorBarPosition;
mColorBarPosition = mColorBarPosition > mMaxPosition ? mMaxPosition : mColorBarPosition;
mColorBarPosition = mColorBarPosition < 0 ? 0 : mColorBarPosition;
this.mAlphaBarPosition = alphaBarPosition;
setAlphaValue();
invalidate();
if (mOnColorChangeLister != null) {
mOnColorChangeLister.onColorChangeListener(mColorBarPosition, mAlphaBarPosition, getColor());
}
}

public void setOnInitDoneListener(OnInitDoneListener listener) {
this.mOnInitDoneListener = listener;
}
Expand Down Expand Up @@ -760,11 +774,21 @@ public int getBarRadius() {

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

public boolean isIsShowColorBar() {
return mIsShowColorBar;
}

public void setShowColorBar(boolean isShowColorBar) {
this.mIsShowColorBar = isShowColorBar;
refreshLayoutParams();
invalidate();
}
}
1 change: 1 addition & 0 deletions colorseekbar/src/main/res/values/attrs.xml
Expand Up @@ -5,6 +5,7 @@
<attr name="barHeight" format="dimension" />
<attr name="thumbHeight" format="dimension" />
<attr name="showAlphaBar" format="boolean" />
<attr name="showColorBar" format="boolean" />
<attr name="alphaBarPosition" format="integer" />
<attr name="colorBarPosition" format="integer" />
<attr name="bgColor" format="color"/>
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Expand Up @@ -15,4 +15,6 @@
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true

0 comments on commit 43579fe

Please sign in to comment.