android setPixel nie działa

0

Chcę edytować pojedynczo piksele bitmapy, ale coś nie działa,
program się wyłącza tuż po uruchomieniu

nie działa ten fragment:
podczas uruchomienia program wyrzuca illegal state exception

for(int i =5;i<50;++i)
        {
            for(int u=5;u<50;++u)
            {
              try {
                    img.setPixel(i,u,Color.argb(0,0,0,0));
                }
                catch (IllegalStateException e1)
                {
                    System.out.println("not mutable");
                }
                catch(IllegalArgumentException e2)
                {
                    System.out.println("out of range");
                }
            }
        }


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;



public class MyCanvas extends View
{
    Paint paint;
    Rect rect;
    Bitmap img;
    int x=1,y=1;
    public MyCanvas (Context context)
    {
        super(context);
        paint = new Paint();
        rect = new Rect();
       img= BitmapFactory.decodeResource(getResources(),R.drawable.owl);


        for(int i =5;i<50;++i)
        {
            for(int u=5;u<50;++u)
            {
               try {
                    img.setPixel(i,u,Color.argb(0,0,0,0));
                }
                catch (IllegalStateException e1)
                {
                    System.out.println("not mutable");
                }
                catch(IllegalArgumentException e2)
                {
                    System.out.println("out of range");
                }
            }
        }
    }

    @Override

    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawBitmap(img,x ,y, paint);
        invalidate();

    }
    public boolean onTouchEvent (MotionEvent event)
    {
        x = (int)event.getX();
        y =(int)event.getY();
        return true;
    }
}
0

prawdopodobnie Twoja bitmapa jest niemutowalna. zrob sobie mutowalna kopie i pracuj na niej:

Bitmap.copy(Bitmap.Config config, boolean isMutable)

1 użytkowników online, w tym zalogowanych: 0, gości: 1