在运行时绘制HSV圆 [英] Draw HSV circle at runtime

查看:81
本文介绍了在运行时绘制HSV圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用中使用一个颜色选择器,如下所示

I would like to have a color picker in my app looking like this

我尝试逐像素填充位图,并尝试通过drawArc()方法使用画布.两种方法都不利于性能.

I've tried fill bitmap pixel by pixel, tried use canvas with drawArc() method. Both ways are not good for performance.

有什么想法吗?

推荐答案

它可能不完全是应该的(与颜色,饱和度等有关),但是

It might not be precisely what it should be (related to colors, saturation and so on), but here is something that start to look like what you want....

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

这篇关于在运行时绘制HSV圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆