@ -22,12 +22,12 @@ import android.graphics.Color;
import android.graphics.ComposeShader ;
import android.graphics.ComposeShader ;
import android.graphics.LinearGradient ;
import android.graphics.LinearGradient ;
import android.graphics.Paint ;
import android.graphics.Paint ;
import android.graphics.Paint.Align ;
import android.graphics.Paint.Style ;
import android.graphics.Point ;
import android.graphics.Point ;
import android.graphics.PorterDuff ;
import android.graphics.PorterDuff ;
import android.graphics.RectF ;
import android.graphics.RectF ;
import android.graphics.Shader ;
import android.graphics.Shader ;
import android.graphics.Paint.Align ;
import android.graphics.Paint.Style ;
import android.graphics.Shader.TileMode ;
import android.graphics.Shader.TileMode ;
import android.util.AttributeSet ;
import android.util.AttributeSet ;
import android.view.MotionEvent ;
import android.view.MotionEvent ;
@ -220,7 +220,9 @@ public class ColorPickerView extends View {
@Override
@Override
protected void onDraw ( Canvas canvas ) {
protected void onDraw ( Canvas canvas ) {
if ( mDrawingRect . width ( ) < = 0 | | mDrawingRect . height ( ) < = 0 ) return ;
if ( mDrawingRect . width ( ) < = 0 | | mDrawingRect . height ( ) < = 0 ) {
return ;
}
drawSatValPanel ( canvas ) ;
drawSatValPanel ( canvas ) ;
drawHuePanel ( canvas ) ;
drawHuePanel ( canvas ) ;
@ -234,18 +236,18 @@ public class ColorPickerView extends View {
if ( BORDER_WIDTH_PX > 0 ) {
if ( BORDER_WIDTH_PX > 0 ) {
mBorderPaint . setColor ( mBorderColor ) ;
mBorderPaint . setColor ( mBorderColor ) ;
canvas . drawRect ( mDrawingRect . left , mDrawingRect . top , rect . right + BORDER_WIDTH_PX , rect . bottom + BORDER_WIDTH_PX , mBorderPaint ) ;
canvas . drawRect ( mDrawingRect . left , mDrawingRect . top , rect . right + BORDER_WIDTH_PX ,
rect . bottom + BORDER_WIDTH_PX , mBorderPaint ) ;
}
}
if ( mValShader = = null ) {
if ( mValShader = = null ) {
mValShader = new LinearGradient ( rect . left , rect . top , rect . left , rect . bottom ,
mValShader = new LinearGradient ( rect . left , rect . top , rect . left , rect . bottom , 0xffffffff , 0xff000000 ,
0xffffffff , 0xff000000 , TileMode . CLAMP ) ;
TileMode . CLAMP ) ;
}
}
int rgb = Color . HSVToColor ( new float [ ] { mHue , 1f , 1f } ) ;
int rgb = Color . HSVToColor ( new float [ ] { mHue , 1f , 1f } ) ;
mSatShader = new LinearGradient ( rect . left , rect . top , rect . right , rect . top ,
mSatShader = new LinearGradient ( rect . left , rect . top , rect . right , rect . top , 0xffffffff , rgb , TileMode . CLAMP ) ;
0xffffffff , rgb , TileMode . CLAMP ) ;
ComposeShader mShader = new ComposeShader ( mValShader , mSatShader , PorterDuff . Mode . MULTIPLY ) ;
ComposeShader mShader = new ComposeShader ( mValShader , mSatShader , PorterDuff . Mode . MULTIPLY ) ;
mSatValPaint . setShader ( mShader ) ;
mSatValPaint . setShader ( mShader ) ;
@ -267,15 +269,13 @@ public class ColorPickerView extends View {
if ( BORDER_WIDTH_PX > 0 ) {
if ( BORDER_WIDTH_PX > 0 ) {
mBorderPaint . setColor ( mBorderColor ) ;
mBorderPaint . setColor ( mBorderColor ) ;
canvas . drawRect ( rect . left - BORDER_WIDTH_PX ,
canvas . drawRect ( rect . left - BORDER_WIDTH_PX , rect . top - BORDER_WIDTH_PX , rect . right + BORDER_WIDTH_PX ,
rect . top - BORDER_WIDTH_PX ,
rect . bottom + BORDER_WIDTH_PX , mBorderPaint ) ;
rect . right + BORDER_WIDTH_PX ,
rect . bottom + BORDER_WIDTH_PX ,
mBorderPaint ) ;
}
}
if ( mHueShader = = null ) {
if ( mHueShader = = null ) {
mHueShader = new LinearGradient ( rect . left , rect . top , rect . left , rect . bottom , buildHueColorArray ( ) , null , TileMode . CLAMP ) ;
mHueShader = new LinearGradient ( rect . left , rect . top , rect . left , rect . bottom , buildHueColorArray ( ) , null ,
TileMode . CLAMP ) ;
mHuePaint . setShader ( mHueShader ) ;
mHuePaint . setShader ( mHueShader ) ;
}
}
@ -298,17 +298,16 @@ public class ColorPickerView extends View {
private void drawAlphaPanel ( Canvas canvas ) {
private void drawAlphaPanel ( Canvas canvas ) {
if ( ! mShowAlphaPanel | | mAlphaRect = = null | | mAlphaPattern = = null ) return ;
if ( ! mShowAlphaPanel | | mAlphaRect = = null | | mAlphaPattern = = null ) {
return ;
}
final RectF rect = mAlphaRect ;
final RectF rect = mAlphaRect ;
if ( BORDER_WIDTH_PX > 0 ) {
if ( BORDER_WIDTH_PX > 0 ) {
mBorderPaint . setColor ( mBorderColor ) ;
mBorderPaint . setColor ( mBorderColor ) ;
canvas . drawRect ( rect . left - BORDER_WIDTH_PX ,
canvas . drawRect ( rect . left - BORDER_WIDTH_PX , rect . top - BORDER_WIDTH_PX , rect . right + BORDER_WIDTH_PX ,
rect . top - BORDER_WIDTH_PX ,
rect . bottom + BORDER_WIDTH_PX , mBorderPaint ) ;
rect . right + BORDER_WIDTH_PX ,
rect . bottom + BORDER_WIDTH_PX ,
mBorderPaint ) ;
}
}
@ -318,8 +317,7 @@ public class ColorPickerView extends View {
int color = Color . HSVToColor ( hsv ) ;
int color = Color . HSVToColor ( hsv ) ;
int acolor = Color . HSVToColor ( 0 , hsv ) ;
int acolor = Color . HSVToColor ( 0 , hsv ) ;
mAlphaShader = new LinearGradient ( rect . left , rect . top , rect . right , rect . top ,
mAlphaShader = new LinearGradient ( rect . left , rect . top , rect . right , rect . top , color , acolor , TileMode . CLAMP ) ;
color , acolor , TileMode . CLAMP ) ;
mAlphaPaint . setShader ( mAlphaShader ) ;
mAlphaPaint . setShader ( mAlphaShader ) ;
@ -396,21 +394,17 @@ public class ColorPickerView extends View {
if ( x < rect . left ) {
if ( x < rect . left ) {
x = 0f ;
x = 0f ;
}
} else if ( x > rect . right ) {
else if ( x > rect . right ) {
x = width ;
x = width ;
}
} else {
else {
x = x - rect . left ;
x = x - rect . left ;
}
}
if ( y < rect . top ) {
if ( y < rect . top ) {
y = 0f ;
y = 0f ;
}
} else if ( y > rect . bottom ) {
else if ( y > rect . bottom ) {
y = height ;
y = height ;
}
} else {
else {
y = y - rect . top ;
y = y - rect . top ;
}
}
@ -429,11 +423,9 @@ public class ColorPickerView extends View {
if ( y < rect . top ) {
if ( y < rect . top ) {
y = 0f ;
y = 0f ;
}
} else if ( y > rect . bottom ) {
else if ( y > rect . bottom ) {
y = height ;
y = height ;
}
} else {
else {
y = y - rect . top ;
y = y - rect . top ;
}
}
@ -447,11 +439,9 @@ public class ColorPickerView extends View {
if ( x < rect . left ) {
if ( x < rect . left ) {
x = 0 ;
x = 0 ;
}
} else if ( x > rect . right ) {
else if ( x > rect . right ) {
x = width ;
x = width ;
}
} else {
else {
x = x - ( int ) rect . left ;
x = x - ( int ) rect . left ;
}
}
@ -482,15 +472,13 @@ public class ColorPickerView extends View {
if ( sat < 0f ) {
if ( sat < 0f ) {
sat = 0f ;
sat = 0f ;
}
} else if ( sat > 1f ) {
else if ( sat > 1f ) {
sat = 1f ;
sat = 1f ;
}
}
if ( val < 0f ) {
if ( val < 0f ) {
val = 0f ;
val = 0f ;
}
} else if ( val > 1f ) {
else if ( val > 1f ) {
val = 1f ;
val = 1f ;
}
}
@ -507,8 +495,7 @@ public class ColorPickerView extends View {
if ( hue < 0f ) {
if ( hue < 0f ) {
hue = 0f ;
hue = 0f ;
}
} else if ( hue > 360f ) {
else if ( hue > 360f ) {
hue = 360f ;
hue = 360f ;
}
}
@ -522,15 +509,13 @@ public class ColorPickerView extends View {
if ( ! mShowAlphaPanel | | mAlphaRect = = null ) {
if ( ! mShowAlphaPanel | | mAlphaRect = = null ) {
update = false ;
update = false ;
}
} else {
else {
int alpha = ( int ) ( mAlpha - x * 10 ) ;
int alpha = ( int ) ( mAlpha - x * 10 ) ;
if ( alpha < 0 ) {
if ( alpha < 0 ) {
alpha = 0 ;
alpha = 0 ;
}
} else if ( alpha > 0xff ) {
else if ( alpha > 0xff ) {
alpha = 0xff ;
alpha = 0xff ;
}
}
@ -608,7 +593,9 @@ public class ColorPickerView extends View {
private boolean moveTrackersIfNeeded ( MotionEvent event ) {
private boolean moveTrackersIfNeeded ( MotionEvent event ) {
if ( mStartTouchPoint = = null ) return false ;
if ( mStartTouchPoint = = null ) {
return false ;
}
boolean update = false ;
boolean update = false ;
@ -622,8 +609,7 @@ public class ColorPickerView extends View {
mHue = pointToHue ( event . getY ( ) ) ;
mHue = pointToHue ( event . getY ( ) ) ;
update = true ;
update = true ;
}
} else if ( mSatValRect . contains ( startX , startY ) ) {
else if ( mSatValRect . contains ( startX , startY ) ) {
mLastTouchedPanel = PANEL_SAT_VAL ;
mLastTouchedPanel = PANEL_SAT_VAL ;
@ -633,8 +619,7 @@ public class ColorPickerView extends View {
mVal = result [ 1 ] ;
mVal = result [ 1 ] ;
update = true ;
update = true ;
}
} else if ( mAlphaRect ! = null & & mAlphaRect . contains ( startX , startY ) ) {
else if ( mAlphaRect ! = null & & mAlphaRect . contains ( startX , startY ) ) {
mLastTouchedPanel = PANEL_ALPHA ;
mLastTouchedPanel = PANEL_ALPHA ;
@ -670,20 +655,17 @@ public class ColorPickerView extends View {
if ( height > heightAllowed | | getTag ( ) . equals ( "landscape" ) ) {
if ( height > heightAllowed | | getTag ( ) . equals ( "landscape" ) ) {
height = heightAllowed ;
height = heightAllowed ;
width = ( int ) ( height + PANEL_SPACING + HUE_PANEL_WIDTH ) ;
width = ( int ) ( height + PANEL_SPACING + HUE_PANEL_WIDTH ) ;
}
} else {
else {
width = widthAllowed ;
width = widthAllowed ;
}
}
}
} else {
else {
width = ( int ) ( heightAllowed - ALPHA_PANEL_HEIGHT + HUE_PANEL_WIDTH ) ;
width = ( int ) ( heightAllowed - ALPHA_PANEL_HEIGHT + HUE_PANEL_WIDTH ) ;
if ( width > widthAllowed ) {
if ( width > widthAllowed ) {
width = widthAllowed ;
width = widthAllowed ;
height = ( int ) ( widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT ) ;
height = ( int ) ( widthAllowed - HUE_PANEL_WIDTH + ALPHA_PANEL_HEIGHT ) ;
}
} else {
else {
height = heightAllowed ;
height = heightAllowed ;
}
}
@ -733,7 +715,6 @@ public class ColorPickerView extends View {
}
}
@Override
@Override
protected void onSizeChanged ( int w , int h , int oldw , int oldh ) {
protected void onSizeChanged ( int w , int h , int oldw , int oldh ) {
super . onSizeChanged ( w , h , oldw , oldh ) ;
super . onSizeChanged ( w , h , oldw , oldh ) ;
@ -779,7 +760,9 @@ public class ColorPickerView extends View {
private void setUpAlphaRect ( ) {
private void setUpAlphaRect ( ) {
if ( ! mShowAlphaPanel ) return ;
if ( ! mShowAlphaPanel ) {
return ;
}
final RectF dRect = mDrawingRect ;
final RectF dRect = mDrawingRect ;
@ -791,12 +774,8 @@ public class ColorPickerView extends View {
mAlphaRect = new RectF ( left , top , right , bottom ) ;
mAlphaRect = new RectF ( left , top , right , bottom ) ;
mAlphaPattern = new AlphaPatternDrawable ( ( int ) ( 5 * mDensity ) ) ;
mAlphaPattern = new AlphaPatternDrawable ( ( int ) ( 5 * mDensity ) ) ;
mAlphaPattern . setBounds (
mAlphaPattern . setBounds ( Math . round ( mAlphaRect . left ) , Math . round ( mAlphaRect . top ) , Math . round ( mAlphaRect . right ) ,
Math . round ( mAlphaRect . left ) ,
Math . round ( mAlphaRect . bottom ) ) ;
Math . round ( mAlphaRect . top ) ,
Math . round ( mAlphaRect . right ) ,
Math . round ( mAlphaRect . bottom )
) ;
}
}
@ -901,7 +880,8 @@ public class ColorPickerView extends View {
mValShader = null ;
mValShader = null ;
mSatShader = null ;
mSatShader = null ;
mHueShader = null ;
mHueShader = null ;
mAlphaShader = null ; ;
mAlphaShader = null ;
;
requestLayout ( ) ;
requestLayout ( ) ;
}
}