【问题标题】:Android setPixel(int x, int y, int color) makes my program crash how do I fix it?Android setPixel(int x, int y, int color) 使我的程序崩溃如何解决?
【发布时间】:2013-10-05 00:00:01
【问题描述】:
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 内的主要活动中运行此代码时,我的程序立即崩溃

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

【问题讨论】:

  • 读取(并包含)异常消息。
  • LogCat 是解决错误的最佳工具之一。习惯于阅读和理解它告诉你的内容。首先要检查的事情之一是文本列中的任何“由...引起”的 cmets。这还将有一个行号,告诉您代码中的哪个位置出现了问题。

标签: android colors bitmap imageview


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这会起作用

        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));
    

    【讨论】:

      猜你喜欢
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2017-03-17
      • 2016-03-26
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      相关资源
      最近更新 更多