如何通过编程将rgb代码转换为十六进制并设置为android中文本视图或按钮的背景色

如何通过编程将rgb代码转换为十六进制并设置为android中文本视图或按钮的背景色,android,textview,background-color,Android,Textview,Background Color,这是我在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 fl

这是我在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 color(当前为像素变量)获取十六进制颜色,请执行以下操作:

将十六进制颜色应用于
视图

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

为了显示我建议您使用的返回
字符串的值

  String toShow = Integer.toHexString(color)

你为什么需要这个魔咒?int还不够吗?因为我想向用户展示,而开发十六进制值是最常用的。
  String toShow = Integer.toHexString(color)