我的操作过程:请看代码
int w = 352;
int h = 288;
int[] pixels = new int[w * h * 10];
ImageView iv = (ImageView) findViewById(R.id.fImageView);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon2);// 创建对象
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),bitmap.getHeight());// 放入
bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),bitmap.getHeight());// 取出
iv.setImageBitmap(bitmap);// 显示
在bitmap.setPixels(pixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(),bitmap.getHeight());出现了一下以下异常:
AndrodRuntime Caused by:java.lang.IllegalStateException
AndrodRuntime at android.graphics.Bitmap.setPixels(Bitmap.java:853)
AndrodRuntime at com.phd.canwasDemo.CanvasDemoF.onCreate(CanvasDemoF.java:37)
AndrodRuntime at android.app.Instrumentation.allActivityOnCreate(Instrumentation.java:1123)
AndrodRuntime at android.app.ActivityThread.performLaunchActivity(ActivityThread.java.2233)
AndrodRuntime ...11 more
但我运行
ImageView iv = (ImageView) findViewById(R.id.gImageView);
Bitmap b = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);// 创建bitmap对象
int[] pixels = new int[b.getWidth() * b.getHeight()];// 创建像素数组
b.setPixel(0, 0, 0xFF00FF00);
b.setPixel(0, 1, 0x8000FF00);
b.setPixel(1, 0, 0x0000FF00);
b.getPixels(pixels, 0, b.getWidth(), 0, 0, b.getWidth(), b.getHeight());
b.setPixels(pixels, 0, b.getWidth(), 0, 0, b.getWidth(), b.getHeight());
iv.setImageBitmap(b);
可正常运行并显示
请各位帮我分析下,该如何解决!
------解决方案--------------------
这个问题我测试过了,如果再建立一个
Bitmap bitmap2=Bitmap.createBitmap(w, h, Config.ARGB_8888);
然后使用
bitmap2.setPixels(pixels, 0, bitmap2.getWidth(), 0, 0, bitmap2.getWidth(),
bitmap2.getHeight());// 取出
显示
iv.setImageBitmap(bitmap2);// 显示
就可以了,我分析会不会是内存处理上的问题。
------解决方案--------------------
我查了下,第一个错误应该是the bitmap is not mutable
------解决方案--------------------
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon2);//返回的估计是一个immutalbe的bitmap;所以不能用setpixels
而Bitmap b = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888);//返回的是一个mutable的bitmap,可以用setpixels
------解决方案--------------------
从Bitmap.createBitmap可以看到有的返回immutable bitmap,有的返回 mutable bitmap
1。static Bitmap createBitmap(Bitmap src)
Returns an immutable bitmap from the source bitmap.
2.static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config)
Returns a immutable bitmap with the specified width and height,
3.static Bitmap createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
Returns a immutable bitmap with the specified width and height,
4.static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
Returns an immutable bitmap from subset of the source bitmap,
5.static Bitmap createBitmap(int width, int height, Bitmap.Config config)
Returns a mutable bitmap with the specified width and height.
6.static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)
Returns an immutable bitmap from the specified subset of the source bitmap.