How do I translate the gradient at runtime without re-creating the shader?

I would like to draw a line with a smooth cut in it. The notch will consist of graduation fading and the gradient fading out. At runtime, I would like to translate this cut along the line in response to user input.

I can draw a gradient using a LinearGradient instance. However, gradient handles are defined at instantiation time. I don't want to recreate the shader on every position change.

int[] colors = { Color.WHITE, Color.TRANSPARENT, Color.TRANSPARENT, Color.WHITE};
cutoutPaint.setShader(new LinearGradient(0, 0, 0, 100, colors, new float[]{0, 0.4f, 0.6f, 1}, Shader.TileMode.CLAMP));

void onDraw(Canvas canvas) {
    canvas.drawLine(..... cutoutPaint);
}

      

My question is, how can I translate this cut along the line without recreating the shader?

+3


source to share





All Articles