试图使位图中的随机像素成为一种新颜色,但它给出了一个错误原因

时间:2015-06-10 01:51:01

标签: android colors bitmap

它在一个视图中,所以setBackground方法应该没问题,我传递Color.BLUE,这绝对是一个合适的int。

    public void changeRandomPixel(int color) 
    {
         Bitmap resizedBitmap = Bitmap.createScaledBitmap(intialBitmap, 10, 10, true);
            int[] pixels = new int[resizedBitmap.getHeight()* resizedBitmap.getWidth()];
            resizedBitmap.getPixels(pixels, 0,  resizedBitmap.getWidth(), 0, 0,  resizedBitmap.getWidth(),  resizedBitmap.getHeight());

            Random rand = new Random();

            pixels[rand.nextInt(100)] = color;          

            resizedBitmap.setPixels(pixels, 0,  resizedBitmap.getWidth(), 0, 0,  resizedBitmap.getWidth(),  resizedBitmap.getHeight());

            Drawable background = new BitmapDrawable(getResources(), resizedBitmap);
            setBackground(background);
    }

0 个答案:

没有答案