Android 获取ImageView的背景色
我有一个普通的Android 获取ImageView的背景色,android,colors,imageview,Android,Colors,Imageview,我有一个普通的ImageView对象colorSquare 通过调用设置对象的颜色很简单 colorSquare.setBackgroundColor(color); 但是我如何做相反的操作,即检索ImageView背景的颜色?您可以做的是 从ImageView中获取彩色绘图 ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground(); 现在 drawable.getColor() 我会给你颜色的 仅当您设置了颜色
ImageView
对象colorSquare
通过调用设置对象的颜色很简单
colorSquare.setBackgroundColor(color);
但是我如何做相反的操作,即检索ImageView背景的颜色?您可以做的是
从ImageView中获取彩色绘图
ColorDrawable drawable = (ColorDrawable) colorSquare.getBackground();
现在
drawable.getColor()
我会给你颜色的
仅当您设置了颜色,否则您将获得ClassCastExceptionprivate int colorfindcolor(视图v、int x、int y){
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;
}
int offset=1;//3x3矩阵
int像素数目=0;
int-xImage=0;
int-yImage=0;
位图imageBitmap=BitmapFactory.decodeResource(context.getResources(),
R.可绘制调色板(颜色);
xImage=(int)(x*((双精度)imageBitmap.getWidth()/(双精度)paletteImg
.getWidth());
yImage=(int)(y*((双精度)imageBitmap.getHeight()/(双精度)paletteImg
.getHeight());
对于(inti=xImage-offset;这个回答必须与问题一起做吗?