打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
bitmap setPixels解决方法
bitmap setPixels
我的操作过程:请看代码
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.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Android高手进阶教程(二十二)之---Android中几种图像特效处理的集锦!! -...
第52章、Bitmap图像处理(从零开始学Android)
Android实现截屏方式整理(总结)
android 实现图片加水印
Android高斯模糊技术,实现毛玻璃效果
8.2.1 Bitmap(位图)全解析 Part 1
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服