private static final int SIN_HEIGHT = (int) Util.dp2px(100);
private static final int SIN_PERIOD = (int) Util.dp2px(200);
private void setPathData() {
// 1、复位 清空一次路径
path.reset();
int offset = 0;
int length = SIN_PERIOD*2;
int linePos = getWidth()/2; // 正弦函数中线在X轴上的位置
for (int i = 0; i < length/SIN_PERIOD; i++) {
int y0 = offset + i*SIN_PERIOD;
int x0 = linePos;
int y1 = SIN_PERIOD/4 + y0;
int x1 = x0+SIN_HEIGHT/2;
int y2 = y0 + SIN_PERIOD/2;
int x2 = linePos;
int y3 = SIN_PERIOD*3/4+y0;
int x3 = linePos-SIN_HEIGHT/2;
int y4 = y0+SIN_PERIOD;
int x4 = linePos;
path.moveTo(x0, y0);
path.quadTo(x1, y1, x2, y2);
path.quadTo(x3, y3, x4, y4);
}
// path.close();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
setPathData();
canvas.drawPath(path, paint2);
int left = (int) (rightLimit - bitmapMarginRight - imageWidth);
if (left < 0) left = 0;
@SuppressLint("DrawAllocation")
Rect rect = new Rect(left, getHeight() / 2 - imageHeight / 2, (int) (rightLimit - bitmapMarginRight), getHeight() / 2 + imageHeight / 2);
canvas.drawBitmap(bitmap, null, rect, null);
}
评论