<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                > 編寫:[allenlsy](https://github.com/allenlsy) - 原文:[http://developer.android.com/training/improving-layouts/smooth-scrolling.html](http://developer.android.com/training/improving-layouts/smooth-scrolling.html) 保持程序流暢的關鍵,是讓主線程(UI 線程)不要進行大量運算。你要確保在其他線程執行磁盤讀寫、網絡讀寫或是 SQL 操作等。為了測試你的應用的狀態,你可以啟用 [StrictMode](http://developer.android.com/reference/android/os/StrictMode.html)。 ### 使用后臺線程 你應該把主線程中的耗時間的操作,提取到一個后臺線程(也叫做“工人線程”,英文 worker thread)中,使得主線程只關注 UI 繪畫。很多時候,使用 [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) 是一個簡單的在主線程以外進行操作的方法。系統使用一個全局隊列來排列所有的 AsyncTask,并且一次運行其中的 [execute()](http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)) 方法。這個行為是全局的,這意味著你不需要考慮自己定義線程池的事情。 在以下的例子中,一個 AsyncTask 被用于在后臺線程載入圖片,并在載入完成后把圖片放入 UI 。當圖片正在載入時,它還回顯示一個進度轉盤。 ~~~ // Using an AsyncTask to load the slow images in a background thread new AsyncTask<ViewHolder, Void, Bitmap>() { private ViewHolder v; @Override protected Bitmap doInBackground(ViewHolder... params) { v = params[0]; return mFakeImageLoader.getImage(); } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (v.position == position) { // If this item hasn't been recycled already, hide the // progress and set and show the image v.progress.setVisibility(View.GONE); v.icon.setVisibility(View.VISIBLE); v.icon.setImageBitmap(result); } } }.execute(holder); ~~~ 從 Android 3.0 (API level 11) 開始, AsyncTask 有個新特性,那就是它可以在多個 CPU 核上運行。你可以調用 [`executeOnExecutor()`](http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor,%20Params...) 來自動根據核數,在多核上執行任務,而不是調用 [`execute()`](http://developer.android.com/reference/android/os/AsyncTask.html#execute(Params...)。 ### 在 View Holder 中填入視圖對象 你的代碼可能在 ListView 滑動時經常使用 `findViewById()`,這樣會降低性能。即使是 Adapter 返回一個用于回收的 inflate 后的視圖,你仍然需要查看這個元素并更新它。避免頻繁調用 `findViewById()` 的方法之一,就是使用 View Holder(視圖占位符)設計模式。 一個 ViewHolder 對象存儲了他的標簽下的每個視圖。這樣你不用頻繁查找這個元素。第一,你需要創建一個類來存儲你會用到的視圖。比如: ~~~ static class ViewHolder { TextView text; TextView timestamp; ImageView icon; ProgressBar progress; int position; } ~~~ 然后,在 Layout 的類中生成一個 ViewHolder 對象: ~~~ ViewHolder holder = new ViewHolder(); holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image); holder.text = (TextView) convertView.findViewById(R.id.listitem_text); holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp); holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner); convertView.setTag(holder); ~~~ 這樣你就可以輕松獲取每個視圖,而不是用 `findViewById()` 來不斷查找視圖,節省了寶貴的運算時間。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看