在运行时绘制HSV圆

时间:2012-11-07 09:28:31

标签: android android-layout

我想在我的应用中有一个颜色选择器,看起来像这样

Something like this

我尝试逐像素填充位图,尝试使用带有drawArc()方法的画布。两种方式都不利于表现。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

可能不是它应该是什么(与颜色,饱和度等有关),但here is something开始看起来像你想要的......

import android.graphics.*;
import android.graphics.drawable.Drawable;

public class HSV_Circle extends Drawable {
    Paint p = new Paint();

    @Override
    public void draw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int min = Math.min(width,height);

        RadialGradient radial_gradient = new RadialGradient(width/2, height/2, min/2, 0xFFFFFFFF,
            0x00FFFFFF, android.graphics.Shader.TileMode.CLAMP);

        int colors[] = new int[13];
        float hsv[] = new float[3];
        hsv[1]=1;
        hsv[2]=1;
        for (int i=0; i<12; i++) {
            hsv[0] = (360 / 12) * i;
            colors[i] = Color.HSVToColor(hsv);
        }
        colors[12] = colors[0];

        SweepGradient sweep_gradient = new SweepGradient(width/2, height/2, colors, null);

        ComposeShader shader = new ComposeShader(sweep_gradient, radial_gradient, PorterDuff.Mode.SRC_OVER);

        p.setDither(true);
        p.setShader(shader);

        canvas.drawCircle(width/2, height/2, min/2, p);
    }

    @Override
    public void setAlpha(int i) {
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}