简体   繁体   中英

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

Here is my code where i am getting the rgb color code in onTouchListener. In ontouch i get the event x and y position for getting the exact pixel value of the image.

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

To get the hex color from the int color (currently your pixel var):

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

To apply your hex color to a View

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

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

  String toShow = Integer.toHexString(color)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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