Android:getPixel(x,y)颜色值有误

时间:2014-01-25 16:32:50

标签: java android rgb getpixel argb

我是一个android / java新手,我正在开发一个简单的应用程序,它将从色轮中选取的颜色产生和谐的色彩。问题是我甚至无法在车轮下方的框中显示正确的颜色(在色轮上触摸黄色会产生紫色:S)我不明白发生了什么。我尝试了几个不同的图像代码,生成的颜色仍然全部关闭..

public class MainActivity extends Activity {

    ImageView cw;

    Bitmap cwBmp;

    ColourView colourArray;


    int redValue;

    int blueValue;

    int greenValue;

    int argb;

    int alpha; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        colourArray = new ColourView(this);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
        rl.addView(colourArray);

        cw = (ImageView) findViewById(R.id.colourwheelpic);

         BitmapDrawable drawable = (BitmapDrawable) cw.getDrawable();
         cwBmp = drawable.getBitmap();

        cw.setOnTouchListener(new ImageView.OnTouchListener(){


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

                int touchX = (int)event.getX();
                  int touchY = (int)event.getY();
                  int pixel;
                  switch (event.getAction()) {
                         case MotionEvent.ACTION_DOWN:
                        touchX = (int)event.getX();
                        touchY = (int)event.getY();

                      case MotionEvent.ACTION_UP:
                      pixel = cwBmp.getPixel(touchX,touchY);
                      redValue = Color.red(pixel);


                    alpha = Color.alpha(pixel);
                    blueValue = Color.blue(pixel);
                        greenValue = Color.green(pixel);


                      argb = Color.argb(alpha, redValue,blueValue,greenValue);


                  colourArray.colors.clear();
                      colourArray.colors.add(argb);
                      colourArray.invalidate();
                      return true;
                  }

                  return false;

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


}

colourArray是ColourView对象

   public class ColourView extends View {

    Paint paint = new Paint();

    ArrayList<Integer> colors = new ArrayList<Integer>();



    public ColourView(Context context) {
        super(context);

        colors.add(Color.WHITE);    

    }

    public void onDraw(Canvas canvas){

        int i = 0;


        while ( i < 220 ){
                paint.setColor(colors.get(0));
                paint.setStrokeWidth(0);
                canvas.drawRect(25+ (44*i), 500,69+(44*i),550, paint);
                i = i + 1;
            }

        }

    private int getRandomInt(){
        int upper = colors.size();
        int r = (int) (Math.random() * (upper));
        return r;

    }


}

这是我的布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

    <ImageView
android:id="@+id/colourwheelpic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/colourwheelbmp" />

    </RelativeLayout>

1 个答案:

答案 0 :(得分:2)

现在,甚至没有阅读你的代码,只是按逻辑:如果你的黄色变为紫色,那么你就错放了GREEN和BLUE RGB组件。

事实上,这个:#ffcccc00是黄色的,这个#ffcc00cc是紫色的。
所以,你看到#aarrggbb中gg和bb放错了位置。

现在,通过阅读您的代码,我得到了确认:

argb = Color.argb(alpha, redValue,blueValue,greenValue);

应该是:

argb = Color.argb(alpha, redValue, greenValue, blueValue);

(a,r,g和b - 不是a,r,b和g)

它发生了。