Agera 一个来自谷歌官方的超轻量Android库

OPEN编辑 9年前
   <p><img alt="" src="https://simg.open-open.com/show/779c0e817eb89c4fd2b31f0dffe333bf.png"></p>    <p>Agera(在瑞典语中表示“行动”)是超轻量Android库,可以为生命周期内不同形式的Android应用组件(如:活动组件Activities)或对象(如:视图对象Views)提供待用数据。该Android库引入函数响应编程机制,有利于数据处理过程中清晰划分时间、地点、要素等内容,因此只需使用拟自然语言简单说明便能描述复杂的异步处理过程。</p>    <p>下列演示了Agera的某些功能。该Wiki内容和Java文档说了Agera 各部分工作机制。</p>    <pre>  <code class="language-java">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        }      }</code></pre>    <p>本站原创,转载时保留以下信息:</p>    <p>本文转自:深度开源(open-open.com)</p>    <p>原文标题:Agera  一个来自谷歌官方的超轻量Android库</p>    <p>原文地址:http://www.open-open.com/lib/view/open1461140484040.html</p>