开源库

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.

开源库_第1张图片

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要简单的多。

[js] view plain copy
  1. // Serialize   
  2. String userJSON = new Gson().toJson(user);  
  3.   
  4. // Deserialize  
  5. User user = new Gson().fromJson(userJSON, User.class); 

5.EventBus: https://github.com/greenrobot/EventBus

EventBus是用于简化应用中各个部件之间通信的一个库。比如从一个Activity发送消息到一个正在运行的服务,亦或是片段之间简单的互动。而下面使用的示例,就是如果网络连接丢失,该如何通知一个活动:

[js] view plain copy
  1. public class NetworkStateReceiver extends BroadcastReceiver {  
  2.   
  3.     // post event if there is no Internet connection  
  4.     public void onReceive(Context context, Intent intent) {  
  5.         super.onReceive(context, intent);  
  6.         if(intent.getExtras()!=null) {  
  7.             NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);  
  8.             if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {  
  9.                 // there is Internet connection  
  10.             } else if(intent  
  11.                 .getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY,Boolean.FALSE)) {  
  12.                 // no Internet connection, send network state changed  
  13.                 EventBus.getDefault().post(new NetworkStateChanged(false));  
  14.             }  
  15. }  
  16.   
  17. // event  
  18. public class NetworkStateChanged {  
  19.   
  20.     private mIsInternetConnected;  
  21.   
  22.     public NetworkStateChanged(boolean isInternetConnected) {  
  23.         this.mIsInternetConnected = isInternetConnected;  
  24.     }  
  25.   
  26.     public boolean isInternetConnected() {  
  27.         return this.mIsInternetConnected;  
  28.     }  
  29. }  
  30.   
  31. public class HomeActivity extends Activity {  
  32.   
  33.     @Override  
  34.     protected void onCreate(Bundle savedInstanceState) {  
  35.         super.onCreate(savedInstanceState);  
  36.         setContentView(R.layout.activity_main);  
  37.   
  38.         EventBus.getDefault().register(this); // register EventBus  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onDestroy() {  
  43.         super.onDestroy();  
  44.         EventBus.getDefault().unregister(this); // unregister EventBus  
  45.     }  
  46.   
  47.     // method that will be called when someone posts an event NetworkStateChanged  
  48.     public void onEventMainThread(NetworkStateChanged event) {  
  49.         if (!event.isInternetConnected()) {  
  50.             Toast.makeText(this"No Internet connection!", Toast.LENGTH_SHORT).show();  
  51.         }  
  52.     }  
  53.   



你可能感兴趣的:(Android,开源)