在Android Rect中设置像素颜色

时间:2017-03-23 01:26:10

标签: android android-canvas android-view android-color

我创建了一个简单的Android视图来绘制矩形。

    private class Rectangle extends View{
    Paint paint = new Paint();
    public Rectangle(Context context) {
        super(context);
    }
    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.GREEN);
        Rect rect = new Rect(0, 0,100, 100);
        canvas.drawRect(rect, paint );
    }
}

我如何创建一个模式来填充矩形而不是使用纯色。例如,我想沿x方向绘制颜色的正弦变化?

1 个答案:

答案 0 :(得分:0)

请查看以下更新的onDraw():

    @Override
public void onDraw(Canvas canvas) {
    paint.setColor(Color.BLACK);
    paint.setStrokeWidth(3);
    canvas.drawRect(30, 30, 80, 80, paint);
    paint.setStrokeWidth(0);
    paint.setColor(Color.CYAN);
    canvas.drawRect(33, 60, 77, 77, paint );
    paint.setColor(Color.YELLOW);
    canvas.drawRect(33, 33, 77, 60, paint );

}

然后这样:

enter image description here