繁体   English   中英

如何以编程方式将rgb代码转换为十六进制并设置为android中textview或button的背景颜色

[英]how to convert rgb code to hex and set as background color for textview or button in android programatically

这是我在 onTouchListener 中获取 rgb 颜色代码的代码。 在ontouch中,我得到事件x和y位置以获取图像的精确像素值。

@Override
public boolean onTouch(View v, MotionEvent event) {

    Matrix inverse = new Matrix();
    picked_imageView.getImageMatrix().invert(inverse);
    float[] touchPoint = new float[] {event.getX(), event.getY()};
    inverse.mapPoints(touchPoint);
    int x = Integer.valueOf((int)touchPoint[0]);
    int y = Integer.valueOf((int)touchPoint[1]);

    //int pixel = bitmap.getPixel(x, y);

    int pixel=((BitmapDrawable)picked_imageView.getDrawable()).getBitmap().getPixel(x,y);

    //then do what you want with the pixel data, e.g
    int redValue = Color.red(pixel);
    int blueValue = Color.blue(pixel);
    int greenValue = Color.green(pixel);
    return false;
}

要从 int 颜色(当前是您的像素变量)获取十六进制颜色:

String hexColor = String.format("#%06X", (0xFFFFFF & pixel));

将十六进制颜色应用于View

view.setBackgroundColor(Color.parseColor(hexColor));

为了显示值,我建议您使用返回String Integer.toHexString(int)

  String toShow = Integer.toHexString(color)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM