如何从给定的颜色代码中了解位图 getPixel(x,y)

How to know about bitmap getPixel(x,y) from given color code

我知道我们可以从 bitmap.getPixel(x,y) 方法中获取颜色,但我没有 x,y,我想从给定的颜色代码中获取 x,y。

当第一次调用 onDraw 方法时,我想用默认颜色在垂直选择器上画线,但那时我没有 x,y,所以无法调用 getPixel(x,y),因为用户-没有发生互动。

public class VerticalSlideColorPicker extends View {

private String TAG = VerticalSlideColorPicker.class.getName();
private Paint paint;
private Paint strokePaint;
private Path path;
private Bitmap bitmap;
private int viewWidth;
private int viewHeight;
private int centerX;
private float colorPickerRadius;
private OnColorChangeListener onColorChangeListener;
private RectF colorPickerBody;
private float selectorYPos;
private int borderColor;
private float borderWidth;
private int[] colors;
private boolean cacheBitmap = true;
private Context mContext;

public VerticalSlideColorPicker(Context context) {
    super(context);
    mContext = context;
    init();
}

public VerticalSlideColorPicker(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.getTheme().obtainStyledAttributes(
            attrs,
            R.styleable.VerticalSlideColorPicker,
            0, 0);

    try {
        borderColor = a.getColor(R.styleable.VerticalSlideColorPicker_borderColor, Color.WHITE);
        borderWidth = a.getDimension(R.styleable.VerticalSlideColorPicker_borderWidth, 10f);
        int colorsResourceId = a.getResourceId(R.styleable.VerticalSlideColorPicker_colors, R.array.default_colors);
        colors = a.getResources().getIntArray(colorsResourceId);
    }
    finally {
        a.recycle();
    }
    init();
}

public VerticalSlideColorPicker(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

public VerticalSlideColorPicker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void init() {
    setWillNotDraw(false);
    paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);

    path = new Path();

    strokePaint = new Paint();
    strokePaint.setStyle(Paint.Style.STROKE);
    strokePaint.setColor(borderColor);
    strokePaint.setAntiAlias(true);
    strokePaint.setStrokeWidth(borderWidth);

    setDrawingCacheEnabled(true);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    path.addRect(colorPickerBody, Path.Direction.CW);
    path.addRect(colorPickerBody, Path.Direction.CW);

    canvas.drawPath(path, strokePaint);
    canvas.drawPath(path, paint);

    if (cacheBitmap) {
        bitmap = getDrawingCache();
        cacheBitmap = false;

        setColor(ContextCompat.getColor(getContext(), R.color.tag_layout_border_audioyes_darkblue));
        //invalidate();
    }
    else {

        canvas.drawLine(colorPickerBody.left, selectorYPos, colorPickerBody.right, selectorYPos, strokePaint);
    }

}
/**
 * Set the color this view should show.
 *
 * @param color The color that should be selected. #argb
 */
public void setColor(int color) {

    /*int[] pixels = new int[bitmap.getHeight()*bitmap.getWidth()];
    bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    for (int i=0; i<bitmap.getWidth()*5; i++)
        pixels[i] = ContextCompat.getColor(getContext(), R.color.blue_picker);
    bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());*/

   /* int alpha = Color.alpha(color);
    int red = Color.red(color);
    int blue = Color.blue(color);
    int green = Color.green(color);

    float[] hsv = new float[3];

    Color.RGBToHSV(red, green, blue, hsv);*/
  /* selectorYPos = 584;
    int selectedColor = bitmap.getPixel(viewWidth / 2, (int) selectorYPos);*/


  /*  this.alpha = alpha;
    hue = hsv[0];
    sat = hsv[1];
    val = hsv[2];*/

    if (onColorChangeListener != null) {
        onColorChangeListener.onColorChange(color);
    }

    invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    float yPos = Math.min(event.getY(), colorPickerBody.bottom);
    android.util.Log.e(TAG, "Float :" + yPos);
    yPos = Math.max(colorPickerBody.top, yPos);
    android.util.Log.e(TAG, "Normal :" + yPos);


    selectorYPos = yPos;
    int selectedColor = bitmap.getPixel(viewWidth / 2, (int) selectorYPos);
    android.util.Log.e(TAG, "Color :" + selectedColor);
    if (onColorChangeListener != null) {
        onColorChangeListener.onColorChange(selectedColor);
    }
    invalidate();

    return true;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    viewWidth = w;
    viewHeight = h;

    centerX = viewWidth / 2;
    colorPickerRadius = (viewWidth / 2) - borderWidth;

    colorPickerBody = new RectF(centerX - colorPickerRadius, borderWidth + colorPickerRadius, centerX + colorPickerRadius, viewHeight - (borderWidth + colorPickerRadius));

    LinearGradient gradient = new LinearGradient(colorPickerBody.left, colorPickerBody.top,
            colorPickerBody.right,
            colorPickerBody.bottom, colors, null, Shader.TileMode.CLAMP);

    paint.setShader(gradient);

    resetToDefault();
}

public void setBorderColor(int borderColor) {
    this.borderColor = borderColor;
    invalidate();
}

public void setBorderWidth(float borderWidth) {
    this.borderWidth = borderWidth;
    invalidate();
}

public void setColors(int[] colors) {
    this.colors = colors;
    cacheBitmap = true;
    invalidate();
}

public void resetToDefault() {
    selectorYPos = borderWidth + colorPickerRadius;

    if (onColorChangeListener != null) {
        onColorChangeListener.onColorChange(Color.TRANSPARENT);
    }
    invalidate();
}

public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) {
    this.onColorChangeListener = onColorChangeListener;
}

}

暴力解决方案可能如下:

private List<Point> getPixelswithColor(Bitmap bitmap, int colorId) {
    List<Point> pixels = new ArrayList();
    for (int x = 0; x < bitmap.getWidth(); x++) {
        for (int y = 0; y < bitmap.getHeight(); y++) {
            if (bitmap.getPixel(x, y) == colorId) {
                pixels.add(new Point(x, y));
            }
        }
    }
    return pixels;
}