android 响应式编程框架,Android 专用响应式编程框架 — Agera

public class AgeraActivity extends Activity

implements Receiver, Updatable {

private static final ExecutorService NETWORK_EXECUTOR =

newSingleThreadExecutor();

private static final ExecutorService DECODE_EXECUTOR =

newSingleThreadExecutor();

private static final String BACKGROUND_BASE_URL =

"http://www.gravatar.com/avatar/4df6f4fe5976df17deeea19443d4429d?s=";

private Repository> 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.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() {

@NonNull

@Override

public HttpRequest get() {

DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

int size = Math.max(displayMetrics.heightPixels,

displayMetrics.widthPixels);

return httpGetRequest(BACKGROUND_BASE_URL + 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>() {

@NonNull

@Override

public Result apply(@NonNull HttpResponse response) {

byte[] body = response.getBody();

return absentIfNull(decodeByteArray(body, 0, body.length));

}

}) // Decode the response to the result of a bitmap, absent on failure

.onDeactivation(SEND_INTERRUPT) // Interrupt thread on deactivation

.compile(); // Create the repository

}

@Override

protected void onResume() {

super.onResume();

// Start listening to the repository, triggering the flow

background.addUpdatable(this);

}

@Override

protected void onPause() {

super.onPause();

// Stop listening to the repository, deactivating it

background.removeUpdatable(this);

}

@Override

public void update() {

// Called as the repository is updated

// If containing a valid bitmap, send to accept below

background.get().ifSucceededSendTo(this);

}

@Override

public void accept(@NonNull Bitmap background) {

// Set the background bitmap to the background view

backgroundView.setImageBitmap(background);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值