Android:GetPixel(X,Y)颜色值是错误的



我是一个Android/Java Newbie,正在开发一个简单的应用程序,该应用程序将从色轮中挑选的颜色中产生和谐的颜色。问题在于,我什至无法获得合适的颜色出现在轮子下方的盒子中(在色轮上触摸黄色会产生紫色: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;
    }

}

颜色是颜色的对象

   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>

现在,甚至没有阅读您的代码,仅通过逻辑:如果您的黄色变成紫色,那么您将放错绿色和蓝色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)

;)

它发生了。

最新更新