2017-08-11 58 views
1

我使用getBitmapbitmap = textureView.getBitmap(width_bit, height_bit); 但它在camera2api的Highspeedcapturesession循環,它可以捕獲高達120fps。所以每秒都可以創建大量的位圖。getbitmap使應用程序緩慢

問題是,我運行應用程序後,時間繼續,它變得非常緩慢,預覽紋理凍結。

我該如何解決這個減緩問題?

(我覺得getbitmap也節省大量的緩衝位圖,如果這個權利,我怎麼能發佈幾個位,同時運行的應用程序?)

感謝。

回答

0

建立在AsyncTask

這樣,你的工作不會過載主線程的位圖:

private class MyAsyncTask extends AsyncTask<String, Void, Bitmap> { 
    protected void onPreExecute() { 
     // Runs on the UI thread before doInBackground 
     // Good for toggling visibility of a progress indicator 
     progressBar.setVisibility(ProgressBar.VISIBLE); 
    } 

    protected Bitmap doInBackground(String... strings) { 
     // Some long-running task like downloading an image. 
     Bitmap = downloadImageFromUrl(strings[0]); 
     return someBitmap; 
    } 

    protected void onProgressUpdate(Progress... values) { 
     // Executes whenever publishProgress is called from doInBackground 
     // Used to update the progress indicator 
     progressBar.setProgress(values[0]); 
    } 

    protected void onPostExecute(Bitmap result) { 
     // This method is executed in the UIThread 
     // with access to the result of the long running task 
     imageView.setImageBitmap(result); 
     // Hide the progress bar 
     progressBar.setVisibility(ProgressBar.INVISIBLE); 
    } 
} 

而當你需要在MAINT線程運行的東西再次使用這樣的:

MyActivity.runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      //Your code to run in GUI thread here 
     }//public void run() { 
}); 
+0

謝謝,我會試試這個。 –

+0

我嘗試了很多次,但是卻出錯。因爲從紋理視圖獲取位圖應該在UI線程中執行。那麼還有其他方法可以嘗試嗎?我希望它是....感謝 –

+0

在調用異步任務之前獲取位圖,然後在該任務中執行繁重的工作。這解決了你的問題? – GuilhermeFGL