我在图片上运行了以下代码。它会产生以下错误。我假设这是因为代码需要几秒钟才能运行然后崩溃,所以必须为cpu处理太多。我想问一下如何修改这段代码,使其在< 1second内完成并且不那么密集,所以它不会崩溃。谢谢。亚历
E/AndroidRuntime: FATAL EXCEPTION: main
Process: cartoonify.alexcz.cartoonify, PID: 25764
java.lang.IllegalStateException
at android.graphics.Bitmap.setPixels(Bitmap.java:1626)
at cartoonify.alexcz.cartoonify.Main$2.onClick(Main.java:111)
change_pic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(picturePresent == true){
int r,g,b,colour;
int [] allpixels = new int [picture.getHeight() * picture.getWidth()];
picture.getPixels(allpixels, 0, picture.getWidth(), 0, 0, picture.getWidth(), picture.getHeight());
for(int i = 0; i < allpixels.length; i++)
{
colour = allpixels[i];
r = Color.red(colour);
b = Color.blue(colour);
g = Color.green(colour);
if(r < FIRSTSECTION){
r = FIRST;
}else if(r >= SECONDSECTION && r < THIRDSECTION){
r = SECOND;
}else if(r >= THIRDSECTION && r < FOURTHSECTION){
r = THIRD;
}else if(r >= FOURTHSECTION && r < FIFTHSECTION){
r = FORTH;
}
if(b < FIRSTSECTION){
b = FIRST;
}else if(b >= SECONDSECTION && b < THIRDSECTION){
b = SECOND;
}else if(b >= THIRDSECTION && b < FOURTHSECTION){
b = THIRD;
}else if(b >= FOURTHSECTION && b < FIFTHSECTION){
b = FORTH;
}
if(g < FIRSTSECTION){
g = FIRST;
}else if(g >= SECONDSECTION && g < THIRDSECTION){
g = SECOND;
}else if(g >= THIRDSECTION && g < FOURTHSECTION){
g = THIRD;
}else if(g >= FOURTHSECTION && g < FIFTHSECTION){
g = FORTH;
}
allpixels[i] = Color.argb(1, r, g, b);
}
picture.setPixels(allpixels, 0, picture.getWidth(), 0, 0, picture.getWidth(), picture.getHeight());
drawableBitmap = new BitmapDrawable(getResources(), picture);
image.setBackground(drawableBitmap);
}
}
});
答案 0 :(得分:1)
setPixels
需要一个可变的位图,否则您将获得IllegalStateException
。请参阅documentation。
您应该复制或创建一个新的位图,并使其可变:
创建一个新的可变位图:
Bitmap newBitmap = Bitmap.createBitmap(originalWidth, originalHeight, Bitmap.Config.ARGB_8888);
或者复制并制作一个可变的位图:
boolean isMutable = true;
Bitmap newBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, isMutable);
现在您可以使用newBitmap.setPixels(...)
如果这是一项昂贵的操作,您应该考虑在后台线程中进行操作(例如AsyncTask
)。