下面列出了怎么用android.graphics.Shader.TileMode的API类实例代码及写法,或者点击链接到github查看源代码。
private Bitmap createColorWheelBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
int colorCount = 12;
int colorAngleStep = 360 / 12;
int colors[] = new int[colorCount + 1];
float hsv[] = new float[] { 0f, 1f, 1f };
for (int i = 0; i < colors.length; i++) {
hsv[0] = (i * colorAngleStep + 180) % 360;
colors[i] = Color.HSVToColor(hsv);
}
colors[colorCount] = colors[0];
SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);
RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER);
colorWheelPaint.setShader(composeShader);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);
return bitmap;
}
@Override
public void draw(Canvas canvas) {
RectF rect = overlay.translateRect(object.getBoundingBox());
// Draws the dark background scrim and leaves the object area clear.
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), scrimPaint);
canvas.drawRoundRect(rect, boxCornerRadius, boxCornerRadius, eraserPaint);
// Draws the bounding box with a gradient border color at vertical.
boxPaint.setShader(
confirmationController.isConfirmed()
? null
: new LinearGradient(
rect.left,
rect.top,
rect.left,
rect.bottom,
boxGradientStartColor,
boxGradientEndColor,
TileMode.CLAMP));
canvas.drawRoundRect(rect, boxCornerRadius, boxCornerRadius, boxPaint);
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
if (this.paint == null) {
this.paint = new Paint();
this.luar = new LinearGradient(0.f, 0.f, 0.f, this.ukuranUiPx, 0xffffffff, 0xff000000,
TileMode.CLAMP);
}
this.tmp00[1] = this.tmp00[2] = 1.f;
this.tmp00[0] = this.hue;
int rgb = Color.HSVToColor(this.tmp00);
this.dalam = new LinearGradient(0.f, 0.f, this.ukuranUiPx, 0.f, 0xffffffff, rgb,
TileMode.CLAMP);
ComposeShader shader = new ComposeShader(this.luar, this.dalam, PorterDuff.Mode.MULTIPLY);
this.paint.setShader(shader);
canvas.drawRect(0.f, 0.f, this.ukuranUiPx, this.ukuranUiPx, this.paint);
}
private Bitmap createColorWheelBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
int colorCount = 12;
int colorAngleStep = 360 / 12;
int colors[] = new int[colorCount + 1];
float hsv[] = { 0f, 1f, 1f };
for (int i = 0; i < colors.length; i++) {
hsv[0] = (i * colorAngleStep + 180) % 360;
colors[i] = Color.HSVToColor(hsv);
}
colors[colorCount] = colors[0];
SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);
RadialGradient radialGradient =
new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER);
colorWheelPaint.setShader(composeShader);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);
return bitmap;
}
public void buildCircularRevealCache() {
if (STRATEGY == BITMAP_SHADER) {
buildingCircularRevealCache = true;
hasCircularRevealCache = false;
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap == null && view.getWidth() != 0 && view.getHeight() != 0) {
bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
}
if (bitmap != null) {
revealPaint.setShader(new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP));
}
buildingCircularRevealCache = false;
hasCircularRevealCache = true;
}
}
private Bitmap createColorWheelBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
int colorCount = 12;
int colorAngleStep = 360 / 12;
int colors[] = new int[colorCount + 1];
float hsv[] = new float[] { 0f, 1f, 1f };
for (int i = 0; i < colors.length; i++) {
hsv[0] = (i * colorAngleStep + 180) % 360;
colors[i] = Color.HSVToColor(hsv);
}
colors[colorCount] = colors[0];
SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);
RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER);
colorWheelPaint.setShader(composeShader);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);
return bitmap;
}
/**
* Set the bitmap reflection
*
* @param bitmap
* @return
*/
public static Bitmap createReflectedImage(Bitmap bitmap, int reflectHeight) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
if (height <= reflectHeight) {
return null;
}
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height
- reflectHeight, width, reflectHeight, matrix, true);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width, reflectHeight,
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(reflectionImage, 0, 0, null);
LinearGradient shader = new LinearGradient(0, 0, 0,
bitmapWithReflection.getHeight(), 0x80ffffff, 0x00ffffff,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, 0, width, bitmapWithReflection.getHeight(), paint);
return bitmapWithReflection;
}
/**
* 初始化BitmapShader
*/
private void setUpShader() {
Drawable drawable = getDrawable();
Bitmap bmp = drawableToBitamp(drawable);
if (drawable == null || bmp == null) {
return;
}
// 将bmp作为着色器,就是在指定区域内绘制bmp
mBitmapShader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
float scale = 1.0f;
// 拿到bitmap宽或高的小值
int bSize = Math.min(bmp.getWidth(), bmp.getHeight());
scale = mWidth * 1.0f / bSize;
// shader的变换矩阵,我们这里主要用于放大或者缩小
mMatrix.setScale(scale, scale);
// 设置变换矩阵
mBitmapShader.setLocalMatrix(mMatrix);
// 设置shader
mBitmapPaint.setShader(mBitmapShader);
}
/**
* 初始化BitmapShader
*/
private void setUpShader() {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
Bitmap bmp = drawableToBitamp(drawable);
// 将bmp作为着色器,就是在指定区域内绘制bmp
mBitmapShader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
float scale = 1.0f;
// 拿到bitmap宽或高的小值
int bSize = Math.min(bmp.getWidth(), bmp.getHeight());
scale = mWidth * 1.0f / bSize;
// shader的变换矩阵,我们这里主要用于放大或者缩小
mMatrix.setScale(scale, scale);
// 设置变换矩阵
mBitmapShader.setLocalMatrix(mMatrix);
// 设置shader
mBitmapPaint.setShader(mBitmapShader);
}
private Bitmap createColorWheelBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
int colorCount = 12;
int colorAngleStep = 360 / 12;
int colors[] = new int[colorCount + 1];
float hsv[] = new float[]{0f, 1f, 1f};
for (int i = 0; i < colors.length; i++) {
hsv[0] = (i * colorAngleStep + 180) % 360;
colors[i] = Color.HSVToColor(hsv);
}
colors[colorCount] = colors[0];
SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);
RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER);
colorWheelPaint.setShader(composeShader);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);
return bitmap;
}
public LinearGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final LinearGradientDirection pLinearGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) {
super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions);
this.mColors = pColors;
this.mPositions = pPositions;
this.mLinearGradientDirection = pLinearGradientDirection;
this.mPaint.setStyle(Style.FILL);
final int right = pBitmapTextureAtlasSource.getTextureWidth() - 1;
final int bottom = pBitmapTextureAtlasSource.getTextureHeight() - 1;
final float fromX = pLinearGradientDirection.getFromX(right);
final float fromY = pLinearGradientDirection.getFromY(bottom);
final float toX = pLinearGradientDirection.getToX(right);
final float toY = pLinearGradientDirection.getToY(bottom);
this.mPaint.setShader(new LinearGradient(fromX, fromY, toX, toY, pColors, pPositions, TileMode.CLAMP));
}
public RadialGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final RadialGradientDirection pRadialGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) {
super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions);
this.mColors = pColors;
this.mPositions = pPositions;
this.mRadialGradientDirection = pRadialGradientDirection;
this.mPaint.setStyle(Style.FILL);
final int width = pBitmapTextureAtlasSource.getTextureWidth();
final int height = pBitmapTextureAtlasSource.getTextureHeight();
final float centerX = width * 0.5f;
final float centerY = height * 0.5f;
final float radius = Math.max(centerX, centerY);
switch (pRadialGradientDirection) {
case INSIDE_OUT:
this.mPaint.setShader(new RadialGradient(centerX, centerY, radius, pColors, pPositions, TileMode.CLAMP));
break;
case OUTSIDE_IN:
ArrayUtils.reverse(pColors);
this.mPaint.setShader(new RadialGradient(centerX, centerY, radius, pColors, pPositions, TileMode.CLAMP));
break;
}
}
private Bitmap createColorWheelBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
int colorCount = 12;
int colorAngleStep = 360 / 12;
int colors[] = new int[colorCount + 1];
float hsv[] = new float[]{0f, 1f, 1f};
for (int i = 0; i < colors.length; i++) {
hsv[0] = (i * colorAngleStep + 180) % 360;
colors[i] = Color.HSVToColor(hsv);
}
colors[colorCount] = colors[0];
SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);
RadialGradient radialGradient = new RadialGradient(width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
ComposeShader composeShader = new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER);
colorWheelPaint.setShader(composeShader);
Canvas canvas = new Canvas(bitmap);
canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);
return bitmap;
}
public LinearGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final LinearGradientDirection pLinearGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) {
super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions);
this.mColors = pColors;
this.mPositions = pPositions;
this.mLinearGradientDirection = pLinearGradientDirection;
this.mPaint.setStyle(Style.FILL);
final int right = pBitmapTextureAtlasSource.getTextureWidth() - 1;
final int bottom = pBitmapTextureAtlasSource.getTextureHeight() - 1;
final float fromX = pLinearGradientDirection.getFromX(right);
final float fromY = pLinearGradientDirection.getFromY(bottom);
final float toX = pLinearGradientDirection.getToX(right);
final float toY = pLinearGradientDirection.getToY(bottom);
this.mPaint.setShader(new LinearGradient(fromX, fromY, toX, toY, pColors, pPositions, TileMode.CLAMP));
}
public RadialGradientFillBitmapTextureAtlasSourceDecorator(final IBitmapTextureAtlasSource pBitmapTextureAtlasSource, final IBitmapTextureAtlasSourceDecoratorShape pBitmapTextureAtlasSourceDecoratorShape, final int[] pColors, final float[] pPositions, final RadialGradientDirection pRadialGradientDirection, final TextureAtlasSourceDecoratorOptions pTextureAtlasSourceDecoratorOptions) {
super(pBitmapTextureAtlasSource, pBitmapTextureAtlasSourceDecoratorShape, pTextureAtlasSourceDecoratorOptions);
this.mColors = pColors;
this.mPositions = pPositions;
this.mRadialGradientDirection = pRadialGradientDirection;
this.mPaint.setStyle(Style.FILL);
final int width = pBitmapTextureAtlasSource.getTextureWidth();
final int height = pBitmapTextureAtlasSource.getTextureHeight();
final float centerX = width * 0.5f;
final float centerY = height * 0.5f;
final float radius = Math.max(centerX, centerY);
switch(pRadialGradientDirection) {
case INSIDE_OUT:
this.mPaint.setShader(new RadialGradient(centerX, centerY, radius, pColors, pPositions, TileMode.CLAMP));
break;
case OUTSIDE_IN:
ArrayUtils.reverse(pColors);
this.mPaint.setShader(new RadialGradient(centerX, centerY, radius, pColors, pPositions, TileMode.CLAMP));
break;
}
}
private void setup() {
init();
Bitmap bitmap = isCircle ? getBitmapFromDrawable(getDrawable()) : null;
if (bitmap != null) {
BitmapShader bitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
tmpMatrix.set(getImageMatrix());
tmpMatrix.postTranslate(getPaddingLeft(), getPaddingTop());
bitmapShader.setLocalMatrix(tmpMatrix);
bitmapPaint.setShader(bitmapShader);
} else {
bitmapPaint.setShader(null);
}
invalidate();
}
private LinearGradient getGradient(PointF point1, PointF point2) {
adjustGradientColors();
int[] colors = new int[this.colors.size()];
for (int i = 0; i < colors.length; i++) {
colors[i] = this.colors.get(i);
}
float[] positions = null;
if (!offsets.isEmpty()) {
positions = new float[this.offsets.size()];
for (int i = 0; i < positions.length; i++) {
positions[i] = this.offsets.get(i);
}
}
try {
LinearGradient gradient = new LinearGradient(point1.x, point1.y, point2.x, point2.y,
colors, positions, TileMode.CLAMP);
return gradient;
} catch (Exception e) {
if (PXLog.isLogging()) {
PXLog.e(TAG, e, "Error while instantiating a LinearGradient");
}
return null;
}
}
/**
* Setup the background paint objects so that the background & selected background drawable can be rendered with
* rounded corners using a bitmap shader
*
* This function is called by setupBackgroundClipPath since the background clip determines whether the button has
* rounded corners. In addition, this function should be called when the drawable or selected drawable changes,
* the selected button radius changes, or the size of either drawable changes.
*/
void setupBackgroundBitmaps()
{
Bitmap bitmap;
// Setup background paint object to render background using a bitmap shader approach under three conditions:
// 1. Background has rounded corners
// 2. There is a background drawable
// 3. Able to successfully create bitmap from drawable
if (backgroundClipPath != null && backgroundDrawable != null
&& (bitmap = getBitmapFromDrawable(backgroundDrawable)) != null)
{
final BitmapShader backgroundBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
backgroundPaint.setShader(backgroundBitmapShader);
}
else
backgroundPaint = null;
// Setup selected background paint object to render background using a bitmap shader approach under three
// conditions:
// 1. Background has rounded corners OR selected button has rounded corners
// 2. There is a background drawable
// 3. Able to successfully create bitmap from drawable
if ((backgroundClipPath != null || selectedButtonRadius > 0) && selectedBackgroundDrawable != null
&& (bitmap = getBitmapFromDrawable(selectedBackgroundDrawable)) != null)
{
final BitmapShader selectedBackgroundBitmapShader = new BitmapShader(bitmap, TileMode.CLAMP,
TileMode.CLAMP);
selectedBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
selectedBackgroundPaint.setShader(selectedBackgroundBitmapShader);
}
else
selectedBackgroundPaint = null;
}
ObjectGraphicInMultiMode(
GraphicOverlay overlay,
DetectedObject object,
ObjectConfirmationController confirmationController) {
super(overlay);
this.object = object;
this.confirmationController = confirmationController;
Resources resources = context.getResources();
boxPaint = new Paint();
boxPaint.setStyle(Style.STROKE);
boxPaint.setStrokeWidth(
resources.getDimensionPixelOffset(
confirmationController.isConfirmed()
? R.dimen.bounding_box_confirmed_stroke_width
: R.dimen.bounding_box_stroke_width));
boxPaint.setColor(Color.WHITE);
boxGradientStartColor = ContextCompat.getColor(context, R.color.bounding_box_gradient_start);
boxGradientEndColor = ContextCompat.getColor(context, R.color.bounding_box_gradient_end);
boxCornerRadius = resources.getDimensionPixelOffset(R.dimen.bounding_box_corner_radius);
scrimPaint = new Paint();
scrimPaint.setShader(
new LinearGradient(
0,
0,
overlay.getWidth(),
overlay.getHeight(),
ContextCompat.getColor(context, R.color.object_confirmed_bg_gradient_start),
ContextCompat.getColor(context, R.color.object_confirmed_bg_gradient_end),
TileMode.MIRROR));
eraserPaint = new Paint();
eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
minBoxLen =
resources.getDimensionPixelOffset(R.dimen.object_reticle_outer_ring_stroke_radius) * 2;
}
private Drawable createInnerStrokesDrawable(final int color, float strokeWidth) {
if (!mStrokeVisible) {
return new ColorDrawable(Color.TRANSPARENT);
}
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
final int bottomStrokeColor = darkenColor(color);
final int bottomStrokeColorHalfTransparent = halfTransparent(bottomStrokeColor);
final int topStrokeColor = lightenColor(color);
final int topStrokeColorHalfTransparent = halfTransparent(topStrokeColor);
final Paint paint = shapeDrawable.getPaint();
paint.setAntiAlias(true);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Style.STROKE);
shapeDrawable.setShaderFactory(new ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(width / 2, 0, width / 2, height,
new int[]{topStrokeColor, topStrokeColorHalfTransparent, color, bottomStrokeColorHalfTransparent, bottomStrokeColor},
new float[]{0f, 0.2f, 0.5f, 0.8f, 1f},
TileMode.CLAMP
);
}
});
return shapeDrawable;
}
private Drawable createInnerStrokesDrawable(final int color, float strokeWidth) {
if (!mStrokeVisible) {
return new ColorDrawable(Color.TRANSPARENT);
}
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
final int bottomStrokeColor = darkenColor(color);
final int bottomStrokeColorHalfTransparent = halfTransparent(bottomStrokeColor);
final int topStrokeColor = lightenColor(color);
final int topStrokeColorHalfTransparent = halfTransparent(topStrokeColor);
final Paint paint = shapeDrawable.getPaint();
paint.setAntiAlias(true);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Style.STROKE);
shapeDrawable.setShaderFactory(new ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(width / 2, 0, width / 2, height,
new int[]{topStrokeColor, topStrokeColorHalfTransparent, color, bottomStrokeColorHalfTransparent, bottomStrokeColor},
new float[]{0f, 0.2f, 0.5f, 0.8f, 1f},
TileMode.CLAMP
);
}
});
return shapeDrawable;
}
/**
* 获得带倒影的Bitmap
*
* @param bitmap 源Bitmap
* @return 带倒影的Bitmap
*/
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
RadialGradient vignette = new RadialGradient(this.mRect.centerX(), (this.mRect.centerY() * 1.0f) / 0.7f, this.mRect.centerX() * 1.3f, new int[]{0, 0, 2130706432}, new float[]{0.0f, 0.7f, 1.0f}, TileMode.CLAMP);
Matrix oval = new Matrix();
oval.setScale(1.0f, 0.7f);
vignette.setLocalMatrix(oval);
this.paint.setShader(new ComposeShader(this.bitmapShader, vignette, Mode.SRC_OVER));
}
public void draw(Canvas canvas) {
if (this.mRebuildShader) {
BitmapShader bitmapShader = new BitmapShader(this.mBitmap, this.mTileModeX, this.mTileModeY);
if (this.mTileModeX == TileMode.CLAMP && this.mTileModeY == TileMode.CLAMP) {
bitmapShader.setLocalMatrix(this.mShaderMatrix);
}
this.mBitmapPaint.setShader(bitmapShader);
this.mRebuildShader = false;
}
if (this.mOval) {
if (this.mBorderWidth > 0.0f) {
canvas.drawOval(this.mDrawableRect, this.mBitmapPaint);
canvas.drawOval(this.mBorderRect, this.mBorderPaint);
return;
}
canvas.drawOval(this.mDrawableRect, this.mBitmapPaint);
} else if (any(this.mCornersRounded)) {
float radius = this.mCornerRadius;
if (this.mBorderWidth > 0.0f) {
canvas.drawRoundRect(this.mDrawableRect, radius, radius, this.mBitmapPaint);
canvas.drawRoundRect(this.mBorderRect, radius, radius, this.mBorderPaint);
redrawBitmapForSquareCorners(canvas);
redrawBorderForSquareCorners(canvas);
return;
}
canvas.drawRoundRect(this.mDrawableRect, radius, radius, this.mBitmapPaint);
redrawBitmapForSquareCorners(canvas);
} else {
canvas.drawRect(this.mDrawableRect, this.mBitmapPaint);
if (this.mBorderWidth > 0.0f) {
canvas.drawRect(this.mBorderRect, this.mBorderPaint);
}
}
}
public RoundedPagerDrawable setTileModeX(TileMode tileModeX) {
if (this.mTileModeX != tileModeX) {
this.mTileModeX = tileModeX;
this.mRebuildShader = true;
invalidateSelf();
}
return this;
}
public RoundedPagerDrawable setTileModeY(TileMode tileModeY) {
if (this.mTileModeY != tileModeY) {
this.mTileModeY = tileModeY;
this.mRebuildShader = true;
invalidateSelf();
}
return this;
}
private static TileMode parseTileMode(int tileMode) {
switch (tileMode) {
case 0:
return TileMode.CLAMP;
case 1:
return TileMode.REPEAT;
case 2:
return TileMode.MIRROR;
default:
return null;
}
}
public void setTileModeX(TileMode tileModeX) {
if (this.mTileModeX != tileModeX) {
this.mTileModeX = tileModeX;
updateDrawableAttrs();
updateBackgroundDrawableAttrs(false);
invalidate();
}
}
public ColorPickerView(final Context context, final AttributeSet attrs, final int defStyleAttr) {
super(context, attrs, defStyleAttr);
// if (DEBUG) Log.v(TAG, "ColorPickerView:");
DENSITY = context.getResources().getDisplayMetrics().density;
RECTANGLE_TRACKER_OFFSET = RECTANGLE_TRACKER_OFFSET_DP * DENSITY;
SELECTED_RADIUS = DEFAULT_SELECTED_RADIUS * DENSITY;
mAlphaShader = new BitmapShader(BitmapHelper.makeCheckBitmap(), TileMode.REPEAT, TileMode.REPEAT);
mAlphaDrawable = new ShaderDrawable(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG, 0);
mAlphaDrawable.setShader(mAlphaShader);
radius = 0;
internalSetColor(mColor, false);
// 色相円用
setHueColorArray(mAlpha, COLORS);
mPaint.setShader(new SweepGradient(0, 0, COLORS, null));
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(0);
// 選択色用
mSelectionPaint.setColor(mColor);
mSelectionPaint.setStrokeWidth(5);
// スライダーのトラッカー表示用
mTrackerPaint.setColor(TRACKER_COLOR);
mTrackerPaint.setStyle(Paint.Style.STROKE);
mTrackerPaint.setStrokeWidth(2f * DENSITY);
mTrackerPaint.setAntiAlias(true);
}
@SuppressLint("DrawAllocation")
@Override
protected void onLayout(final boolean changed, final int left, final int top, final int right, final int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (getWidth() != 0 && getHeight() != 0) {
// if (DEBUG) Log.v(TAG, String.format("onLayout:(%d,%d)(%d,%d)", left, top, right, bottom));
final int width = getWidth();
final int height = getHeight();
mDrawingRect.set(0, 0, width, height);
// 右端(明度)と下端(アルファ)にスライダーを表示するスペースを確保
int dimeter = Math.min(width, height);
slider_width = dimeter / 10; // 表示可能領域の10%
if (slider_width < 32) slider_width = (int)(32 * DENSITY); // でも32dpを最小値とする
final int space = slider_width + (int)(16 * DENSITY); // 更に16dpのスペースを開ける
// 色相円の直径
dimeter -= space;
// 色相円の外周半径
radius = dimeter >>> 1;
radius2 = radius * radius; // 2乗
// 色相円の中央座標
center_x = (width - (mShowValSlider ? slider_width : 0)) >>> 1;
center_y = (height - (mShowAlphaSlider ? slider_width : 0)) >>> 1;
// 選択色表示用の円の半径
final int selection_radius = (int)(Math.sqrt(center_x * center_x + center_y * center_y)) - radius;
mSelectionRect.set(-selection_radius, -selection_radius, selection_radius, selection_radius);
// 色相円用のグラデーションを初期化
mGradientPaint.setShader(new RadialGradient(0, 0, radius, 0xffffffff, 0x00ffffff, TileMode.CLAMP));
// スライダーの初期化
setupAlphaRect();
setUpValRect();
// 選択色を更新
setColor(mAlpha, mHue, mSat, mVal, true);
}
}