Fresco
簡介
Fresco是Facebook最新推出的一款用於Android應用中展示圖片的強大圖片庫,可以從網絡、本地存儲和本地資源中加載圖片。其中的Drawees可以顯示佔位符,直到圖片加載完成。而當圖片從屏幕上消失時,會自動釋放內存。
特性
1.內存管理
在5.0以下,GC將會顯著地引發界面卡頓。Fresco將圖片放到一個特別的內存區域,在圖片不顯示的時候,佔用的內存會自動被釋放,減少因圖片內存佔用而引發的OOM。(內存不夠用)
2.圖片的漸進式呈現
漸進式圖片格式先呈現大致的圖片輪廓,然後隨着圖片下載的繼續,呈現逐漸清晰的圖片,這對於移動設備,尤其是慢網絡有極大的利好,可帶來更好的用戶體驗。
3.Gif圖和WebP格式
支持加載Gif圖,支持WebP格式
4.圖像的呈現(Fresco 的 Drawees 設計)
自定義居中焦點(對人臉等圖片顯示非常有幫助)
圓角圖,當然圓圈也行。
下載失敗之後,點擊重現下載
自定義佔位圖,自定義overlay, 或者進度條
指定用戶按壓時的overlay(疊加)
5.圖像的加載(Fresco 的 image pipeline 設計,允許用戶在多方面控制圖片的加載)
爲同一個圖片指定不同的遠程路徑,或者使用已經存在本地緩存中的圖片
先顯示一個低解析度的圖片,等高清圖下載完之後再顯示高清圖
加載完成回調通知
對於本地圖,如有EXIF縮略圖,在大圖加載完成之前,可先顯示縮略圖
縮放或者旋轉圖片
處理已下載的圖片
WebP支持
EventBus
作用
兩個組件之間進行通信,但兩個組件之間並不知曉,開銷小,代碼更優雅,將發送者和接收者解耦。(同Intent等)
使用
1.在接收消息界面註冊
2.發送消息(eventbus.post()方法)
3.接收消息界面實現(四個)函數,可選擇性實現不同函數的功能
4.接收消息界面解除註冊
代碼實現
MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
public class MainActivity extends Activity {
private Button mButton;
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//註冊EventBus
EventBus.getDefault().register(this);
mButton= (Button) findViewById(R.id.btn_main);
mTextView= (TextView) findViewById(R.id.textview);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
}
@Subscribe
public void onEventMainThread(EventMessage message){
String msg="onEventMainThread收到了消息:"+message.getMessage();
mTextView.setText(msg);
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//解除註冊
}
}
SecondActivity:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.example.administrator.myimageloader.model.EventMessage;
import de.greenrobot.event.EventBus;
public class SecondActivity extends Activity {
private Button mButtonSecond;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
mButtonSecond= (Button) findViewById(R.id.btn_second);
mButtonSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new EventMessage("click button second"));
}
});
}
}