下面列出了android.graphics.Canvas#drawLine ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void drawSeparator(Canvas canvas, int height, int tabCount) {
if (dividerThickness <= 0) {
return;
}
final int dividerHeightPx = (int) (Math.min(Math.max(0f, dividerHeight), 1f) * height);
final SmartTabLayout.TabColorizer tabColorizer = getTabColorizer();
// Vertical separators between the titles
final int separatorTop = (height - dividerHeightPx) / 2;
final int separatorBottom = separatorTop + dividerHeightPx;
final boolean isLayoutRtl = Utils.isLayoutRtl(this);
for (int i = 0; i < tabCount - 1; i++) {
View child = getChildAt(i);
int end = Utils.getEnd(child);
int endMargin = Utils.getMarginEnd(child);
int separatorX = isLayoutRtl ? end - endMargin : end + endMargin;
dividerPaint.setColor(tabColorizer.getDividerColor(i));
canvas.drawLine(separatorX, separatorTop, separatorX, separatorBottom, dividerPaint);
}
}
void drawHalf(Canvas canvas, @NonNull Rect src, @NonNull Matrix transform, Paint shadowPaint, boolean top) {
canvas.save();
if (this.reversed) {
canvas.save();
canvas.translate(0.0f, (float) ((int) (((float) (top ? -1 : 1)) * (this.topFoldFactor * ((float) src.height())))));
canvas.clipRect(src);
} else {
this.clip.set(0.0f, 0.0f, (float) (src.right - src.left), (float) (src.bottom - src.top));
transform.mapRect(this.transformedClip, this.clip);
canvas.clipRect(this.transformedClip);
canvas.concat(transform);
canvas.save();
canvas.translate(0.0f, (float) (-src.top));
}
super.dispatchDraw(canvas);
if (this.reversed) {
int y = this.initialFoldCenter;
canvas.drawLine(0.0f, (float) y, (float) getWidth(), (float) y, this.foldLinePaint);
}
canvas.restore();
if (!this.reversed) {
canvas.drawRect(0.0f, 0.0f, (float) getMeasuredWidth(), (float) getMeasuredHeight(), shadowPaint);
}
canvas.restore();
}
/**
* Draws the Legend-form at the given position with the color at the given
* index.
*
* @param c canvas to draw with
* @param x position
* @param y position
* @param index the index of the color to use (in the colors array)
*/
protected void drawForm(Canvas c, float x, float y, int index, Legend legend) {
if (legend.getColors()[index] == ColorTemplate.COLOR_SKIP)
return;
mLegendFormPaint.setColor(legend.getColors()[index]);
float formsize = legend.getFormSize();
float half = formsize / 2f;
switch (legend.getForm()) {
case CIRCLE:
c.drawCircle(x + half, y, half, mLegendFormPaint);
break;
case SQUARE:
c.drawRect(x, y - half, x + formsize, y + half, mLegendFormPaint);
break;
case LINE:
c.drawLine(x, y, x + formsize, y, mLegendFormPaint);
break;
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPath.reset();
//贝塞尔曲线
mPath.moveTo(mStartXPoint, mStartYPoint);
mPath.quadTo(mConXPoint, mConYPoint, mEndXPoint, mEndYPoint);
canvas.drawPath(mPath, mPaint);
//辅助线
canvas.drawLine(mStartXPoint, mStartYPoint, mConXPoint, mConYPoint, mLinePaint);
canvas.drawLine(mConXPoint, mConYPoint, mEndXPoint, mEndYPoint, mLinePaint);
//文字
canvas.drawPoint(mStartXPoint, mStartYPoint, mPaint);
canvas.drawText("起始点", mStartXPoint, mStartYPoint + 30, mTextPaint);
canvas.drawPoint(mEndXPoint, mEndYPoint, mPaint);
canvas.drawText("结束点", mEndXPoint, mEndYPoint + 30, mTextPaint);
canvas.drawPoint(mConXPoint, mConYPoint, mPaint);
canvas.drawText("控制点", mConXPoint, mConYPoint - 30, mTextPaint);
}
private void drawRuleOfThirdsGuidelines(Canvas canvas) {
final float left = Edge.LEFT.getCoordinate();
final float top = Edge.TOP.getCoordinate();
final float right = Edge.RIGHT.getCoordinate();
final float bottom = Edge.BOTTOM.getCoordinate();
// Draw vertical guidelines.
final float oneThirdCropWidth = Edge.getWidth() / 3;
final float x1 = left + oneThirdCropWidth;
canvas.drawLine(x1, top, x1, bottom, mGuidelinePaint);
final float x2 = right - oneThirdCropWidth;
canvas.drawLine(x2, top, x2, bottom, mGuidelinePaint);
// Draw horizontal guidelines.
final float oneThirdCropHeight = Edge.getHeight() / 3;
final float y1 = top + oneThirdCropHeight;
canvas.drawLine(left, y1, right, y1, mGuidelinePaint);
final float y2 = bottom - oneThirdCropHeight;
canvas.drawLine(left, y2, right, y2, mGuidelinePaint);
}
/**
* Draws the Legend-form at the given position with the color at the given
* index.
*
* @param c canvas to draw with
* @param x position
* @param y position
* @param index the index of the color to use (in the colors array)
*/
protected void drawForm(Canvas c, float x, float y, int index, Legend legend) {
if (legend.getColors()[index] == ColorTemplate.COLOR_SKIP)
return;
mLegendFormPaint.setColor(legend.getColors()[index]);
float formsize = legend.getFormSize();
float half = formsize / 2f;
switch (legend.getForm()) {
case CIRCLE:
c.drawCircle(x + half, y, half, mLegendFormPaint);
break;
case SQUARE:
c.drawRect(x, y - half, x + formsize, y + half, mLegendFormPaint);
break;
case LINE:
c.drawLine(x, y, x + formsize, y, mLegendFormPaint);
break;
}
}
@Override
protected void onDraw(Canvas canvas) {
canvas.translate(pointerShadowRadius, getHeight() / 2);
canvas.save();
pointerPath.reset();
pointerPath.addCircle(pointerPosition.x, pointerPosition.y, pointerRadius, Path.Direction.CW);
pointerPath.close();
canvas.clipPath(pointerPath, Region.Op.DIFFERENCE);
canvas.drawLine(0, 0, length, 0, paint);
canvas.drawCircle(pointerPosition.x, pointerPosition.y, pointerShadowRadius, pointerShadowPaint);
canvas.restore();
canvas.drawCircle(pointerPosition.x, pointerPosition.y, pointerRadius, pointerPaint);
}
@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
int width = parent.getWidth();
int top;
int childCount = parent.getChildCount() - (single ? 0 : 1);
for (int i = 0; i < childCount; i++) {
View child = parent.getChildAt(i);
View nextChild = i < childCount - 1 ? parent.getChildAt(i + 1) : null;
int position = parent.getChildAdapterPosition(child);
if (position < skipRows || child instanceof GraySectionCell || nextChild instanceof GraySectionCell) {
continue;
}
top = child.getBottom();
canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(72), top, width - (LocaleController.isRTL ? AndroidUtilities.dp(72) : 0), top, Theme.dividerPaint);
}
}
private void a(Canvas canvas, double d, int i, int j, double d1,
boolean flag, Paint paint)
{
double d2 = Math.toRadians(90D);
int k = (int)(10D * Math.sin(d - d2));
int l = (int)(10D * Math.cos(d - d2));
int i1 = (int)(d1 * Math.sin(d));
int j1 = (int)(d1 * Math.cos(d));
int k1 = i + i1;
int l1 = j + j1;
float af[];
if (flag)
{
int i2 = i + (int)(0.84999999999999998D * d1 * Math.sin(d));
int j2 = j + (int)(0.84999999999999998D * d1 * Math.cos(d));
float af1[] = new float[6];
af1[0] = i2 - k;
af1[1] = j2 - l;
af1[2] = k1;
af1[3] = l1;
af1[4] = i2 + k;
af1[5] = l + j2;
float f = paint.getStrokeWidth();
paint.setStrokeWidth(5F);
canvas.drawLine(i, j, k1, l1, paint);
paint.setStrokeWidth(f);
af = af1;
} else
{
af = new float[6];
af[0] = i - k;
af[1] = j - l;
af[2] = k1;
af[3] = l1;
af[4] = k + i;
af[5] = l + j;
}
drawPath(canvas, af, paint, true);
}
@Override
public void draw(View view, Canvas canvas) {
super.draw(view, canvas);
final int color;
if (view instanceof ViewGroup) {
color = CONTAINER_COLOR;
} else {
color = VIEW_COLOR;
}
mBorderPaint.setColor(color);
final int width = view.getWidth();
final int height = view.getHeight();
final float lineWidth = Math.min(mMaxBorderSize, width * BORDER_SIZE_RATIO);
final float lineHeight = Math.min(mMaxBorderSize, height * BORDER_SIZE_RATIO);
canvas.drawLine(0, 0, lineHeight, 0, mBorderPaint);
canvas.drawLine(0, 0, 0, lineHeight, mBorderPaint);
canvas.drawLine(0, height, 0, height - lineHeight, mBorderPaint);
canvas.drawLine(0, height, lineWidth, height, mBorderPaint);
canvas.drawLine(width, 0, width, lineHeight, mBorderPaint);
canvas.drawLine(width, 0, width - lineWidth, 0, mBorderPaint);
canvas.drawLine(width, height, width, height - lineHeight, mBorderPaint);
canvas.drawLine(width, height, width - lineWidth, height, mBorderPaint);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top,
int y, int bottom, Paint paint) {
canvas.drawText(text, start, end, x, y, paint);
if (!mLengthIsCached) {
mSpanLength = paint.measureText(mSpan);
}
int yOffset = 24;
canvas.drawLine(x, y + yOffset, x + mSpanLength, y + yOffset, this.mPaint);
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
int width = getWidth();
int height = getHeight();
if (frameSize == 1f) {
paint.setStrokeWidth(frameThickness * 2);
Rect frame = new Rect();
getDrawingRect(frame);
canvas.drawRect(frame, paint);
} else {
paint.setStrokeWidth(frameThickness);
// Left, Top
canvas.drawLine(0, offset, width * frameSize, offset, paint);
canvas.drawLine(offset, frameThickness, offset, height * frameSize, paint);
// Right, Top
canvas.drawLine(width - width * frameSize, offset, width, offset, paint);
canvas.drawLine(width - offset, frameThickness, width - offset, height * frameSize, paint);
// Right, Bottom
canvas.drawLine(width - width * frameSize, height - offset, width, height - offset, paint);
canvas.drawLine(height - offset, height - height * frameSize, width - offset, height - (frameThickness), paint);
// Left, Bottom
canvas.drawLine(0, height - offset, width * frameSize, height - offset, paint);
canvas.drawLine(offset, height - height * frameSize, offset, height - (frameThickness), paint);
}
}
@Override
public void onDraw(Canvas canvas, Rect bounds) {
mTime.setToNow();
// Draw the background.
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
/*
* These calculations reflect the rotation in degrees per unit of
* time, e.g. 360 / 60 = 6 and 360 / 12 = 30
*/
final float secondsRotation = mTime.second * 6f;
final float minutesRotation = mTime.minute * 6f;
// account for the offset of the hour hand due to minutes of the hour.
final float hourHandOffset = mTime.minute / 2f;
final float hoursRotation = (mTime.hour * 30) + hourHandOffset;
// save the canvas state before we begin to rotate it
canvas.save();
canvas.rotate(hoursRotation, mCenterX, mCenterY);
canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mHourHandLength, mHandPaint);
canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mMinuteHandLength, mHandPaint);
if (!mAmbient) {
canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
canvas.drawLine(mCenterX, mCenterY, mCenterX, mCenterY - mSecondHandLength,
mHandPaint);
}
// restore the canvas' original orientation.
canvas.restore();
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawLine(getPaddingLeft(), AndroidUtilities.dp(8), getWidth() - getPaddingRight(), AndroidUtilities.dp(8), Theme.dividerPaint);
}
private void a(Canvas canvas, Paint paint, float f, float f1)
{
canvas.drawLine(f - c, f1 - c, f + c, f1 + c, paint);
canvas.drawLine(f + c, f1 - c, f - c, f1 + c, paint);
}
@Override
public void onDraw(Canvas canvas) {
int viewWidth = getWidth();
if (viewWidth == 0 || !mIsInitialized) {
return;
}
if (!mDrawValuesReady) {
mXCenter = getWidth() / 2;
mYCenter = getHeight() / 2;
mCircleRadius = (int) (Math.min(mXCenter, mYCenter) * mCircleRadiusMultiplier);
if (!mIs24HourMode) {
// We'll need to draw the AM/PM circles, so the main circle will need to have
// a slightly higher center. To keep the entire view centered vertically, we'll
// have to push it up by half the radius of the AM/PM circles.
int amPmCircleRadius = (int) (mCircleRadius * mAmPmCircleRadiusMultiplier);
mYCenter -= amPmCircleRadius * 0.75;
}
mSelectionRadius = (int) (mCircleRadius * mSelectionRadiusMultiplier);
mDrawValuesReady = true;
}
// Calculate the current radius at which to place the selection circle.
mLineLength = (int) (mCircleRadius * mNumbersRadiusMultiplier * mAnimationRadiusMultiplier);
int pointX = mXCenter + (int) (mLineLength * Math.sin(mSelectionRadians));
int pointY = mYCenter - (int) (mLineLength * Math.cos(mSelectionRadians));
// Draw the selection circle.
mPaint.setAlpha(mSelectionAlpha);
canvas.drawCircle(pointX, pointY, mSelectionRadius, mPaint);
if (mForceDrawDot | mSelectionDegrees % 30 != 0) {
// We're not on a direct tick (or we've been told to draw the dot anyway).
mPaint.setAlpha(FULL_ALPHA);
canvas.drawCircle(pointX, pointY, (mSelectionRadius * 2 / 7), mPaint);
} else {
// We're not drawing the dot, so shorten the line to only go as far as the edge of the
// selection circle.
int lineLength = mLineLength;
lineLength -= mSelectionRadius;
pointX = mXCenter + (int) (lineLength * Math.sin(mSelectionRadians));
pointY = mYCenter - (int) (lineLength * Math.cos(mSelectionRadians));
}
// Draw the line from the center of the circle.
mPaint.setAlpha(255);
mPaint.setStrokeWidth(4);
canvas.drawLine(mXCenter, mYCenter, pointX, pointY, mPaint);
}
@Override
protected void onDraw(Canvas canvas) {
if (needDivider) {
canvas.drawLine(getPaddingLeft(), getHeight() - 1, getWidth() - getPaddingRight(), getHeight() - 1, Theme.dividerPaint);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// shadow.
float startAngle = 270 - ARC_ANGLE / 2f;
float progressSweepAngleDay = (float) (1.0 * progresses[0] / maxes[0] * ARC_ANGLE);
float progressEndAngleDay = startAngle + progressSweepAngleDay;
float progressSweepAngleNight = (float) (1.0 * progresses[1] / maxes[1] * ARC_ANGLE);
float progressEndAngleNight = startAngle + progressSweepAngleNight;
if (progressEndAngleDay == progressEndAngleNight) {
drawShadow(canvas, 0, progressEndAngleDay, x2ShaderWrapper.getShader());
} else if (progressEndAngleDay > progressEndAngleNight) {
drawShadow(canvas, 0, progressEndAngleDay, x1ShaderWrapper.getShader());
drawShadow(canvas, 1, progressEndAngleNight, x2ShaderWrapper.getShader());
} else { // progressEndAngleDay < progressEndAngleNight
drawShadow(canvas, 1, progressEndAngleNight, x1ShaderWrapper.getShader());
drawShadow(canvas, 0, progressEndAngleDay, x2ShaderWrapper.getShader());
}
// sub line.
paint.setColor(lineColors[2]);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(dottedLineSize);
paint.setPathEffect(effect);
canvas.drawArc(rectF, startAngle, ARC_ANGLE, false, paint);
canvas.drawLine(
margin,
getMeasuredHeight() - margin,
getMeasuredWidth() - margin,
getMeasuredHeight() - margin,
paint
);
// path.
drawPathLine(canvas, 1, startAngle, (float) (1.0 * progresses[1] / maxes[1] * ARC_ANGLE));
drawPathLine(canvas, 0, startAngle, (float) (1.0 * progresses[0] / maxes[0] * ARC_ANGLE));
int restoreCount;
// icon.
for (int i = 1; i >= 0; i --) {
if (iconDrawables[i] == null || progresses[i] <= 0) {
continue;
}
restoreCount = canvas.save();
canvas.translate(iconPositions[i][0], iconPositions[i][1]);
canvas.rotate(iconRotations[i], iconSize / 2f, iconSize / 2f);
iconDrawables[i].draw(canvas);
canvas.restoreToCount(restoreCount);
}
}
@Override
protected void onDraw(Canvas canvas) {
if (needDivider && Theme.dividerPaint != null) {
canvas.drawLine(LocaleController.isRTL ? 0 : AndroidUtilities.dp(imageView.getVisibility() == VISIBLE ? 71 : 20), getMeasuredHeight() - 1, getMeasuredWidth() - (LocaleController.isRTL ? AndroidUtilities.dp(imageView.getVisibility() == VISIBLE ? 71 : 20) : 0), getMeasuredHeight() - 1, Theme.dividerPaint);
}
}
protected void drawVisualWaveOnSeek(Canvas canvas, int millisecond) {
// Tại thời điểm T, tương ứng với đoạn :
float CurrentSeekPos = WaveLength / (mSongDeclareDuration + 0.0f) * millisecond;
// Chiều rộng của view là mWidth
float move;
// Nếu như SeekPos nhỏ hơn một nửa độ rộng, ta không cần di chuyển
if (CurrentSeekPos <= mWidth / 2) {
move = 0;
} else { // còn nếu SeekPos lớn hơn một nửa độ rộng
// Ta cần dịch sang trái một đoạn sao cho SeekPos ở chính giữa theo chiều ngang
move = CurrentSeekPos - mWidth / 2;
}
// Tiếp theo ta vẽ đoạn visual có thể thấy đc lên view
// Nghĩa là vẽ đoạn visual từ vị trí move tới vị trí SeekPos + mWidth /2
// Ta tính xem những Pen nào thì sẽ xuất hiện trong view, và ta chỉ vẽ chúng mà thôi
int firstPen = (int) (move / PenWidth);
int midPen = (int) (CurrentSeekPos / PenWidth);
int endPen = (int) ((move + mWidth) / PenWidth);
for (int i = firstPen; i <= endPen && i != getTotalPens() - 1; i++) {
// càng cách xa midPen, kích thước càng nhỏ
float runtime_height;
// chiều dài thường (0 -> 1)
float pen_pos = -move + PenWidth * i + PenWidth / 2;
// 1 <-> 0 <-> 1
float delta = (float) ((pen_pos < mWidth / 2f) ? (Math.pow((mWidth / 2f - pen_pos) / (mWidth / 2f), 3)) : (Math.pow((pen_pos - mWidth / 2f) / (mWidth / 2), 3)));
// float delta = (float) ((pen_pos<mWidth/2) ?(Math.sqrt((mWidth/2 - pen_pos)/(mWidth/2))) : Math.sqrt((pen_pos- mWidth/2)/(mWidth/2)));
/*
runtime_height = (float) (lineHeight
- on_seek_animate_value //gia tăng theo animation
*delta // gia tăng theo vị trí pen, càng xa tâm thì càng tiến gần 1
*(lineHeight - min_seek_height)); // hằng số
*/
float min_max_delta = max_seek_height - min_seek_height;
// ở chính tâm thì có độ lớn là max_seek_height,
// ở ngoài cùng thì có độ lớn là min_seek_height
runtime_height = max_seek_height - delta * min_max_delta;
runtime_height = (runtime_height - lineHeight) * on_seek_animate_value + lineHeight;
double alpha_delta = 1 - Math.pow(delta, 4) * 0.85 * on_seek_animate_value;
// double alpha_delta =1 - Math.pow(delta,4)*0.85;
if (alpha_delta > 1) alpha_delta = 1;
mActivePaint.setAlpha((int) (255 * alpha_delta));
mTranslucentActivePaint.setAlpha((int) (255 * amount_translucent_paint * alpha_delta));
mHidePaint.setAlpha((int) (255 * amount_hide_paint * alpha_delta));
mTranslucentHidePaint.setAlpha((int) (255 * amount_translucent_hide_paint * alpha_delta));
if (i < midPen) {
canvas.drawLine(
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y - mOneDp),
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y - mOneDp - runtime_height * getSmoothedPenGain(i)),
mActivePaint);
canvas.drawLine(
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y + mOneDp),
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y + mOneDp + runtime_height * getSmoothedPenGain(i)),
mTranslucentActivePaint);
} else if (i > midPen) {
canvas.drawLine(
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y - mOneDp),
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y - mOneDp - runtime_height * getSmoothedPenGain(i)),
mHidePaint);
canvas.drawLine(
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y + mOneDp),
(float) (-move + PenWidth * i + PenWidth / 2),
(float) (mSeekBarCenter.Y + mOneDp + runtime_height * getSmoothedPenGain(i)),
mTranslucentHidePaint);
} else {
if (midPen * PenWidth + PenWidth - mPenDistance / 2 > CurrentSeekPos) {
canvas.drawRect(
-move + CurrentSeekPos,
(float) (mSeekBarCenter.Y - mOneDp - runtime_height * getSmoothedPenGain(i)),
(float) (-move + midPen * PenWidth + PenWidth - mPenDistance / 2),
(float) mSeekBarCenter.Y - mOneDp,
mHidePaint
);
canvas.drawRect(
-move + CurrentSeekPos,
(float) mSeekBarCenter.Y + mOneDp,
(float) (-move + midPen * PenWidth + PenWidth - mPenDistance / 2),
(float) (mSeekBarCenter.Y + mOneDp + runtime_height * getSmoothedPenGain(i)),
mTranslucentHidePaint
);
}
if (midPen * PenWidth + mPenDistance / 2 < CurrentSeekPos) {
canvas.drawRect(
(float) (-move + PenWidth * midPen + mPenDistance / 2),
(float) (mSeekBarCenter.Y - mOneDp - runtime_height * getSmoothedPenGain(i)),
(float) (-move + CurrentSeekPos),
(float) mSeekBarCenter.Y - mOneDp,
mActivePaint);
canvas.drawRect(
(float) (-move + PenWidth * midPen + mPenDistance / 2),
(float) mSeekBarCenter.Y + mOneDp,
-move + CurrentSeekPos,
(float) (mSeekBarCenter.Y + mOneDp + runtime_height * getSmoothedPenGain(i)),
mTranslucentActivePaint);
}
}
}
}