I’m trying to make a color picker that allows the users to pick color by simply having a touch input. My hueShade gives the hue to the color circle, while the saturationShade gives
me the whiting out of the center. The SweepGradient does not need a radius, but the Radial Gradient does require it. However, I am calling this at the time of the creation when it can’t get the actual values of the width and height. Instead it picks up the values stated. Is there an intermediate step where the radius can be set when that information is available? Or should I try a different approach?
private Paint mPaint;
private int[] mColors;
private int viewWidth = 120;
private int viewHeight = 120;
private int centerX = 60;
private int centerY = 60;
private int padding = 10;
public ColorWheelView(Context context) {
super(context);
onInitialize();
// TODO Auto-generated constructor stub
}
private void onInitialize()
{
mColors = new int[] {
0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000
};
Shader totalShade = new SweepGradient(0, 0, mColors, null);
Shader hueShade = new SweepGradient(0, 0, mColors, null);
Shader saturationShade = new RadialGradient(0, 0, (float)viewWidth-padding, 0xFFFFFFFF, 0x00FFFFFF, Shader.TileMode.CLAMP);
//Shader valueShade;
totalShade = new ComposeShader(hueShade, saturationShade, PorterDuff.Mode.SCREEN);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setShader(totalShade);
mPaint.setStyle(Paint.Style.FILL);
}
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
viewWidth = xNew;
viewHeight = yNew;
centerX = viewWidth / 2;
centerY = viewHeight / 2;
}
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
{
super.onLayout(changed, left, top, right, bottom);
viewWidth = right - left;
viewHeight = bottom - top;
centerX = viewWidth / 2;
centerY = viewHeight / 2;
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredWidth, measuredHeight);
}
If it helps to figure out what I am achieving, I’m trying to make this view available to use for different size screens. That’s why I need the information from the device.
It is best to call this in onLayout since it’s typically guaranteed when any change in size or orientation is performed. I wasn’t getting desired values previously, but that was because I inherited from a base class other than the Android.view.View class.