Canvas 使用渐变填充和曲线形状绘制
Canvas drawing with gradient fill and curvy shape
我的问题标题似乎很普通..但这与其他问题不同..我尝试了很多解决方案但仍无法获得完美的解决方案。也无法理解绘图功能。
这是我迄今为止尝试过的方法:
public class DrawingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawing);
ImageView img=(ImageView) findViewById(R.id.img);
Bitmap bitmap = Bitmap.createBitmap(
1000, // Width
300, // Height
Bitmap.Config.ARGB_8888 // Config
);
// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);
// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);
// Initialize a new Paint instance to draw the line
Paint paint = new Paint();
// Line color
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
// Line width in pixels
paint.setStrokeWidth(8);
paint.setAntiAlias(true);
final RectF oval = new RectF();
// Set a pixels value to offset the line from canvas edge
int offset = 0;
canvas.drawLine(
offset, // startX
canvas.getHeight() / 2, // startY
300, // stopX
canvas.getHeight() / 2, // stopY
paint // Paint
);
oval.set(250, 100, 300,200);
canvas.drawArc(oval, 90, -(float) 90, false,paint);
oval.set(450, 300, 500,350);
canvas.drawArc(oval, 90, -(float) 90, false,paint);
canvas.drawLine(
300, // startX
canvas.getHeight() -50, // startY
1000, // stopX
canvas.getHeight() -50, // stopY
paint // Paint
);
// Display the newly created bitmap on app interface
img.setImageBitmap(bitmap);
}
}
我的输出:
所以我的问题是:
- 如果有人可以解释它的要点或示例代码,我想按照预期的输出绘制曲线。
- 想了解左、上、右、下点的绘制弧线(我已经阅读了文档,但它只是告诉它们是浮动的,没有对它的描述)。
- 我只想用渐变色填充这个形状,这个形状不完整 canvas。
如有任何建议,我们将不胜感激。谢谢!
使用路径可以绘制贝塞尔路径。
检查我的努力如下。
private class MyDrawView extends View {
Paint paint;
Path mPath;
public MyDrawView(Context context) {
this(context, null);
}
public MyDrawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPath = new Path();
mPath.moveTo(200, 200);
mPath.lineTo(200, 100);
mPath.lineTo(600, 100);
mPath.lineTo(600, 300);
mPath.lineTo(300, 300);
mPath.cubicTo(250, 270, 400, 170, 200, 200);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setPathEffect(new CornerPathEffect(10));
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, paint);
invalidate();
}
}
初始化此自定义视图并添加到您的框架布局中。喜欢,
MyDrawView myDrawView = new MyDrawView(this);
frmCustom.addView(myDrawView);
我的问题标题似乎很普通..但这与其他问题不同..我尝试了很多解决方案但仍无法获得完美的解决方案。也无法理解绘图功能。
这是我迄今为止尝试过的方法:
public class DrawingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawing);
ImageView img=(ImageView) findViewById(R.id.img);
Bitmap bitmap = Bitmap.createBitmap(
1000, // Width
300, // Height
Bitmap.Config.ARGB_8888 // Config
);
// Initialize a new Canvas instance
Canvas canvas = new Canvas(bitmap);
// Draw a solid color on the canvas as background
canvas.drawColor(Color.LTGRAY);
// Initialize a new Paint instance to draw the line
Paint paint = new Paint();
// Line color
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
// Line width in pixels
paint.setStrokeWidth(8);
paint.setAntiAlias(true);
final RectF oval = new RectF();
// Set a pixels value to offset the line from canvas edge
int offset = 0;
canvas.drawLine(
offset, // startX
canvas.getHeight() / 2, // startY
300, // stopX
canvas.getHeight() / 2, // stopY
paint // Paint
);
oval.set(250, 100, 300,200);
canvas.drawArc(oval, 90, -(float) 90, false,paint);
oval.set(450, 300, 500,350);
canvas.drawArc(oval, 90, -(float) 90, false,paint);
canvas.drawLine(
300, // startX
canvas.getHeight() -50, // startY
1000, // stopX
canvas.getHeight() -50, // stopY
paint // Paint
);
// Display the newly created bitmap on app interface
img.setImageBitmap(bitmap);
}
}
我的输出:
所以我的问题是:
- 如果有人可以解释它的要点或示例代码,我想按照预期的输出绘制曲线。
- 想了解左、上、右、下点的绘制弧线(我已经阅读了文档,但它只是告诉它们是浮动的,没有对它的描述)。
- 我只想用渐变色填充这个形状,这个形状不完整 canvas。
如有任何建议,我们将不胜感激。谢谢!
使用路径可以绘制贝塞尔路径。
检查我的努力如下。
private class MyDrawView extends View {
Paint paint;
Path mPath;
public MyDrawView(Context context) {
this(context, null);
}
public MyDrawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPath = new Path();
mPath.moveTo(200, 200);
mPath.lineTo(200, 100);
mPath.lineTo(600, 100);
mPath.lineTo(600, 300);
mPath.lineTo(300, 300);
mPath.cubicTo(250, 270, 400, 170, 200, 200);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setPathEffect(new CornerPathEffect(10));
paint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(mPath, paint);
invalidate();
}
}
初始化此自定义视图并添加到您的框架布局中。喜欢,
MyDrawView myDrawView = new MyDrawView(this);
frmCustom.addView(myDrawView);