Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

included initial color and also solved slider causing app to crash #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
/**
* Created by Mark on 11/08/2016.
*/

public class VerticalSlideColorPicker extends View {

private Paint paint;
Expand All @@ -35,8 +34,9 @@ public class VerticalSlideColorPicker extends View {
private float borderWidth;
private int[] colors;
private boolean cacheBitmap = true;
private int selectedColor ;

public VerticalSlideColorPicker(Context context) {
public VerticalSlideColorPicker(Context context) {
super(context);
init();
}
Expand Down Expand Up @@ -64,6 +64,7 @@ public VerticalSlideColorPicker(Context context, AttributeSet attrs, int defStyl
init();
}

@SuppressLint("NewApi")
public VerticalSlideColorPicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
Expand Down Expand Up @@ -99,33 +100,57 @@ protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);

if (cacheBitmap) {

bitmap = getDrawingCache();
getPixel();
cacheBitmap = false;
invalidate();
} else {

}
else {
canvas.drawLine(colorPickerBody.left, selectorYPos, colorPickerBody.right, selectorYPos, strokePaint);
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {

float yPos = Math.min(event.getY(), colorPickerBody.bottom);
yPos = Math.max(colorPickerBody.top, yPos);

selectorYPos = yPos;
int selectedColor = bitmap.getPixel(viewWidth/2, (int) selectorYPos);
try
{
float yPos = Math.min(event.getY(), colorPickerBody.bottom);
yPos = Math.max(colorPickerBody.top, yPos);

if (onColorChangeListener != null) {
onColorChangeListener.onColorChange(selectedColor);
}
selectorYPos = yPos;
if(!bitmap.isRecycled())
{
getPixel();
}
else
{
bitmap = getDrawingCache();
getPixel();

invalidate();
}

}
catch(Exception e)
{
e.printStackTrace();
}
return true;
}

@Override
private void getPixel() {
selectedColor = bitmap.getPixel(viewWidth/2, (int) selectorYPos);
if (onColorChangeListener != null) {
onColorChangeListener.onColorChange(selectedColor);
}

invalidate();
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

Expand Down Expand Up @@ -173,8 +198,17 @@ public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener
this.onColorChangeListener = onColorChangeListener;
}

public int intialColor()
{
return selectedColor;
}




public interface OnColorChangeListener {

void onColorChange(int selectedColor);

}
}