Android/Android 개발 소스

View-ImageView-Gradient

로이누리 2019. 2. 20. 13:08

기본적으로 안드로이드에서는 start, center, end 총 3군데의 색상을 지정하여 그라데이션 효과를 얻을 수 있습니다.


하지만 PaintDrawable로 색상 작업을 통해 다양한 색을 그라데이션으로 표현 할 수 있습니다.

ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
float x0 = 0;
float y0 = 0;
float x1 = width;
float y1 = height;
int[] colors = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.WHITE, Color.BLACK};
float[] positions = new float[]{0, 0.2f, 0.4f, 0.6f, 0.8f, 1};

return new LinearGradient(x0, y0, x1, y1, colors, positions, Shader.TileMode.REPEAT);
}
};

PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setShape(new RectShape());
paintDrawable.setShaderFactory(shaderFactory);

binding.gradientImg.setBackground(paintDrawable);