Java Android:getPixel(x,y)颜色值错误
我是一个android/java新手,正在开发一个简单的应用程序,可以从色轮中挑选出和谐的颜色。问题是,我甚至无法在控制盘下方的框中显示正确的颜色(触摸颜色控制盘上的黄色会生成紫色:S)。我不明白发生了什么。我也用几个不同的图像尝试了这段代码,生成的颜色仍然都是关闭的Java Android:getPixel(x,y)颜色值错误,java,android,rgb,getpixel,argb,Java,Android,Rgb,Getpixel,Argb,我是一个android/java新手,正在开发一个简单的应用程序,可以从色轮中挑选出和谐的颜色。问题是,我甚至无法在控制盘下方的框中显示正确的颜色(触摸颜色控制盘上的黄色会生成紫色:S)。我不明白发生了什么。我也用几个不同的图像尝试了这段代码,生成的颜色仍然都是关闭的 public class MainActivity extends Activity { ImageView cw; Bitmap cwBmp; ColourView colourArray;
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;
}
}
ColorArray是一个ColorView对象
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;
}
}
公共类colorView扩展了视图{
油漆=新油漆();
ArrayList colors=新的ArrayList();
公共颜色视图(上下文){
超级(上下文);
颜色。添加(颜色。白色);
}
公共空白onDraw(画布){
int i=0;
而(i<220){
paint.setColor(colors.get(0));
油漆。设置行程宽度(0);
帆布。drawRect(25+(44*i),500,69+(44*i),550,油漆);
i=i+1;
}
}
私有int getRandomInt(){
int upper=colors.size();
int r=(int)(Math.random()*(上限));
返回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)
)()
发生了。它现在工作得很好:)谢谢你这么快就发现了我的错误,你给了我正确的输入,说黄色正在变成紫色。我和颜色打了很长一段时间的交道,我立即想到了这个问题……;)纯粹的运气,你用对了词!