我有以下代码将图像保存到我的应用程序的缓存目录中。该方法在单独的Runnable()
内调用,我希望这会加快应用程序的速度。就目前而言,Bitmap的压缩在处理器上非常昂贵,并且导致应用程序的其余部分非常慢。我怎样才能加快速度?
public void combineImages(Bitmap bmp, int count, int count2){
Bitmap bottomImage = bmp.copy(Config.RGB_565, true);
float width = bmp.getWidth();
float height = bmp.getHeight();
bmp.recycle();
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
Bitmap topImage = myBitmap.copy(Config.ARGB_4444, true);
myBitmap.recycle();
myBitmap = null;
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, (width / 2) - 200, (height / 2) - 160, null);
topImage.recycle();
topImage = null;
// bottomImage is now a composite of the two.
// To write the file out to the Cache:
OutputStream os = null;
try {
os = new FileOutputStream(getApplicationContext().getCacheDir() + "/" + path);
bottomImage.compress(CompressFormat.PNG, 50, os); //Here it is most expensive and what slows the app down
} catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
您应该使用单独的线程来运行该方法,方法是创建一个扩展Thread
的新内部类并调用该方法或创建一个新的Thread
并传递Runnable
作为构造函数的参数。如果这是一项持续性任务,即您需要处理许多位图,则可能需要查看HandlerThread
或IntentService
。