Android的结合许多位图一个大单出现问题,不能循环使用位图 [英] Android Combine many bitmaps to one large one goes wrong and can't recycle bitmap
问题描述
我想结合中所包含的ArrayList中一个大的位图许多小位图。
不过,我不知道为什么大的位图的循环。这意味着它似乎复制阵列中的第一个元素。我试图绘制每个小位图阵列中进行测试,它工作正常,但是当我运行,如跌破code中的循环,它出了问题。
在addiditon,当我添加了 bmp.recycle()
和 BMP = NULL
,它会导致错误试图用一个循环位图。我不明白为什么错误发生。
你能帮助我,请,谢谢!
公共静态位图getBitmapForVisibleRegion(的WebView的WebView){
位图returnedBitmap = NULL;
webview.setDrawingCacheEnabled(真正的);
returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(假);
返回returnedBitmap;
}
公共无效CombineBitmap(){
ArrayList的<位图>骨形态发生蛋白=新的ArrayList<位图>();
的for(int i = 0; I< webView.getWidth; I + = needToCapture){
bmps.add(getBitmapForVisibleRegion(web视图));
webView.scrollBy(needToCapture,0);
}
位图bigbitmap = Bitmap.createBitmap(largeBitmapWidth,largeBitmapHeight,Bitmap.Config.ARGB_8888);
帆布bigcanvas =新的Canvas(bigbitmap);
涂料粉刷=新的油漆();
INT iWidth = 0;
的for(int i = 0; I< bmps.size();我++){
BMP位= bmps.get(我);
bigcanvas.drawBitmap(BMP,iWidth,0,漆);
iWidth + = bmp.getWidth();
bmp.recycle();
BMP = NULL;
}
}
我终于找到了我的问题。这是因为我的伪错误。
我要使用scrollTo而不是scrollBy
在我改变scrollTo,一切工作正常。这实在是一个有益的经验。
I want to combine many small bitmaps which are contained in ArrayList to one large bitmap.
However, I don't know why the large bitmap is looped. It means it seems to copy only the first element in the array. I tried to draw each small bitmap in the array to test and it works fine, but when I run the loop like the below code, it goes wrong.
In addiditon, when I add the bmp.recycle()
and bmp = null
, it causes the error "trying to use a recycled bitmap". I don't understand why the error happens.
Can you help me, please, thanks!
public static Bitmap getBitmapForVisibleRegion(WebView webview) {
Bitmap returnedBitmap = null;
webview.setDrawingCacheEnabled(true);
returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
return returnedBitmap;
}
public void CombineBitmap(){
ArrayList<Bitmap> bmps = new ArrayList<Bitmap>();
for (int i = 0; i < webView.getWidth; i+=needToCapture){
bmps.add(getBitmapForVisibleRegion(webView));
webView.scrollBy(needToCapture, 0);
}
Bitmap bigbitmap = Bitmap.createBitmap(largeBitmapWidth, largeBitmapHeight, Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bigbitmap);
Paint paint = new Paint();
int iWidth = 0;
for (int i = 0; i < bmps.size(); i++) {
Bitmap bmp = bmps.get(i);
bigcanvas.drawBitmap(bmp, iWidth , 0, paint);
iWidth +=bmp.getWidth();
bmp.recycle();
bmp=null;
}
}
I finally found out my problem. It's because of my dummy mistake.
I have to use scrollTo instead scrollBy
After I change to scrollTo, everything works fine. This is really an useful experience.
这篇关于Android的结合许多位图一个大单出现问题,不能循环使用位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!