Android setPixel(int x,int y,int color)使我的程序崩溃如何解决?

时间:2013-10-05 00:00:02

标签: android colors bitmap imageview

ImageView testImage = (ImageView) findViewById(R.id.imageView1);
Bitmap bitmap = ((BitmapDrawable)testImage.getDrawable()).getBitmap();
bitmap.setHasAlpha(true);
bitmap.setPixel(10, 10, Color.argb(255,255,255,255));

当我尝试在onCreate中的主要活动中运行此代码时,我的程序会立即崩溃

我做错了什么?我想做的就是改变位图中的一个像素

2 个答案:

答案 0 :(得分:0)

有什么例外?正如我在默认情况下所记得的那样,位图是不可变的意味着你无法修改它。需要创建位图的副本。

答案 1 :(得分:0)

这将起作用

    ImageView testImage = (ImageView) findViewById(R.id.imageView1);
    BitmapFactory.Options options = new BitmapFactory.Options();
    Resources res = context.getResources();
    options.inMutable = true;
    Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.testImage,
                    options);
    bitmap.setPixel(10, 10, Color.argb(255,255,255,255));