1.图片裁剪:https://github.com/Yalantis/uCrop
2.图片加载:https://github.com/bumptech/glide
Glide is a fast and efficient open source media management and image loading framework for Android that wraps mediadecoding, memory and disk caching, and resource pooling into a simple and easy to use interface.
Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible APIthat allows developers to plug in to almost any network stack. By default Glide uses a customHttpUrlConnection
basedstack, but also includes utility libraries plug in to Google's Volley project or Square's OkHttp library instead.
Glide's primary focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide isalso effective for almost any case where you need to fetch, resize, and display a remote image.
3.懒人自动化工具:https://github.com/JakeWharton/butterknife
让我们从大量的findViewById()和setonclicktListener()解放出来,其对性能的影响微乎其微(查看过Butter Knife的源码,其自定义注解的实现都是限定为RetentionPolicy.CLASS,也就是到编译出.class文件为止有效,在运行时不额外消耗性能,其是通过java注解自动生成java代码的形式来完成工作),其也有一个明显的缺点,那就是代码的可读性差一些
class ExampleActivity extends Activity { @Bind(R.id.title) TextView title; @Bind(R.id.subtitle) TextView subtitle; @Bind(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); // TODO Use fields... } }
class ExampleActivity extends Activity { @BindString(R.string.title) String title; @BindDrawable(R.drawable.graphic) Drawable graphic; @BindColor(R.color.red) int red; // int or ColorStateList field @BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field // ... }
public class FancyFragment extends Fragment { @Bind(R.id.button1) Button button1; @Bind(R.id.button2) Button button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } }
4.GSON:
Gson是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可用于将Java对象转换成对应的JSON表示,也可以将JSON字符串转换成一个等效的Java对象。如果与API打交道的话,那么这将会是你经常需要的东西。我们主要使用JSON的原因就是,相较XML,轻量级的JSON要简单的多。
- // Serialize
- String userJSON = new Gson().toJson(user);
- // Deserialize
- User user = new Gson().fromJson(userJSON, User.class);
5.EventBus: https://github.com/greenrobot/EventBus
EventBus是用于简化应用中各个部件之间通信的一个库。比如从一个Activity发送消息到一个正在运行的服务,亦或是片段之间简单的互动。而下面使用的示例,就是如果网络连接丢失,该如何通知一个活动:
- public class NetworkStateReceiver extends BroadcastReceiver {
- // post event if there is no Internet connection
- public void onReceive(Context context, Intent intent) {
- super.onReceive(context, intent);
- if(intent.getExtras()!=null) {
- NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
- if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
- // there is Internet connection
- } else if(intent
- .getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {
- // no Internet connection, send network state changed
- EventBus.getDefault().post(new NetworkStateChanged(false));
- }
- }
- // event
- public class NetworkStateChanged {
- private mIsInternetConnected;
- public NetworkStateChanged(boolean isInternetConnected) {
- this.mIsInternetConnected = isInternetConnected;
- }
- public boolean isInternetConnected() {
- return this.mIsInternetConnected;
- }
- }
- public class HomeActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- EventBus.getDefault().register(this); // register EventBus
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- EventBus.getDefault().unregister(this); // unregister EventBus
- }
- // method that will be called when someone posts an event NetworkStateChanged
- public void onEventMainThread(NetworkStateChanged event) {
- if (!event.isInternetConnected()) {
- Toast.makeText(this, "No Internet connection!", Toast.LENGTH_SHORT).show();
- }
- }
- }