0

我正在使用 setPixel 函数来操作位图,但它不起作用。应用程序崩溃。02-21 13:05:04.582: E/AndroidRuntime(839): java.lang.IllegalStateException: 无法执行活动的方法

 public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void function(View view){

    Bitmap img = BitmapFactory.decodeResource(getResources(), R.drawable.test);
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();
    for(int x = 0; x < imgWidth; x++){
        for(int y = 0; y < imgHeight; y++){
            int pixColor = img.getPixel(x,y);
            if(pixColor == Color.WHITE){
                img.setPixel(x, y, Color.BLACK);
            }
            else if(pixColor == Color.BLACK){
                img.setPixel(x, y, Color.WHITE);
            }

        }
    }

    ImageView imgView = (ImageView) findViewById(R.id.imageView2);
    imgView.setImageBitmap(img);
    System.out.println("Picture Changed");

}



}
4

0 回答 0