【发布时间】:2012-02-18 07:43:36
【问题描述】:
我有一个普通的ImageView 对象colorSquare。
直接调用即可设置对象的颜色。
colorSquare.setBackgroundColor(color);
但是我该如何做相反的事情,即检索 ImageView 背景的颜色?
【问题讨论】:
我有一个普通的ImageView 对象colorSquare。
直接调用即可设置对象的颜色。
colorSquare.setBackgroundColor(color);
但是我该如何做相反的事情,即检索 ImageView 背景的颜色?
【问题讨论】:
你能做的就是
从 ImageView 中获取 ColorDrawable。
ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground();
现在
drawable.getColor()
会给你颜色。
这只有在你设置了颜色时才有效,否则你会得到 ClassCastException
【讨论】:
private int colorfindcolor(View v, int x, int y) {
int offset = 1; // 3x3 Matrix
int pixelsNumber = 0;
int xImage = 0;
int yImage = 0;
Bitmap imageBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.palette_color);
xImage = (int) (x * ((double) imageBitmap.getWidth() / (double) paletteImg
.getWidth()));
yImage = (int) (y * ((double) imageBitmap.getHeight() / (double) paletteImg
.getHeight()));
for (int i = xImage - offset; i <= xImage + offset; i++) {
for (int j = yImage - offset; j <= yImage + offset; j++) {
try {
color = imageBitmap.getPixel(i, j);
pixelsNumber += 1;
} catch (Exception e) {
// Log.w(TAG, "Error picking color!");
}
}
}
return color;
}
其中x,y是imageview触摸事件的event.getX(),event.getX()
【讨论】: