我试图找到ImageView的黑色点。我正在使用png图像。虽然ImageView的高度大于我传递的y的值。但仍然会发生异常。你能帮我辨认一下我的错误吗?
public class MainActivity extends ActionBarActivity {
List<Point> points = new ArrayList<Point>();
TextView textView;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView1);
//textView = (TextView)findViewById(R.id.textView1);
}
@Override
public void onWindowFocusChanged (boolean hasFocus){
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
int top= imageView.getTop();
int left= imageView.getLeft();
int right= imageView.getRight();
int bottom = imageView.getBottom();
Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
for(int x=left; x<right; x++)
{
for(int y=top; y<bottom; y++)
{
int pixel = bitmap.getPixel(x, y);
if(pixel==color.black)
{
Point point=new Point();
point.x=x;
point.y=y;
points.add(point);
}
}
}
//textView.setText("Starting points: " + top + "," + left + " Ending Points:" + right + ","+ bottom);
}
}
@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;
}
class Point {
float x, y;
@Override
public String toString() {
return x + ", " + y;
}
}
}
答案 0 :(得分:1)
你不需要在0开始x和y,然后迭代到bitmap.Width和bitmap.Height?您正在使用imageView获取尺寸,这将与位图不同。