Java 其中一个RGB值始终为255-ANDROID

Java 其中一个RGB值始终为255-ANDROID,java,android,colors,bluetooth,rgb,Java,Android,Colors,Bluetooth,Rgb,我编写了一个简单的应用程序,可以显示图像中触摸颜色的RGB值。 问题是,每次我触摸图像时,其中一个RGB值是255。 例如我应该有值#F0F0F0我有#FFF0F0或#F0FFF0 这是我的密码: iv=(ImageView)findViewById(R.id.imageView1); mTextLog=(TextView)findViewById(R.id.textView3); iv.setOnTouchListener(新的OnTouchListener(){ int x=0,y=0; 浮

我编写了一个简单的应用程序,可以显示图像中触摸颜色的RGB值。 问题是,每次我触摸图像时,其中一个RGB值是255。 例如我应该有值#F0F0F0我有#FFF0F0或#F0FFF0

这是我的密码:

iv=(ImageView)findViewById(R.id.imageView1);
mTextLog=(TextView)findViewById(R.id.textView3);
iv.setOnTouchListener(新的OnTouchListener(){
int x=0,y=0;
浮动汇率,fy;
公共布尔onTouch(视图v,运动事件){
ImageView ImageView=((ImageView)v);
位图位图=((BitmapDrawable)imageView.getDrawable()).getBitmap();
v、 getWidth();
v、 getHeight();
bitmap.getWidth();
bitmap.getHeight();
fx=((event.getX()/656)*960);
fy=((event.getY()/721)*1029);
如果(fx>950)fx=950;
如果(fy>1000)fy=1000;
如果(fx<32)fx=32;
x=数学圆(fx);
y=数学圆(fy);
如果(外汇>0&&fy>0){
int pixel=bitmap.getPixel(x,y);
int redValue=Color.red(像素);
int blueValue=颜色。蓝色(像素);
int greenValue=颜色。绿色(像素);
如果(redValue>255)redValue=255;
如果(redValue<0)redValue=0;
如果(greenValue>255)greenValue=255;
如果(greenValue<0)greenValue=0;
如果(blueValue>255)blueValue=255;
如果(blueValue<0)blueValue=0;
br=(字节)redValue;
bg=(字节)绿色值;
bb=(字节)蓝色值;
tv2.setText(“红色:“+redValue+”绿色:“+greenValue+”蓝色:“+blueValue”);
RelativeLayout rl=(RelativeLayout)findViewById(R.id.idd);
rl.setBackgroundColor(像素);
另一个问题是,当我在屏幕上移动手指时,改变背景颜色是可以的,但当我试图通过蓝牙将其发送到microkontroler时,出现了一个问题。 如果我触摸黑色两次,它首先发出黑色,然后发出蓝色。:O

只有当我从这个onTouch方法返回true时,才会发生这种情况

如果有任何帮助,我将不胜感激

顺便说一句。对不起我的英语。

我在你的代码中看到一些冲突(奇怪!)。首先,如果你正确计算了颜色,那么没有必要检查大于255或小于0的值,我指的是这些行

    if(redValue > 255) redValue = 255;
    if(redValue < 0) redValue = 0;
    if(greenValue > 255) greenValue = 255;
    if(greenValue < 0) greenValue = 0;
    if(blueValue > 255) blueValue = 255;
    if(blueValue < 0) blueValue = 0;

请尝试一下,然后回来报告,我们需要修复它。

我更改了几行,问题仍然存在

我的文本视图向我展示了fe。它是粉红色的:“A:-1R:-1G:25B:-7”。 如果我触摸的不是白色,那么3个RGB值中的2个不可能等于-1

这是我用来给微控制器发送颜色的代码。 “H”是我的“钥匙”,uC理解为“现在将有一个RGB值”

if(mConnectedThread!=null){

当我从这个onTouch方法返回“false”时,没有任何错误发生。 我只想触摸和移动我的手指来实时改变RGB灯的颜色。 这是我在应用程序中使用的图像。

byte[] hcolor;
hcolor=ByteBuffer.allocate(4).putInt(pixel).array();
out(hcolor[0]);//the first byte represent the alpha, so it should be 255.
          int pixel = bitmap.getPixel(x, y);
          int redValue = Color.red(pixel);
          int blueValue = Color.blue(pixel);
          int greenValue = Color.green(pixel);
          hcolor = ByteBuffer.allocate(4).putInt(pixel).array();
          br = (byte) redValue;
          bg = (byte) greenValue;
          bb = (byte) blueValue;

          //tv2.setText("Red: " + redValue + " Green: " + greenValue >+ " Blue: " + blueValue);
          tv2.setText("A: " + hcolor[0] + " R: " + hcolor[1] + " G: " >+ hcolor[2] + " B: " + hcolor[3]);
              mConnectedThread.write(h);
              try {
                  Thread.sleep(5);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              mConnectedThread.write(br);
              try {
                  Thread.sleep(5);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              mConnectedThread.write(bg);
              try {
                  Thread.sleep(5);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              mConnectedThread.write(bb);
              try {
                  Thread.sleep(5);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }