的背景颜色我有一个普通的ImageView
对象colorSquare
。获得一个ImageView的
它是简单的通过调用设置对象的颜色。
colorSquare.setBackgroundColor(color);
但我怎么做反向,即检索ImageView的背景颜色?
的背景颜色我有一个普通的ImageView
对象colorSquare
。获得一个ImageView的
它是简单的通过调用设置对象的颜色。
colorSquare.setBackgroundColor(color);
但我怎么做反向,即检索ImageView的背景颜色?
你可以做什么是
从ImageView获取ColorDrawable。
ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground();
现在
drawable.getColor()
将要给你颜色。
如果u已设置的颜色这只会工作,否则U将得到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()
WTH这是否响应要做的瓦特/的问题? – swooby 2015-12-23 21:10:41
是的,这很有用。 – CjS 2012-02-18 11:13:36
这只能在API层面上工作11或以上 – Maragues 2012-03-27 11:07:56
@Maragues是的,它只会工作的API等级> = 11 – 2012-03-27 11:57:55