setPixel在android中不起作用

时间:2013-02-21 12:42:49

标签: android

我正在使用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");

}



}

0 个答案:

没有答案