Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion MyApplication2/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- XXX Hardware acceleration crashes this. Fix it! -->
<application
android:allowBackup="true"
android:hardwareAccelerated="false"
android:hardwareAccelerated="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class AdvancedColorPicker extends LinearLayout {
// Conversions between HSV and RGB can be lossy, so we store both
// separately so that all of the sliders can move independently
private float[] mHSV = new float[] { 0f, 1f, 1f };
private int mColor = Color.BLACK;
// TODO: use CYAN for now, to ease debug. Revert back to black as default value later.
private int mColor = Color.CYAN;//BLACK;

private Bar mHueBar;
private Bar mSatBar;
Expand Down Expand Up @@ -94,7 +95,7 @@ public AdvancedColorPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.advanced_color_picker, this);
Color.colorToHSV(DEFAULT_COLORS.get(2), mHSV);
Color.colorToHSV(mColor, mHSV);

wheel = (Wheel) findViewById(R.id.wheel);
for (Integer i : DEFAULT_COLORS) {
Expand Down Expand Up @@ -141,13 +142,13 @@ public void onChange(int index) {
wheel.setCenter(0.5f, 1.02f);
}

mHueBar = findBar(R.id.hueSeekbar, R.id.hueLabel, HUE);
mSatBar = findBar(R.id.satSeekbar, R.id.satLabel, SAT);
mValBar = findBar(R.id.valSeekbar, R.id.valLabel, VAL);
mHueBar = findBar(context, R.id.hueSeekbar, R.id.hueLabel, HUE);
mSatBar = findBar(context, R.id.satSeekbar, R.id.satLabel, SAT);
mValBar = findBar(context, R.id.valSeekbar, R.id.valLabel, VAL);

mRedBar = findBar(R.id.redSeekbar, R.id.redLabel, RED);
mGreenBar = findBar(R.id.greenSeekbar, R.id.greenLabel, GREEN);
mBlueBar = findBar(R.id.blueSeekbar, R.id.blueLabel, BLUE);
mRedBar = findBar(context, R.id.redSeekbar, R.id.redLabel, RED);
mGreenBar = findBar(context, R.id.greenSeekbar, R.id.greenLabel, GREEN);
mBlueBar = findBar(context, R.id.blueSeekbar, R.id.blueLabel, BLUE);


TextView hsvLabel = (TextView) findViewById(R.id.hsvLabel);
Expand All @@ -172,13 +173,14 @@ public void onClick(View v) {
});

updateGradients(mHSV);
setColor(mColor);
updateLabels();
}

private Bar findBar(final int seekbarId, final int labelId, final int type) {
BorderedBox border = new BorderedBox();
private Bar findBar(Context context, final int seekbarId, final int labelId, final int type) {
BorderedBox border = new BorderedBox(context);
border.setBorderRadius(1);
border.setBorderThickness(3);
border.setBorderColor(Color.LTGRAY);
border.setShadowInset(true);
border.setShadowShader(new LinearGradient(0f, 0f, 0f, 50, new int[] {
Color.argb(100,0,0,0), Color.argb(0,0,0,0)
Expand Down Expand Up @@ -214,7 +216,7 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {
try {
CharSequence text = bar.label.getText();
val = Float.parseFloat(text.toString());
} catch (NullPointerException ex) {
} catch (NumberFormatException ex) {
Log.i(LOGTAG, "Invalid float", ex);
}

Expand Down Expand Up @@ -276,15 +278,15 @@ private void updateGradients(float[] hsv) {
grad[i] = Color.HSVToColor(new float[]{360*i/(SIZE-1), hsv[1], hsv[2]});
}

mHueBar.seekbar.setBackgroundGradientColors(grad, mHueBar.seekbar.getMeasuredWidth());
mHueBar.seekbar.setBackgroundGradientColors(grad);
mSatBar.seekbar.setGradientColors(new int[] {
Color.HSVToColor(new float[]{hsv[0], 1, hsv[2]}),
Color.HSVToColor(new float[]{hsv[0], 0, hsv[2]}),
}, mSatBar.seekbar.getMeasuredWidth());
});
mValBar.seekbar.setGradientColors(new int[] {
Color.HSVToColor(new float[]{hsv[0], hsv[1], 0}),
Color.HSVToColor(new float[]{hsv[0], hsv[1], 1}),
}, mValBar.seekbar.getMeasuredWidth());
});

int color = Color.HSVToColor(hsv);
mHueBar.seekbar.setThumbColor(color);
Expand All @@ -305,9 +307,9 @@ private void updateGradients(int color) {
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
mRedBar.seekbar.setGradientColors(new int[]{Color.rgb(0, g, b), Color.rgb(255, g, b)}, mRedBar.seekbar.getMeasuredWidth());
mGreenBar.seekbar.setGradientColors(new int[]{Color.rgb(r, 0, b), Color.rgb(r, 255, b)}, mGreenBar.seekbar.getMeasuredWidth());
mBlueBar.seekbar.setGradientColors(new int[]{Color.rgb(r, g, 0), Color.rgb(r, g, 255)}, mBlueBar.seekbar.getMeasuredWidth());
mRedBar.seekbar.setGradientColors(new int[]{Color.rgb(0, g, b), Color.rgb(255, g, b)});
mGreenBar.seekbar.setGradientColors(new int[]{Color.rgb(r, 0, b), Color.rgb(r, 255, b)});
mBlueBar.seekbar.setGradientColors(new int[]{Color.rgb(r, g, 0), Color.rgb(r, g, 255)});

mRedBar.seekbar.setThumbColor(color);
mGreenBar.seekbar.setThumbColor(color);
Expand Down Expand Up @@ -390,17 +392,26 @@ private void setRGB(int r, int g, int b) {
setColor(Color.rgb(r, g, b));
}

private void setColor(int color) {
public void setColor(int color) {
mColor = color;
Color.colorToHSV(mColor, mHSV);
updateGradients(mColor);
updateGradients(mHSV);
updateLabels();
setWheel(mColor);
}

public int getColor() {
return mColor;
}

private void setHSV(float h, float s, float v) {
mHSV[0] = h;
mHSV[1] = s;
mHSV[2] = v;
updateGradients(mHSV);
setWheel(mHSV);
mColor = Color.HSVToColor(mHSV);
updateGradients(mColor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void step() {
return;
}

float dt = (float)(now - startTime);
float dt = now - startTime;
stepAnimation(dt/DURATION);

if (Build.VERSION.SDK_INT >= 16) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.myapplication2;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
Expand All @@ -18,28 +19,27 @@ public class BorderedBox extends Drawable {
private final Paint mBorderPaint;
private final Paint mCenter;
private float mRadius = 0;
private int mBorderWidth = 0;
private PointF mShadowOffset = new PointF(0,0);
private int mWidth = -1;
private int mHeight = -1;
private boolean mShadowInset;

public BorderedBox() {
public BorderedBox(Context context) {
mShadowPaint = new Paint();
mShadowPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mShadowPaint.setColor(Color.GRAY);
mShadowPaint.setColor(context.getResources().getColor(android.R.color.background_dark));
mShadowPaint.setAntiAlias(true);

mBorderPaint = new Paint();
mBorderPaint.setStyle(Paint.Style.STROKE);
mBorderPaint.setColor(Color.WHITE);
mBorderPaint.setColor(context.getResources().getColor(android.R.color.background_light));
mBorderPaint.setAntiAlias(true);

mCenter = new Paint();
mCenter.setColor(Color.RED);
mCenter.setAntiAlias(true);
}

@Override
public void draw(Canvas canvas) {
Rect bounds = getBounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class MainActivity extends Activity {
Expand All @@ -22,16 +20,18 @@ protected void onCreate(Bundle savedInstanceState) {
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AdvancedColorPicker acp = new AdvancedColorPicker(MainActivity.this);
(new AlertDialog.Builder(MainActivity.this))
.setTitle("Pick a color")
.setView(new AdvancedColorPicker(MainActivity.this))
.setView(acp)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();;
dialog.dismiss();
}
}).create()
.show();
acp.setColor(0);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

public class SeekBar extends android.widget.SeekBar {
private BorderedBox mThumb;
private int[] mBackgroundColors;
private int[] mGradientColors;

public SeekBar(Context context) {
super(context);
Expand All @@ -27,7 +29,7 @@ public SeekBar(Context context, AttributeSet attrs, int defStyle) {
}

private void init(Context context) {
mThumb = new BorderedBox();
mThumb = new BorderedBox(context);
mThumb.setBorderThickness(5);
mThumb.setBorderRadius(1f);
mThumb.setShadowOffset(new PointF(0, 2));
Expand All @@ -36,27 +38,48 @@ private void init(Context context) {
setThumb(mThumb);
}

public void setBackgroundGradientColors(int[] colors, int width) {
setGrad(getBackground(), colors, width);
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
updateGradient();
updateBackgroundGradient();
}

public void setGradientColors(int[] colors) {
mGradientColors = colors;
updateGradient();
}

private void updateGradient() {
if (mGradientColors == null || mGradientColors.length == 0)
return;
setGradient(getProgressDrawable(), mGradientColors);
invalidate();
}

public void setBackgroundGradientColors(int[] colors) {
mBackgroundColors = colors;
updateBackgroundGradient();
}

private void updateBackgroundGradient() {
if (mBackgroundColors == null || mBackgroundColors.length == 0)
return;
setGradient(getBackground(), mBackgroundColors);
invalidate();
}

private void setGrad(Drawable d, int[] colors, int width) {
private void setGradient(Drawable d, int[] colors) {
if (d == null)
return;

LinearGradient lg = new LinearGradient(0f, 0f, width, 0f, colors, null, Shader.TileMode.CLAMP);
LinearGradient lg = new LinearGradient(0f, 0f, getWidth(), 0f, colors, null, Shader.TileMode.CLAMP);
if (d instanceof BorderedBox)
((BorderedBox)d).setShader(lg);
else if (d instanceof ShapeDrawable)
((ShapeDrawable)d).getPaint().setShader(lg);
}

public void setGradientColors(int[] colors, int width) {
setGrad(getProgressDrawable(), colors, width);
invalidate();
}

public void setThumbColor(int color) {
mThumb.setColor(color);
}
Expand Down
63 changes: 15 additions & 48 deletions MyApplication2/src/main/java/com/example/myapplication2/Slice.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package com.example.myapplication2;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
Expand All @@ -22,12 +17,6 @@
public class Slice extends View {

private static final String LOGTAG = "Slice";
private static Path mPath;
private static Paint mPaint = new Paint();
private static int mStrokeWidth = 40;
private float mAngle;
private float mRadius;
private float mOffset;

public Slice(Context context) {
super(context);
Expand All @@ -47,51 +36,29 @@ public Slice(Context context, AttributeSet attrs, int defStyle) {
@Override
public void setBackgroundColor(int color) {
Log.i(LOGTAG, "Set color " + color);
mPaint.setColor(color);
super.setBackgroundColor(color);
}

private void init(Context context) {
Drawable d = getBackground();
Log.i(LOGTAG, "Get background " + d);
int color = Color.BLACK;
if (d instanceof ColorDrawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android
// If the ColorDrawable makes use of its bounds in the draw method,
// we may not be able to get the color we want. This is not the usual
// case before Ice Cream Sandwich (4.0.1 r1).
// Yet, we change the bounds temporarily, just to be sure that we are
// successful.
ColorDrawable colorDrawable = (ColorDrawable) d;
if (d instanceof ColorDrawable && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
// http://stackoverflow.com/questions/8089054/get-the-background-color-of-a-button-in-android
// If the ColorDrawable makes use of its bounds in the draw method,
// we may not be able to get the color we want. This is not the usual
// case before Ice Cream Sandwich (4.0.1 r1).
// Yet, we change the bounds temporarily, just to be sure that we are
// successful.
ColorDrawable colorDrawable = (ColorDrawable) d;

Rect bounds = new Rect();
bounds.set(colorDrawable.getBounds()); // Save the original bounds.
colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.
Rect bounds = new Rect();
bounds.set(colorDrawable.getBounds()); // Save the original bounds.
colorDrawable.setBounds(0, 0, 1, 1); // Change the bounds.

Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
colorDrawable.draw(canvas);
color = bitmap.getPixel(0, 0);

colorDrawable.setBounds(bounds); // Restore the original bounds.
} else {
color = ((ColorDrawable) d).getColor();
}
}

mPaint.setColor(color);
}

protected void onConfigurationChanged (Configuration newConfig) {
mPath = null;
}

public void setParams(float angle, float r, float offset) {
mAngle = angle;
mRadius = r;
if (mOffset != offset) {
mOffset = offset;
Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
colorDrawable.draw(canvas);
colorDrawable.setBounds(bounds); // Restore the original bounds.
}
}
}
Loading