Agera 一個來自谷歌官方的超輕量Android庫

五嘎子 8年前發布 | 59K 次閱讀 開源 Android開發 移動開發 Agera Android

Agera(在瑞典語中表示“行動”)是超輕量Android庫,可以為生命周期內不同形式的Android應用組件(如:活動組件Activities)或對象(如:視圖對象Views)提供待用數據。該Android庫引入函數響應編程機制,有利于數據處理過程中清晰劃分時間、地點、要素等內容,因此只需使用擬自然語言簡單說明便能描述復雜的異步處理過程。

下列演示了Agera的某些功能。該Wiki內容和Java文檔說了Agera 各部分工作機制。

public class AgeraActivity extends Activity implements Receiver<Bitmap>, Updatable {
      private static final ExecutorService NETWORK_EXECUTOR = newSingleThreadExecutor();
      private static final ExecutorService DECODE_EXECUTOR = newSingleThreadExecutor();

      private Repository<Result<Bitmap>> background;
      private ImageView backgroundView;

      @Override
      protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set the content view
        setContentView(R.layout.activity_main);

        // Find the background view
        backgroundView = (ImageView) findViewById(R.id.background);

        // Create a repository containing the result of a bitmap request. Initially absent, but
        // configured to fetch the bitmap over the network based on display size.
        background = repositoryWithInitialValue(Result.<Bitmap>absent())
            .observe() // Optionally refresh the bitmap on events. In this case, never
            .onUpdatesPerLoop() // Refresh per Looper thread loop. In this case, never
            .getFrom(new Supplier<HttpRequest>() {
              @NonNull
              @Override
              public HttpRequest get() {
                DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
                int size = Math.max(displayMetrics.heightPixels, displayMetrics.widthPixels);
                return httpGetRequest(
                    "http://www.gravatar.com/avatar/4df6f4fe5976df17deeea19443d4429d?s=" + size)
                    .compile();
              }
            }) // Supply an HttpRequest based on the display size
            .goTo(NETWORK_EXECUTOR) // Change execution to the network executor
            .attemptTransform(httpFunction())
            .orSkip() // Make the actual http request, skip on failure
            .goTo(DECODE_EXECUTOR) // Change execution to the decode executor
            .thenTransform(new Function<HttpResponse, Result<Bitmap>>() {
              @NonNull
              @Override
              public Result<Bitmap> apply(@NonNull HttpResponse response) {
                byte[] body = response.getBody();
                return absentIfNull(decodeByteArray(body, 0, body.length));
              }
            }) // Decode the successful http response to the result of a bitmap, absent on failure
            .onDeactivation(SEND_INTERRUPT) // Interrupt http request/decode on deactivation
            .compile(); // Create the repository
      }

      @Override
      protected void onResume() {
        super.onResume();
        background.addUpdatable(this); // Start listening to the repository, triggering the flow
      }

      @Override
      protected void onPause() {
        super.onPause();
        background.removeUpdatable(this); // Stop listening to the repository, deactivating it
      }

      @Override
      public void update() {
        // Called as the repository is updated
        background.get().ifSucceededSendTo(this); // If containing a valid bitmap, send to accept below
      }

      @Override
      public void accept(@NonNull Bitmap background) {
        backgroundView.setImageBitmap(background); // Set the background bitmap to the background view
      }
    }

本站原創,轉載時保留以下信息:

本文轉自:深度開源(open-open.com)

原文標題:Agera  一個來自谷歌官方的超輕量Android庫

原文地址:http://www.baiduhome.net/lib/view/open1461140484040.html

 本文由用戶 五嘎子 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!