帆布绘图与渐变填充和弯曲的形状

时间:2018-05-31 07:05:48

标签: android android-layout android-canvas draw curve

我的问题标题似乎很常见..但这与其他问题不同。我尝试了很多解决方案,但还没有能够获得完美的解决方案。也无法理解绘图功能。

这是我到目前为止所尝试的:

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

}

我的输出:

enter image description here

所以我的问题是:

  1. 如果有人可以解释其要点或示例代码,我想按照预期的输出绘制曲线。
  2. 想知道左侧,上方,右侧,底部的绘制弧线(我已经阅读了文档,但它只是告诉它们没有浮动没有描述)。
  3. 我想用渐变颜色填充这个形状,只有这个形状不是整个画布。
  4. 任何建议将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:3)

使用路径您可以绘制贝塞尔路径。

检查我的努力如下。

 MyDrawView myDrawView = new MyDrawView(this);
 frmCustom.addView(myDrawView);

初始化此自定义视图并添加框架布局。像,

myBall