Java Code Examples for android.graphics.Canvas#drawOval()

The following examples show how to use android.graphics.Canvas#drawOval() . 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: DownloadButtonProgress.java    From AndroidButtonProgress with Apache License 2.0 6 votes vote down vote up
private void drawDeterminateState(Canvas canvas) {
    if (mDeterminateBgDrawable != null) {
        mDeterminateBgDrawable.setBounds(0, 0, getWidth(), getHeight());
        mDeterminateBgDrawable.draw(canvas);
    } else {
        mBgRect.set(0, 0, getWidth(), getHeight());
        mBgPaint.setColor(mDeterminateBgColor);
        canvas.drawOval(mBgRect, mBgPaint);
    }

    if (mCancelable) {
        drawDrawableInCenter(mCancelIcon, canvas, mCancelIconWidth, mCancelIconHeight);
    }

    setProgressRectBounds();
    mProgressPaint.setColor(mProgressDeterminateColor);
    canvas.drawArc(mProgressRect, BASE_START_ANGLE, getDegrees(), false, mProgressPaint);
}
 
Example 2
Source File: ShapeBadgeItem.java    From BottomNavigation with Apache License 2.0 6 votes vote down vote up
/**
 * draw's specified shape
 *
 * @param canvas on which shape has to be drawn
 */
void draw(Canvas canvas) {
    mCanvasRect.set(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight());
    switch (mShape) {
        case SHAPE_RECTANGLE:
            canvas.drawRect(mCanvasRect, mCanvasPaint);
            break;
        case SHAPE_OVAL:
            canvas.drawOval(mCanvasRect, mCanvasPaint);
            break;
        case SHAPE_STAR_3_VERTICES:
        case SHAPE_STAR_4_VERTICES:
        case SHAPE_STAR_5_VERTICES:
        case SHAPE_STAR_6_VERTICES:
            drawStar(canvas, mShape);
            break;
        case SHAPE_HEART:
            drawHeart(canvas);
            break;
    }
}
 
Example 3
Source File: TextDrawable.java    From Muzesto with GNU General Public License v3.0 5 votes vote down vote up
private void drawBorder(Canvas canvas) {
    RectF rect = new RectF(getBounds());
    rect.inset(borderThickness / 2, borderThickness / 2);

    if (shape instanceof OvalShape) {
        canvas.drawOval(rect, borderPaint);
    } else if (shape instanceof RoundRectShape) {
        canvas.drawRoundRect(rect, radius, radius, borderPaint);
    } else {
        canvas.drawRect(rect, borderPaint);
    }
}
 
Example 4
Source File: RoundedDrawable.java    From Android with MIT License 5 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
  if (mRebuildShader) {
    BitmapShader bitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY);
    if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) {
      bitmapShader.setLocalMatrix(mShaderMatrix);
    }
    mBitmapPaint.setShader(bitmapShader);
    mRebuildShader = false;
  }

  if (mOval) {
    if (mBorderWidth > 0) {
      canvas.drawOval(mDrawableRect, mBitmapPaint);
      canvas.drawOval(mBorderRect, mBorderPaint);
    } else {
      canvas.drawOval(mDrawableRect, mBitmapPaint);
    }
  } else {
    if (any(mCornersRounded)) {
      float radius = mCornerRadius;
      if (mBorderWidth > 0) {
        canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
        canvas.drawRoundRect(mBorderRect, radius, radius, mBorderPaint);
        redrawBitmapForSquareCorners(canvas);
        redrawBorderForSquareCorners(canvas);
      } else {
        canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
        redrawBitmapForSquareCorners(canvas);
      }
    } else {
      canvas.drawRect(mDrawableRect, mBitmapPaint);
      if (mBorderWidth > 0) {
        canvas.drawRect(mBorderRect, mBorderPaint);
      }
    }
  }
}
 
Example 5
Source File: FloatingActionButton.java    From UltimateRecyclerView with Apache License 2.0 5 votes vote down vote up
protected Drawable createAlphaDrawble(RectF circleRect, int color, float alpha) {
    final Bitmap bitmap = Bitmap.createBitmap(mDrawableSize, mDrawableSize, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(color);
    paint.setAlpha(opacityToAlpha(alpha));
    canvas.drawOval(circleRect, paint);
    return new BitmapDrawable(getResources(), bitmap);
}
 
Example 6
Source File: CircleImageView.java    From aurora-imui with MIT License 5 votes vote down vote up
public static Bitmap getBitmap(int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);
    canvas.drawOval(new RectF(0.0f, 0.0f, width, height), paint);

    return bitmap;
}
 
Example 7
Source File: CircleImageView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
public static Bitmap getBitmap(int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLACK);
    canvas.drawOval(new RectF(0.0f, 0.0f, width, height), paint);

    return bitmap;
}
 
Example 8
Source File: DownloadProgressBar.java    From hipda with GNU General Public License v2.0 5 votes vote down vote up
private void drawFinishState(Canvas canvas) {
    if (mFinishBgDrawable != null) {
        mFinishBgDrawable.setBounds(0, 0, getWidth(), getHeight());
        mFinishBgDrawable.draw(canvas);
    } else {
        mBgRect.set(0, 0, getWidth(), getHeight());
        mBgPaint.setColor(mFinishBgColor);
        canvas.drawOval(mBgRect, mBgPaint);
    }

    drawDrawableInCenter(mFinishIcon, canvas, mFinishIconWidth, mFinishIconHeight);
}
 
Example 9
Source File: CheckableDrawable.java    From SublimePicker with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(Canvas canvas) {
    if (!mReady) {
        setDimens(getBounds().width(), getBounds().height());
        return;
    }

    canvas.drawOval(mRectToDraw, mPaint);
}
 
Example 10
Source File: ProgressRadial.java    From Building-Professional-Android-Applications with MIT License 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    float startAngle = 180.0f;
    float maxAngleLength = 360.0f;

    canvas.drawOval(rectF, circlePaint);
    float angleLength = maxAngleLength * progress;

    canvas.drawArc(rectF, startAngle, angleLength, true, arcPaint);
}
 
Example 11
Source File: RoundedDrawable.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas) {
  if (mRebuildShader) {
    BitmapShader bitmapShader = new BitmapShader(mBitmap, mTileModeX, mTileModeY);
    if (mTileModeX == Shader.TileMode.CLAMP && mTileModeY == Shader.TileMode.CLAMP) {
      bitmapShader.setLocalMatrix(mShaderMatrix);
    }
    mBitmapPaint.setShader(bitmapShader);
    mRebuildShader = false;
  }

  if (mOval) {
    if (mBorderWidth > 0) {
      canvas.drawOval(mDrawableRect, mBitmapPaint);
      canvas.drawOval(mBorderRect, mBorderPaint);
    } else {
      canvas.drawOval(mDrawableRect, mBitmapPaint);
    }
  } else {
    if (any(mCornersRounded)) {
      float radius = mCornerRadius;
      if (mBorderWidth > 0) {
        canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
        canvas.drawRoundRect(mBorderRect, radius, radius, mBorderPaint);
        redrawBitmapForSquareCorners(canvas);
        redrawBorderForSquareCorners(canvas);
      } else {
        canvas.drawRoundRect(mDrawableRect, radius, radius, mBitmapPaint);
        redrawBitmapForSquareCorners(canvas);
      }
    } else {
      canvas.drawRect(mDrawableRect, mBitmapPaint);
      canvas.drawRect(mBorderRect, mBorderPaint);
    }
  }
}
 
Example 12
Source File: CircularSplashView.java    From HaiNaBaiChuan with Apache License 2.0 4 votes vote down vote up
public void draw(Canvas canvas) {
    if (drawingRect != null)
        canvas.drawOval(drawingRect, paint);
}
 
Example 13
Source File: CropImageView.java    From ImageCropRotateFilter with MIT License 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (mFirstDrawFlag) {
        mFirstDrawFlag = false;
        imageScale();
    }

    setRemainArea();
    canvas.drawRect(mRemainLeftArea, mRemainAreaPaint);
    canvas.drawRect(mRemainRightArea, mRemainAreaPaint);
    canvas.drawRect(mRemainTopArea, mRemainAreaPaint);
    canvas.drawRect(mRemainBottomArea, mRemainAreaPaint);

    mChooseAreaPaint.setColor(Color.WHITE);
    mChooseAreaPaint.setStrokeWidth(5.0f);
    mChooseAreaPaint.setStyle(Paint.Style.STROKE);
    canvas.drawRect(mChooseArea, mChooseAreaPaint);

    mChooseAreaPaint.setColor(Color.argb(153, 255, 255, 255));
    mChooseAreaPaint.setStrokeWidth(2.0f);
    int point1x = (mChooseArea.right - mChooseArea.left) / 3 + mChooseArea.left;
    int point1y = mChooseArea.top;
    int point2x = (mChooseArea.right - mChooseArea.left) * 2 / 3 + mChooseArea.left;
    int point2y = mChooseArea.top;
    int point3x = mChooseArea.right;
    int point3y = (mChooseArea.bottom - mChooseArea.top) / 3 + mChooseArea.top;
    int point4x = mChooseArea.right;
    int point4y = (mChooseArea.bottom - mChooseArea.top) * 2 / 3 + mChooseArea.top;
    int point5x = point2x;
    int point5y = mChooseArea.bottom;
    int point6x = point1x;
    int point6y = mChooseArea.bottom;
    int point7x = mChooseArea.left;
    int point7y = point4y;
    int point8x = mChooseArea.left;
    int point8y = point3y;
    canvas.drawLine(point1x, point1y, point6x, point6y, mChooseAreaPaint);
    canvas.drawLine(point2x, point2y, point5x, point5y, mChooseAreaPaint);
    canvas.drawLine(point8x, point8y, point3x, point3y, mChooseAreaPaint);
    canvas.drawLine(point7x, point7y, point4x, point4y, mChooseAreaPaint);

    mChooseAreaPaint.setColor(Color.WHITE);
    mChooseAreaPaint.setStyle(Paint.Style.FILL);
    setPressArea();
    canvas.drawOval(new RectF(mPressLTArea), mChooseAreaPaint);
    canvas.drawOval(new RectF(mPressRTArea), mChooseAreaPaint);
    canvas.drawOval(new RectF(mPressRBArea), mChooseAreaPaint);
    canvas.drawOval(new RectF(mPressLBArea), mChooseAreaPaint);
}
 
Example 14
Source File: FloatingActionButton.java    From Gazetti_Newspaper_Reader with MIT License 4 votes vote down vote up
private Drawable createStrokesDrawable(RectF circleRect) {
    final Bitmap bitmap = Bitmap.createBitmap(mDrawableSize, mDrawableSize, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);

    final float strokeWidth = getDimension(R.dimen.fab_stroke_width);
    final float halfStrokeWidth = strokeWidth / 2f;

    RectF outerStrokeRect = new RectF(
            circleRect.left - halfStrokeWidth,
            circleRect.top - halfStrokeWidth,
            circleRect.right + halfStrokeWidth,
            circleRect.bottom + halfStrokeWidth
    );

    RectF innerStrokeRect = new RectF(
            circleRect.left + halfStrokeWidth,
            circleRect.top + halfStrokeWidth,
            circleRect.right - halfStrokeWidth,
            circleRect.bottom - halfStrokeWidth
    );

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStrokeWidth(strokeWidth);
    paint.setStyle(Style.STROKE);

    // outer
    paint.setColor(Color.BLACK);
    paint.setAlpha(opacityToAlpha(0.02f));
    canvas.drawOval(outerStrokeRect, paint);

    // inner bottom
    paint.setShader(new LinearGradient(innerStrokeRect.centerX(), innerStrokeRect.top, innerStrokeRect.centerX(), innerStrokeRect.bottom,
            new int[] { Color.TRANSPARENT, HALF_TRANSPARENT_BLACK, Color.BLACK },
            new float[] { 0f, 0.8f, 1f },
            TileMode.CLAMP
    ));
    paint.setAlpha(opacityToAlpha(0.04f));
    canvas.drawOval(innerStrokeRect, paint);

    // inner top
    paint.setShader(new LinearGradient(innerStrokeRect.centerX(), innerStrokeRect.top, innerStrokeRect.centerX(), innerStrokeRect.bottom,
            new int[] { Color.WHITE, HALF_TRANSPARENT_WHITE, Color.TRANSPARENT },
            new float[] { 0f, 0.2f, 1f },
            TileMode.CLAMP
    ));
    paint.setAlpha(opacityToAlpha(0.8f));
    canvas.drawOval(innerStrokeRect, paint);

    return new BitmapDrawable(getResources(), bitmap);
}
 
Example 15
Source File: FlyoutMenuView.java    From FlyoutMenus with MIT License 4 votes vote down vote up
void drawButton(Canvas canvas, float alpha) {

		// scale button down as it fades out
		if (alpha < 1) {
			Matrix m = new Matrix();
			m.preTranslate(-buttonCenter.x, -buttonCenter.y);
			m.postScale(alpha, alpha);
			m.postTranslate(buttonCenter.x, buttonCenter.y);
			canvas.concat(m);
		}

		if (buttonElevation > 0) {
			if (buttonShadowBitmap == null) {
				buttonShadowBitmap = createButtonShadowBitmap();
			}

			paint.setAlpha((int) ((alpha * alpha * alpha) * 255));
			float buttonShadowOffset = buttonElevation / 2;
			canvas.drawBitmap(buttonShadowBitmap, buttonCenter.x - buttonShadowBitmap.getWidth() / 2, buttonCenter.y - buttonShadowBitmap.getHeight() / 2 + buttonShadowOffset, paint);
		}

		int scaledAlpha = (int) (alpha * 255);
		paint.setAlpha(scaledAlpha);

		if (buttonRenderer == null) {
			paint.setColor(buttonBackgroundColor);
			canvas.drawOval(buttonFillOval, paint);
		}

		if (buttonDrawable != null) {
			buttonDrawable.setAlpha(scaledAlpha);

			// scale the radius to fit drawable inside circle
			float innerRadius = buttonRadius / 1.41421356237f;
			buttonDrawable.setBounds(
					(int) (buttonCenter.x - innerRadius),
					(int) (buttonCenter.y - innerRadius),
					(int) (buttonCenter.x + innerRadius),
					(int) (buttonCenter.y + innerRadius));

			buttonDrawable.draw(canvas);
		} else if (buttonRenderer != null) {
			buttonRenderer.onDrawButtonBase(canvas, buttonFillOval, buttonBackgroundColor, alpha);
			buttonRenderer.onDrawButtonContent(canvas, buttonFillOval, buttonBackgroundColor, alpha);
		}
	}
 
Example 16
Source File: CircularProgressView.java    From NaviBee with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected synchronized void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    //Either we are using "single-arc-progress" or "multiple-arc-progress".
    mValuesToDrawList.clear();
    if (!mMultipleArcsEnabled) {
        mValuesToDrawList.add(mProgress);
        mProgressPaintList.clear();
        mProgressPaintList.add(mProgressPaint);
    } else {
        mValuesToDrawList.addAll(mProgressList);
    }

    float angle;
    float previousAngle = mStartingAngle;
    float radius = getWidth() / 2 - mDefaultViewPadding - mProgressIconThickness - mProgressStrokeThickness / 2;
    double endX, endY;

    //Shadow logic
    if (mShadowEnabled) {
        angle = 360 * (mMultipleArcsEnabled ? mProgressListTotal : mProgress) / mMax;
        if (mReverseEnabled) {
            angle *= -1;
        }
        if (!mMultipleArcsEnabled && mProgressThumbEnabled) {
            //Only in "single-arc-progress", otherwise we'll end up with N thumbs

            //Who doesn't love a bit of math? :)
            //cos(a) = adj / hyp <>cos(angle) = x / radius <>x = cos(angle) * radius
            //sin(a) = opp / hyp <>sin(angle) = y / radius <>y = sin(angle) * radius
            //x = cos(startingAngle + progressAngle) * radius + originX(center)
            //y = sin(startingAngle + progressAngle) * radius + originY(center)
            endX = (Math.cos(Math.toRadians(previousAngle + angle)) * radius);
            endY = (Math.sin(Math.toRadians(previousAngle + angle)) * radius);
            canvas.drawCircle((float) endX + mShadowRectF.centerX(), (float) endY + mShadowRectF.centerY(), mProgressIconThickness, mShadowPaint);
        }
        canvas.drawArc(mShadowRectF, previousAngle, angle, false, mShadowPaint);
    }

    //Progress logic
    for (int i = 0; i < mValuesToDrawList.size(); i++) {
        if (!mMultipleArcsEnabled) {
            //No background will be used when "multiple-arc-progress" is enable because it will be mixed with the "progress-colors"
            canvas.drawOval(mProgressRectF, mBackgroundPaint);
        }

        angle = 360 * mValuesToDrawList.get(i) / mMax;
        if (mReverseEnabled) {
            angle *= -1;
        }
        float offset = !mReverseEnabled && mMultipleArcsEnabled ? ANGLE_OFFSET_FOR_MULTIPLE_ARC_PROGRESS : 0; //to better glue all the "pieces"
        canvas.drawArc(mProgressRectF, previousAngle - offset, angle + offset, false, mProgressPaintList.get(i));
        if (!mMultipleArcsEnabled && mProgressThumbEnabled) {
            //Only in "single-arc-progress", otherwise we'll end up with N thumbs
            endX = (Math.cos(Math.toRadians(previousAngle + angle)) * radius);
            endY = (Math.sin(Math.toRadians(previousAngle + angle)) * radius);
            canvas.drawCircle((float) endX + mProgressRectF.centerX(), (float) endY + mProgressRectF.centerY(), mProgressIconThickness, mProgressPaintList.get(i));
        }
        previousAngle += angle;
    }
}
 
Example 17
Source File: ViewfinderView.java    From QrCodeLib with MIT License 4 votes vote down vote up
private void drawLaserScanner(Canvas canvas, Rect frame) {
        paint.setColor(laserColor);
        //扫描线闪烁效果
//    paint.setAlpha(SCANNER_ALPHA[scannerAlpha]);
//    scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.length;
//    int middle = frame.height() / 2 + frame.top;
//    canvas.drawRect(frame.left + 2, middle - 1, frame.right - 1, middle + 2, paint);
        //线性渐变
        LinearGradient linearGradient = new LinearGradient(
                frame.left, scannerStart,
                frame.left, scannerStart + SCANNER_LINE_HEIGHT,
                shadeColor(laserColor),
                laserColor,
                Shader.TileMode.MIRROR);

        RadialGradient radialGradient = new RadialGradient(
                (float) (frame.left + frame.width() / 2),
                (float) (scannerStart + SCANNER_LINE_HEIGHT / 2),
                360f,
                laserColor,
                shadeColor(laserColor),
                Shader.TileMode.MIRROR);

        SweepGradient sweepGradient = new SweepGradient(
                (float) (frame.left + frame.width() / 2),
                (float) (scannerStart + SCANNER_LINE_HEIGHT),
                shadeColor(laserColor),
                laserColor);

        ComposeShader composeShader = new ComposeShader(radialGradient, linearGradient, PorterDuff.Mode.ADD);

        paint.setShader(radialGradient);
        if (scannerStart <= scannerEnd) {
            //矩形
//      canvas.drawRect(frame.left, scannerStart, frame.right, scannerStart + SCANNER_LINE_HEIGHT, paint);
            //椭圆
            RectF rectF = new RectF(frame.left + 2 * SCANNER_LINE_HEIGHT, scannerStart, frame.right - 2 * SCANNER_LINE_HEIGHT, scannerStart + SCANNER_LINE_HEIGHT);
            canvas.drawOval(rectF, paint);
            scannerStart += SCANNER_LINE_MOVE_DISTANCE;
        } else {
            scannerStart = frame.top;
        }
        paint.setShader(null);
    }
 
Example 18
Source File: HighlightView.java    From styT with Apache License 2.0 4 votes vote down vote up
private void drawCircle(Canvas canvas) {
    outlinePaint.setStrokeWidth(1);
    canvas.drawOval(new RectF(drawRect), outlinePaint);
}
 
Example 19
Source File: ProgressPieView.java    From okhttp-OkGo with Apache License 2.0 4 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    mInnerRectF.set(0, 0, mViewSize, mViewSize);
    mInnerRectF.offset((getWidth() - mViewSize) / 2, (getHeight() - mViewSize) / 2);
    if (mShowStroke) {
        final int halfBorder = (int) (mStrokePaint.getStrokeWidth() / 2f + 0.5f);
        mInnerRectF.inset(halfBorder, halfBorder);
    }
    float centerX = mInnerRectF.centerX();
    float centerY = mInnerRectF.centerY();

    canvas.drawArc(mInnerRectF, 0, 360, true, mBackgroundPaint);

    switch (mProgressFillType) {
        case FILL_TYPE_RADIAL:
            float sweepAngle = 360 * mProgress / mMax;
            if (mInverted) {
                sweepAngle = sweepAngle - 360;
            }
            if (mCounterclockwise) {
                sweepAngle = -sweepAngle;
            }
            canvas.drawArc(mInnerRectF, mStartAngle, sweepAngle, true, mProgressPaint);
            break;
        case FILL_TYPE_CENTER:
            float radius = (mViewSize / 2) * ((float) mProgress / mMax);
            if (mShowStroke) {
                radius = radius + 0.5f - mStrokePaint.getStrokeWidth();
            }
            canvas.drawCircle(centerX, centerY, radius, mProgressPaint);
            break;
        default:
            throw new IllegalArgumentException("Invalid Progress Fill = " + mProgressFillType);
    }

    if (!TextUtils.isEmpty(mText) && mShowText) {
        if (!TextUtils.isEmpty(mTypeface)) {
            Typeface typeface = sTypefaceCache.get(mTypeface);
            if (null == typeface && null != getResources()) {
                AssetManager assets = getResources().getAssets();
                if (null != assets) {
                    typeface = Typeface.createFromAsset(assets, mTypeface);
                    sTypefaceCache.put(mTypeface, typeface);
                }
            }
            mTextPaint.setTypeface(typeface);
        }
        int xPos = (int) centerX;
        int yPos = (int) (centerY - (mTextPaint.descent() + mTextPaint.ascent()) / 2);
        canvas.drawText(mText, xPos, yPos, mTextPaint);
    }

    if (null != mImage && mShowImage) {
        int drawableSize = mImage.getIntrinsicWidth();
        mImageRect.set(0, 0, drawableSize, drawableSize);
        mImageRect.offset((getWidth() - drawableSize) / 2, (getHeight() - drawableSize) / 2);
        mImage.setBounds(mImageRect);
        mImage.draw(canvas);
    }

    if (mShowStroke) {
        canvas.drawOval(mInnerRectF, mStrokePaint);
    }

}
 
Example 20
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);
    }

}