Java Code Examples for android.graphics.SweepGradient#setLocalMatrix()

The following examples show how to use android.graphics.SweepGradient#setLocalMatrix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeviceSeekArc.java    From arcusandroid with Apache License 2.0 6 votes vote down vote up
private Paint getHiliteStrokePaint(float startAngle, float endAngle, RectF seekArcBounds, int strokeWidth) {
    // Create the gradient shader
    int[] colors = {startColor, startColor, endColor, endColor};
    float[] positions = {0, startAngle / 360f, endAngle / 360f, 1};
    SweepGradient gradient = new SweepGradient(seekArcBounds.left + seekArcBounds.width() / 2, seekArcBounds.top + seekArcBounds.height() / 2, colors , positions);

    // Rotate to match the orientation of the seek arc
    Matrix theMatrix = new Matrix();
    theMatrix.setRotate(90, seekArcBounds.left + seekArcBounds.width() / 2, seekArcBounds.top + seekArcBounds.height() / 2);
    gradient.setLocalMatrix(theMatrix);

    // Create the paint
    Paint p = new Paint();
    p.setStrokeWidth(strokeWidth);
    p.setShader(gradient);
    p.setAntiAlias(true);
    p.setStyle(Paint.Style.STROKE);

    return p;
}
 
Example 2
Source File: CircleProgressBar.java    From ProjectX with Apache License 2.0 6 votes vote down vote up
/**
 * 绘制进度
 *
 * @param canvas 画布
 */
protected void drawProgress(Canvas canvas) {
    if (mRadius == 0 || mProgressSize == 0 || mAnimatorProgress < 0)
        return;
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(mProgressSize);
    final float halfCircleSize = mProgressSize * 0.5f;
    mRectF.set(-mRadius + halfCircleSize, -mRadius + halfCircleSize,
            mRadius - halfCircleSize, mRadius - halfCircleSize);
    SweepGradient sweepGradient = new SweepGradient(0, 0, mGradientColors, mGradientPositions);
    mRotateMatrix.setRotate(mStartAngle, 0, 0);
    sweepGradient.setLocalMatrix(mRotateMatrix);
    mPaint.setShader(sweepGradient);
    canvas.save();
    canvas.drawArc(mRectF, mStartAngle, mAnimatorProgressAngle, false, mPaint);
    canvas.restore();
}
 
Example 3
Source File: CircleProgressBar.java    From ProjectX with Apache License 2.0 6 votes vote down vote up
/**
 * 绘制载入
 *
 * @param canvas 画布
 */
protected void drawLoading(Canvas canvas) {
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(mProgressSize);
    final float halfCircleSize = mProgressSize * 0.5f;
    SweepGradient sweepGradient = new SweepGradient(0, 0, mGradientColors, mGradientPositions);
    mRotateMatrix.setRotate(mStartAngle - mLoadingStartAngle -
            mLoadingSweepAngle * mLoadingOffset, 0, 0);
    sweepGradient.setLocalMatrix(mRotateMatrix);
    mPaint.setShader(sweepGradient);
    canvas.save();
    canvas.rotate(mLoadingStartAngle);
    canvas.rotate(mLoadingSweepAngle * mLoadingOffset);
    canvas.drawPoint(mRadius - halfCircleSize, 0, mPaint);
    canvas.restore();
}
 
Example 4
Source File: CircleLoadingView.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
private void initRect(float width, float height) {
    if (width > height) {
        rect.left = (width - height) / 2;
        rect.right = width - rect.left;
        rect.top = 0;
        rect.bottom = height;
    } else {
        rect.left = 0;
        rect.right = width;
        rect.top = (height - width) / 2;
        rect.bottom = height - rect.top;
    }

   int  circleThickness = (int) (width*circleThicknessRatio);
    paint.setStrokeWidth(circleThickness);

    rect.left = rect.left + circleThickness / 2;
    rect.right = rect.right - circleThickness / 2;
    rect.top = rect.top + circleThickness / 2;
    rect.bottom = rect.bottom - circleThickness / 2;

    //因为这个圆环是顺时针旋转的,所有endColor, startColor在shader上反过来写了
    shader = new SweepGradient(width / 2, height / 2, endColor, startColor);
    matrix = new Matrix();
    matrix.setRotate(-90 + 20,width/2,height/2);//着色器初始位置,12点钟方法加20度,收尾留一点空隙
    shader.setLocalMatrix(matrix);
    paint.setShader(shader);
}
 
Example 5
Source File: CircleProgress.java    From android with MIT License 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    RectF oval = new RectF(
            CIRCLE_STROKE_WIDTH,
            CIRCLE_STROKE_WIDTH,
            mSize - CIRCLE_STROKE_WIDTH,
            mSize - CIRCLE_STROKE_WIDTH);

    Paint circlePaint = new Paint();
    circlePaint.setAntiAlias(true);
    circlePaint.setStrokeWidth(CIRCLE_STROKE_WIDTH);
    circlePaint.setStyle(Paint.Style.STROKE);
    circlePaint.setStrokeCap(Paint.Cap.ROUND);
    Matrix matrix = new Matrix();
    matrix.preRotate(CIRCLE_START_ANGLE, mSize / 2, mSize / 2);
    SweepGradient gradient = new SweepGradient(mSize / 2, mSize / 2,
            CIRCLE_STROKE_COLOR, CIRCLE_STROKE_COLOR_END);
    gradient.setLocalMatrix(matrix);
    circlePaint.setShader(gradient);
    canvas.drawArc(oval, CIRCLE_START_ANGLE, mProgress, false, circlePaint);

    Paint fullPaint = new Paint();
    fullPaint.setAntiAlias(true);
    circlePaint.setStyle(Paint.Style.STROKE);
    fullPaint.setStrokeCap(Paint.Cap.ROUND);
    fullPaint.setColor(Color.TRANSPARENT);

    if (mProgress == 360) {
        fullPaint.setColor(CIRCLE_STROKE_COLOR_END);
    } else if (mProgress > 1) {
        fullPaint.setColor(CIRCLE_STROKE_COLOR);
    }
    canvas.drawCircle(mSize / 2, CIRCLE_STROKE_WIDTH, CIRCLE_STROKE_WIDTH / 2, fullPaint);

}
 
Example 6
Source File: CircularProgressBarDrawable.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/**
 * This checks mRectIsDirty, and if it is true, recomputes both our drawing
 * rectangle (mRect) and the gradient itself, since it depends on our
 * rectangle too.
 * @return true if the resulting rectangle is not empty, false otherwise
 */
private boolean ensureValidRect() {
    if (mRectIsDirty) {
        mRectIsDirty = false;

        Rect bounds = getBounds();
        float inset = 0;

        final CircularProgressBarState st = mCircularProgressBarState;

        mRect.set(bounds.left + inset, bounds.top + inset,
                bounds.right - inset, bounds.bottom - inset);

        final int[] colors = st.mColors;
        if (colors != null) {
            RectF r = mRect;
            float x0, y0;

            x0 = r.left + (r.right - r.left) * st.mCenterX;
            y0 = r.top + (r.bottom - r.top) * st.mCenterY;

            final SweepGradient sweepGradient = new SweepGradient(x0, y0, colors, null);
            Matrix flipMatrix = new Matrix();
            flipMatrix.setScale(1, -1);
            flipMatrix.postTranslate(0, (r.bottom - r.top));
            flipMatrix.postRotate(-startingAngle, x0, y0);
            sweepGradient.setLocalMatrix(flipMatrix);
            mFillPaint.setShader(sweepGradient);

            // If we don't have a solid color, the alpha channel must be
            // maxed out so that alpha modulation works correctly.
            if (!st.mHasSolidColor) {
                mFillPaint.setColor(Color.BLACK);
            }
        }
    }
    return !mRect.isEmpty();
}
 
Example 7
Source File: CircularProgressBarDrawable.java    From android-circular-progress-bar with Apache License 2.0 5 votes vote down vote up
/**
 * This checks mRectIsDirty, and if it is true, recomputes both our drawing
 * rectangle (mRect) and the gradient itself, since it depends on our
 * rectangle too.
 * @return true if the resulting rectangle is not empty, false otherwise
 */
private boolean ensureValidRect() {
    if (mRectIsDirty) {
        mRectIsDirty = false;

        Rect bounds = getBounds();
        float inset = 0;

        final CircularProgressBarState st = mCircularProgressBarState;

        mRect.set(bounds.left + inset, bounds.top + inset,
                bounds.right - inset, bounds.bottom - inset);

        final int[] colors = st.mColors;
        if (colors != null) {
            RectF r = mRect;
            float x0, y0;

            x0 = r.left + (r.right - r.left) * st.mCenterX;
            y0 = r.top + (r.bottom - r.top) * st.mCenterY;

            final SweepGradient sweepGradient = new SweepGradient(x0, y0, colors, null);
            Matrix flipMatrix = new Matrix();
            flipMatrix.setScale(1, -1);
            flipMatrix.postTranslate(0, (r.bottom - r.top));
            flipMatrix.postRotate(-startingAngle, x0, y0);
            sweepGradient.setLocalMatrix(flipMatrix);
            mFillPaint.setShader(sweepGradient);

            // If we don't have a solid color, the alpha channel must be
            // maxed out so that alpha modulation works correctly.
            if (!st.mHasSolidColor) {
                mFillPaint.setColor(Color.BLACK);
            }
        }
    }
    return !mRect.isEmpty();
}
 
Example 8
Source File: CircularProgressBarDrawable.java    From Noyze with Apache License 2.0 5 votes vote down vote up
/**
 * This checks mRectIsDirty, and if it is true, recomputes both our drawing
 * rectangle (mRect) and the gradient itself, since it depends on our
 * rectangle too.
 * @return true if the resulting rectangle is not empty, false otherwise
 */
private boolean ensureValidRect() {
    if (mRectIsDirty) {
        mRectIsDirty = false;

        Rect bounds = getBounds();
        float inset = 0;

        final CircularProgressBarState st = mCircularProgressBarState;

        mRect.set(bounds.left + inset, bounds.top + inset,
                bounds.right - inset, bounds.bottom - inset);

        final int[] colors = st.mColors;
        if (colors != null) {
            RectF r = mRect;
            float x0, y0;

            x0 = r.left + (r.right - r.left) * st.mCenterX;
            y0 = r.top + (r.bottom - r.top) * st.mCenterY;

            final SweepGradient sweepGradient = new SweepGradient(x0, y0, colors, null);
            Matrix flipMatrix = new Matrix();
            flipMatrix.setScale(1, -1);
            flipMatrix.postTranslate(0, (r.bottom - r.top));
            flipMatrix.postRotate(-startingAngle, x0, y0);
            sweepGradient.setLocalMatrix(flipMatrix);
            mFillPaint.setShader(sweepGradient);

            // If we don't have a solid color, the alpha channel must be
            // maxed out so that alpha modulation works correctly.
            if (!st.mHasSolidColor) {
                mFillPaint.setColor(Color.BLACK);
            }
        }
    }
    return !mRect.isEmpty();
}
 
Example 9
Source File: DynamicPieChartOld.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
protected void drawPie(Canvas canvas, RectF rectf, float f1, float f2, float f3, float f4, float f5)
{
    canvas.drawCircle(f1, f2, f3 - o.getStrokeWidth() / 2.0F - n, o);
    canvas.drawCircle(f1, f2, f3 - i.getStrokeWidth() / 2.0F - 2.0F * n, i);
    float f6 = f4 * f5;
    if (f6 > 1.0F)
    {
        f6 = 1.0F;
    }
    if (c)
    {
        int ai[] = {
            0xffffff, -1, 0xffffff
        };
        if (f6 == 0.0F)
        {
            f6 = 0.5F;
        }
        k = new SweepGradient(f1, f2, ai, new float[] {
            0.0F, f6, 1.0F
        });
        j.setRotate(-90F + mRotate, f1, f2);
        k.setLocalMatrix(j);
        h.setShader(k);
    } else
    {
        h.setShader(null);
        h.setColor(-1);
    }
    if (f6 < 1.0F)
    {
        canvas.drawArc(m, -90F + mRotate + l, f6 * (360F - 2.0F * l), false, h);
    } else
    {
        canvas.drawCircle(f1, f2, f3 - i.getStrokeWidth() / 2.0F - 2.0F * n, h);
    }
    switch (d)
    {
    case 0: // '\0'
    default:
        return;

    case 1: // '\001'
        ChartUtil.erase(canvas, p);
        ChartUtil.drawBitmapCenter(canvas, p.centerX(), p.centerY(), mDensityScale, e, null);
        return;

    case 2: // '\002'
        ChartUtil.erase(canvas, p);
        ChartUtil.drawBitmapCenter(canvas, p.centerX(), p.centerY(), mDensityScale, f, null);
        return;

    case 3: // '\003'
        ChartUtil.erase(canvas, p);
        break;
    }
    ChartUtil.drawBitmapCenter(canvas, p.centerX(), p.centerY(), mDensityScale, g, null);
}
 
Example 10
Source File: ColorPicker.java    From px-android with MIT License 2 votes vote down vote up
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    // drawing color wheel

    canvas.drawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null);

    // drawing color view

    colorViewPaint.setColor(Color.HSVToColor(colorHSV));
    canvas.drawPath(colorViewPath, colorViewPaint);

    // drawing value slider

    float[] hsv = { colorHSV[0], colorHSV[1], 1f };

    SweepGradient sweepGradient =
        new SweepGradient(centerX, centerY, new int[] { Color.BLACK, Color.HSVToColor(hsv), Color.WHITE }, null);
    sweepGradient.setLocalMatrix(gradientRotationMatrix);
    valueSliderPaint.setShader(sweepGradient);

    canvas.drawPath(valueSliderPath, valueSliderPaint);

    // drawing color wheel pointer

    float hueAngle = (float) Math.toRadians(colorHSV[0]);
    int colorPointX = (int) (-Math.cos(hueAngle) * colorHSV[1] * colorWheelRadius) + centerX;
    int colorPointY = (int) (-Math.sin(hueAngle) * colorHSV[1] * colorWheelRadius) + centerY;

    float pointerRadius = 0.075f * colorWheelRadius;
    int pointerX = (int) (colorPointX - pointerRadius / 2);
    int pointerY = (int) (colorPointY - pointerRadius / 2);

    colorPointerCoords.set(pointerX, pointerY, pointerX + pointerRadius, pointerY + pointerRadius);
    canvas.drawOval(colorPointerCoords, colorPointerPaint);

    // drawing value pointer

    valuePointerPaint.setColor(Color.HSVToColor(new float[] { 0f, 0f, 1f - colorHSV[2] }));

    double valueAngle = (colorHSV[2] - 0.5f) * Math.PI;
    float valueAngleX = (float) Math.cos(valueAngle);
    float valueAngleY = (float) Math.sin(valueAngle);

    canvas.drawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY,
        valueAngleX * outerWheelRadius + centerX,
        valueAngleY * outerWheelRadius + centerY, valuePointerPaint);

    // drawing pointer arrow

    if (arrowPointerSize > 0) {
        drawPointerArrow(canvas);
    }
}
 
Example 11
Source File: ColorPicker.java    From Mi-Band with GNU General Public License v2.0 2 votes vote down vote up
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    // drawing color wheel

    canvas.drawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null);

    // drawing color view

    colorViewPaint.setColor(Color.HSVToColor(colorHSV));
    canvas.drawPath(colorViewPath, colorViewPaint);

    // drawing value slider

    float[] hsv = new float[]{colorHSV[0], colorHSV[1], 1f};

    SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[]{Color.BLACK, Color.HSVToColor(hsv), Color.WHITE}, null);
    sweepGradient.setLocalMatrix(gradientRotationMatrix);
    valueSliderPaint.setShader(sweepGradient);

    canvas.drawPath(valueSliderPath, valueSliderPaint);

    // drawing color wheel pointer

    float hueAngle = (float) Math.toRadians(colorHSV[0]);
    int colorPointX = (int) (-Math.cos(hueAngle) * colorHSV[1] * colorWheelRadius) + centerX;
    int colorPointY = (int) (-Math.sin(hueAngle) * colorHSV[1] * colorWheelRadius) + centerY;

    float pointerRadius = 0.075f * colorWheelRadius;
    int pointerX = (int) (colorPointX - pointerRadius / 2);
    int pointerY = (int) (colorPointY - pointerRadius / 2);

    colorPointerCoords.set(pointerX, pointerY, pointerX + pointerRadius, pointerY + pointerRadius);
    canvas.drawOval(colorPointerCoords, colorPointerPaint);

    // drawing value pointer

    valuePointerPaint.setColor(Color.HSVToColor(new float[]{0f, 0f, 1f - colorHSV[2]}));

    double valueAngle = (colorHSV[2] - 0.5f) * Math.PI;
    float valueAngleX = (float) Math.cos(valueAngle);
    float valueAngleY = (float) Math.sin(valueAngle);

    canvas.drawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY, valueAngleX * outerWheelRadius + centerX,
            valueAngleY * outerWheelRadius + centerY, valuePointerPaint);

    // drawing pointer arrow

    if (arrowPointerSize > 0) {
        drawPointerArrow(canvas);
    }

}
 
Example 12
Source File: ColorPicker.java    From redalert-android with Apache License 2.0 2 votes vote down vote up
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    // drawing color wheel

    canvas.drawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null);

    // drawing color view

    colorViewPaint.setColor(Color.HSVToColor(colorHSV));
    canvas.drawPath(colorViewPath, colorViewPaint);

    // drawing value slider

    float[] hsv = new float[]{colorHSV[0], colorHSV[1], 1f};

    SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[]{Color.BLACK, Color.HSVToColor(hsv), Color.WHITE}, null);
    sweepGradient.setLocalMatrix(gradientRotationMatrix);
    valueSliderPaint.setShader(sweepGradient);

    canvas.drawPath(valueSliderPath, valueSliderPaint);

    // drawing color wheel pointer

    float hueAngle = (float) Math.toRadians(colorHSV[0]);
    int colorPointX = (int) (-Math.cos(hueAngle) * colorHSV[1] * colorWheelRadius) + centerX;
    int colorPointY = (int) (-Math.sin(hueAngle) * colorHSV[1] * colorWheelRadius) + centerY;

    float pointerRadius = 0.075f * colorWheelRadius;
    int pointerX = (int) (colorPointX - pointerRadius / 2);
    int pointerY = (int) (colorPointY - pointerRadius / 2);

    colorPointerCoords.set(pointerX, pointerY, pointerX + pointerRadius, pointerY + pointerRadius);
    canvas.drawOval(colorPointerCoords, colorPointerPaint);

    // drawing value pointer

    valuePointerPaint.setColor(Color.HSVToColor(new float[]{0f, 0f, 1f - colorHSV[2]}));

    double valueAngle = (colorHSV[2] - 0.5f) * Math.PI;
    float valueAngleX = (float) Math.cos(valueAngle);
    float valueAngleY = (float) Math.sin(valueAngle);

    canvas.drawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY, valueAngleX * outerWheelRadius + centerX,
            valueAngleY * outerWheelRadius + centerY, valuePointerPaint);

    // drawing pointer arrow

    if (arrowPointerSize > 0) {
        drawPointerArrow(canvas);
    }

}
 
Example 13
Source File: ColorPicker.java    From Android-Color-Picker with Apache License 2.0 2 votes vote down vote up
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    // drawing color wheel

    canvas.drawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null);

    // drawing color view

    colorViewPaint.setColor(Color.HSVToColor(colorHSV));
    canvas.drawPath(colorViewPath, colorViewPaint);

    // drawing value slider

    float[] hsv = new float[] { colorHSV[0], colorHSV[1], 1f };

    SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[] { Color.BLACK, Color.HSVToColor(hsv), Color.WHITE }, null);
    sweepGradient.setLocalMatrix(gradientRotationMatrix);
    valueSliderPaint.setShader(sweepGradient);

    canvas.drawPath(valueSliderPath, valueSliderPaint);

    // drawing color wheel pointer

    float hueAngle = (float) Math.toRadians(colorHSV[0]);
    int colorPointX = (int) (-Math.cos(hueAngle) * colorHSV[1] * colorWheelRadius) + centerX;
    int colorPointY = (int) (-Math.sin(hueAngle) * colorHSV[1] * colorWheelRadius) + centerY;

    float pointerRadius = 0.075f * colorWheelRadius;
    int pointerX = (int) (colorPointX - pointerRadius / 2);
    int pointerY = (int) (colorPointY - pointerRadius / 2);

    colorPointerCoords.set(pointerX, pointerY, pointerX + pointerRadius, pointerY + pointerRadius);
    canvas.drawOval(colorPointerCoords, colorPointerPaint);

    // drawing value pointer

    valuePointerPaint.setColor(Color.HSVToColor(new float[] { 0f, 0f, 1f - colorHSV[2] }));

    double valueAngle = (colorHSV[2] - 0.5f) * Math.PI;
    float valueAngleX = (float) Math.cos(valueAngle);
    float valueAngleY = (float) Math.sin(valueAngle);

    canvas.drawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY, valueAngleX * outerWheelRadius + centerX,
            valueAngleY * outerWheelRadius + centerY, valuePointerPaint);

    // drawing pointer arrow

    if (arrowPointerSize > 0) {
        drawPointerArrow(canvas);
    }

}
 
Example 14
Source File: MultiColorPicker.java    From Android-Color-Picker with Apache License 2.0 2 votes vote down vote up
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    // drawing color wheel

    canvas.drawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null);

    // drawing color view

    int[] segmentColors = getColors();
    float sweepAngleStep = 180f / paramColorCount;
    for (int i = 0; i < paramColorCount; i++) {

        colorViewPath.reset();
        colorViewPath.arcTo(outerWheelRect, 270 - i * sweepAngleStep, -sweepAngleStep);
        colorViewPath.arcTo(innerWheelRect, 90 + (paramColorCount - i - 1) * sweepAngleStep, sweepAngleStep);

        colorViewPaint.setColor(segmentColors[i]);

        canvas.drawPath(colorViewPath, colorViewPaint);

    }

    // drawing value slider

    float[] hsv = new float[] { colorHSV[0], colorHSV[1], 1f };

    SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[] { Color.BLACK, Color.HSVToColor(hsv), Color.WHITE }, null);
    sweepGradient.setLocalMatrix(gradientRotationMatrix);
    valueSliderPaint.setShader(sweepGradient);

    canvas.drawPath(valueSliderPath, valueSliderPaint);

    // drawing color wheel pointer

    for (int i = 0; i < paramColorCount; i++) {
        drawColorWheelPointer(canvas, (float) Math.toRadians(adjacentHue[i]));
    }

    // drawing value pointer

    valuePointerPaint.setColor(Color.HSVToColor(new float[] { 0f, 0f, 1f - colorHSV[2] }));

    double valueAngle = (colorHSV[2] - 0.5f) * Math.PI;
    float valueAngleX = (float) Math.cos(valueAngle);
    float valueAngleY = (float) Math.sin(valueAngle);

    canvas.drawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY, valueAngleX * outerWheelRadius + centerX,
            valueAngleY * outerWheelRadius + centerY, valuePointerPaint);

    // drawing pointer arrow

    if (arrowPointerSize > 0) {
        drawPointerArrow(canvas);
    }

}
 
Example 15
Source File: ColorPicker.java    From SystemBarTint with Apache License 2.0 2 votes vote down vote up
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    // drawing color wheel

    canvas.drawBitmap(colorWheelBitmap, centerX - colorWheelRadius, centerY - colorWheelRadius, null);

    // drawing color view

    colorViewPaint.setColor(Color.HSVToColor(colorHSV));
    canvas.drawPath(colorViewPath, colorViewPaint);

    // drawing value slider

    float[] hsv = new float[] { colorHSV[0], colorHSV[1], 1f };

    SweepGradient sweepGradient = new SweepGradient(centerX, centerY, new int[] { Color.BLACK, Color.HSVToColor(hsv), Color.WHITE }, null);
    sweepGradient.setLocalMatrix(gradientRotationMatrix);
    valueSliderPaint.setShader(sweepGradient);

    canvas.drawPath(valueSliderPath, valueSliderPaint);

    // drawing color wheel pointer

    float hueAngle = (float) Math.toRadians(colorHSV[0]);
    int colorPointX = (int) (-Math.cos(hueAngle) * colorHSV[1] * colorWheelRadius) + centerX;
    int colorPointY = (int) (-Math.sin(hueAngle) * colorHSV[1] * colorWheelRadius) + centerY;

    float pointerRadius = 0.075f * colorWheelRadius;
    int pointerX = (int) (colorPointX - pointerRadius / 2);
    int pointerY = (int) (colorPointY - pointerRadius / 2);

    colorPointerCoords.set(pointerX, pointerY, pointerX + pointerRadius, pointerY + pointerRadius);
    canvas.drawOval(colorPointerCoords, colorPointerPaint);

    // drawing value pointer

    valuePointerPaint.setColor(Color.HSVToColor(new float[] { 0f, 0f, 1f - colorHSV[2] }));

    double valueAngle = (colorHSV[2] - 0.5f) * Math.PI;
    float valueAngleX = (float) Math.cos(valueAngle);
    float valueAngleY = (float) Math.sin(valueAngle);

    canvas.drawLine(valueAngleX * innerWheelRadius + centerX, valueAngleY * innerWheelRadius + centerY, valueAngleX * outerWheelRadius + centerX,
            valueAngleY * outerWheelRadius + centerY, valuePointerPaint);

    // drawing pointer arrow

    if (arrowPointerSize > 0) {
        drawPointerArrow(canvas);
    }

}