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

Android 如何从给定的颜色代码中了解位图getPixel(x,y),android,android-canvas,android-bitmap,ondraw,Android,Android Canvas,Android Bitmap,Ondraw,我知道我们可以从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();

我知道我们可以从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;
}
公共类VerticalSlideColorPicker扩展视图{
私有字符串标记=VerticalSlideColorPicker.class.getName();
私人油漆;
私人涂料;
专用路径;
私有位图;
私有int视图宽度;
私家车内景高度;
私人int centerX;
私人花车;
私有OnColorChangeListener OnColorChangeListener;
专用RectF彩色选择器;
私人浮动选择POS;
私家色彩;
私有浮动边界宽度;
私有int[]颜色;
私有布尔缓存位图=true;
私有上下文;
公共垂直滑块颜色选择器(上下文){
超级(上下文);
mContext=上下文;
init();
}
公共垂直滑块颜色选择器(上下文上下文、属性集属性){
超级(上下文,attrs);
TypedArray a=context.getTheme().ActainStyledAttributes(
属性,
R.styleable.VerticalSlideColorPicker,
0, 0);
试一试{
borderColor=a.getColor(R.styleable.VerticalSlideColorPicker\u borderColor,Color.WHITE);
borderWidth=a.getDimension(R.styleable.VerticalSlideColorPicker_borderWidth,10f);
int colorsResourceId=a.getResourceId(R.styleable.VerticalSlideColorPicker\u colors,R.array.default\u colors);
colors=a.getResources().getIntArray(colorsResourceId);
}
最后{
a、 回收();
}
init();
}
公共垂直滑块颜色选择器(上下文上下文、属性集属性、int defStyleAttr){
super(上下文、attrs、defStyleAttr);
init();
}
public VerticalSlideColorPicker(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
init();
}
私有void init(){
setWillNotDraw(假);
油漆=新油漆();
绘制.设置样式(绘制.样式.填充);
paint.setAntiAlias(真);
路径=新路径();
strokePaint=新油漆();
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(边框颜色);
strokePaint.setAntiAlias(真);
strokePaint.setStrokeWidth(边框宽度);
setDrawingCacheEnabled(真);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
addRect(colorPickerBody,path.Direction.CW);
addRect(colorPickerBody,path.Direction.CW);
画布.drawPath(路径,strokePaint);
画布.绘制路径(路径,绘制);
if(缓存位图){
位图=getDrawingCache();
cacheBitmap=false;
setColor(ContextCompat.getColor(getContext(),R.color.tag_布局_边框_音频是_深蓝色));
//使无效();
}
否则{
画布.抽绳(colorPickerBody.left、selectorYPos、colorPickerBody.right、selectorYPos、strokePaint);
}
}
/**
*设置此视图应显示的颜色。
*
*@param color应选择的颜色。#argb
*/
公共void setColor(int-color){
/*int[]像素=新的int[bitmap.getHeight()*bitmap.getWidth()];
getPixels(像素,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());

对于(int i=0;i而言,蛮力解决方案可以如下所示:

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;
}
私有列表getPixelswithColor(位图位图,int-colorId){
列表像素=新的ArrayList();
对于(int x=0;x
您可以遍历每个(x,y)并按颜色代码对其进行过滤。然后,您会得到一个匹配坐标列表。感谢您对我的帮助。我刚刚将getColor更改为getPixel