每个android项目都应该使用的android 库

http://blog.teamtreehouse.com/android-libraries-use-every-project

A good developer knows to never reinvent the wheel (“unless you plan on learning more about wheels“). With its roots in Linux and the open source community, Android has a vibrant and strong ecosystem of open source libraries that developers can freely use in their own apps. While some are for very specific uses that you may never need, others are so helpful and, well, simply delightful that you should never start an app without them.

It may sound like a lot of work to use an open source library in a project, but with Android Studio, it’s easier than ever. Often it only takes one line of code in your app’s build.gradle file to include a library automatically. Gradle is the new build automation tool in Android Studio, and it will automatically download and include libraries if they are set up for it. It is so easy and popular that many libraries have this built in and include instructions on how to do it.

ButterKnife

On to my favorite libraries! Let’s start with a test: which of these do you like better?

protected TextView mWelcomeLabel;
protected EditText mUsernameField;
protected EditText mPasswordField;
protected Button mSubmitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mWelcomeLabel = (TextView) findViewById(R.id.welcomeLabel);
  mUsernameField = (EditText) findViewById(R.id.usernameField);
  mPasswordField = (EditText) findViewById(R.id.passwordField);
  mSubmitButton = (Button) findViewById(R.id.submitButton);
}

or

@InjectView(R.id.welcomeLabel) protected TextView mWelcomeLabel;
@InjectView(R.id.usernameField) protected EditText mUsernameField;
@InjectView(R.id.passwordField) protected EditText mPasswordField;
@InjectView(R.id.submitButton) protected Button mSubmitButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_my);
  ButterKnife.inject(this);
}

The latter code is more concise and understandable, don’t you think? The second block of code is using a library called ButterKnife, which uses annotations to “inject” views by creating boilerplate code for you. ButterKnife is small, simple, and lightweight, and because it makes your life as a developer easier, you should pretty much always use it. It would be great if the Android SDK itself could improve in this manner!

There are additional attributes you can use to make OnClickListeners and other common, verbose aspects of Android development easier to write and understand.

Below is all you need to include this library automatically in your Android Studio projects. Just add this one line to your app’s build.gradle file (in app/src):

compile ‘com.jakewharton:butterknife:5.1.2‘

You need to add it in the dependencies section, like this:

dependencies {
  compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
  compile ‘com.jakewharton:butterknife:5.1.2‘
}

Picasso

You may not need this for every app, but if you are downloading images from the web, then you should use Picasso. There are a few popular libraries that do this kind of work, but Picasso is my favorite because it’s simple and easy and I really like how the API is written. Code should always be this intuitive and pleasurable to use!

If you have never downloaded an image from the web in Android, then perhaps you don’t know why this is so helpful. Here are the steps you need to take to download an image with only the standard Android APIs:

  1. Get Image URL
  2. Create AsyncTask to download image
  3. Execute AsyncTask
  4. Store result in Bitmap
  5. Set bitmap as source
  6. Cache image for future

That’s a lot of work! Doesn’t it seem like you should just be able to provide the URL to an ImageView and magically have it appear? Check out the steps if you use Picasso:

  1. Get image URL
  2. Load it into an ImageView with one line:
Picasso.with(this).load(imageUrl).into(mImageView);

This last line says, “With this context, load this image URL into this ImageView.” Not only is it short and sweet, but it also takes care of those other steps mentioned above behind the scenes. It’s an asynchronous download and the image is automatically cached for future use. It also has additional features that make it helpful for debugging and other work.

Once again, adding it to your project is super easy in Android Studio. Just add this line to your dependencies section (like ButterKnife above):

compile ‘com.squareup.picasso:picasso:2.3.3‘

If you want to see this in action, I cover using it in Build a Self-Destructing Message App and Implementing Designs for Android.

Downloading things other than images from the web? Check out android-async-http!

Animations

The Material Design guidelines focus heavily on animation, and if you use it correctly, you can really make your app look polished and make your interactions more intuitive and enjoyable. Animations can be hard, but good libraries make them easy!

For a set of regular View animations, check out AndroidViewAnimations. I’ll let the author’s animated GIF speak for itself:

The syntax for this library is similar to what we saw for Picasso above:

YoYo.with(Techniques.Bounce)
    .duration(700)
    .playOn(findViewById(R.id.usernameField));

Including it is simple, though it does require two other projects as well:

dependencies {
  compile ‘com.nineoldandroids:library:2.4.0‘
  compile ‘com.daimajia.easing:library:[email protected]‘
  compile ‘com.daimajia.androidanimations:library:[email protected]‘
}

Another really helpful animations library is ListViewAnimations, which makes animating items in a list very easy. Check out the demo video on Google Play to see examples of everything it can do.

How to Discover Libraries

If you are new to the open source community, you may be wondering how you can find libraries like this on your own. It’s really just a mindset. Every time you go to add new code, ask, “Has somebody done this already?”

  • Ask Google
  • Ask GitHub
  • Ask StackOverflow
  • Ask a friend

Then, once you find a library that looks interesting, remember the most important two letters for researching open source libraries: vs.

Whenever I’m researching a library, I google the library’s name followed by “vs”. Then in autocomplete I see competing libraries that the one I’m researching has been compared to. That helps me evaluate similar libraries and choose the best one for me. Check out this example of ButterKnife:

RoboGuice, AndroidAnnotations, and Dagger are all libraries that do similar injection things like we saw ButterKnife does for us.

Lastly, you can utilize social media and newsletters to keep an eye on the open source landscape. I passively monitor the following and bookmark any libraries that look useful.

Remember, one of the main goals of programming is to be efficient and complete. Learning how to use the work of others to augment your own is a valuable skill even the most seasoned professionals rely on.

时间: 2024-11-01 20:42:17

每个android项目都应该使用的android 库的相关文章

Android项目从Eclipse增加支持Android Studio

使用Eclipse开发Android已经有些年头了,然而Android Studio(后面简称AS)为谷歌自己推的IDE.现在AS已经出了2.0版本,其功能的确要比Eclipse要多. AS对硬件要求较高,Eclipse使用ADT插件也是简单易用.本文不对这2个IDE的优缺点进行阐述,没有哪个一定能完全替代哪一个这么一说,选择自己喜欢的就好. 不过博主喜欢接触新事务,AS都出这么久了,也该拿出来遛遛了.对于一直使用Eclipse开发的安卓项目如何转换增加支持AS.(是增加支持AS,处理后AS和E

Android 项目的代码混淆,Android proguard 使用说明

简单介绍 Java代码是非常easy反编译的. 为了非常好的保护Java源码,我们往往会对编译好的class文件进行混淆处理. ProGuard是一个混淆代码的开源项目.它的主要作用就是混淆,当然它还能对字节码进行缩减体积.优化等,但那些对于我们来说都算是次要的功能. 官网地址:http://proguard.sourceforge.net/ 原理 Java 是一种跨平台的.解释型语言,Java 源码编译成中间"字节码"存储于 class 文件里.因为跨平台的须要,Java 字节码中包

之前的Android项目报错,新建Android项目报错,代码中找不到错误解决方案

打开一年前的东西,结果发现里面的android项目全部有个红叉,也找不到错误.新建一个项目也报错,首先确定自己的环境应该没问题,然后通过查看网上的资料,发现可能是debug的keystore到期啦. 通过网上资料的引导,做以下操作: 1.进入C:\Documents and Settings\Administrator\.android 删除路径下的debug.keystore及 ddms.cfg(我只删除了debug.kestore也可以.网上说两个都删除).(不同环境下的目录可能略有不同,可

在Android项目中调用已有.so库

注意该.so库指的是android平台的,非一般linux.unix平台:1.现有库libcom_ycan_testLib.so2.新建android项目TestLib23.添加新类:类名:testLib包路径:参考现有库名,应为com.ycan4.在新类中声明库的本地方法,如下: ? 1 2 3 4 5 6 package com.ycan; public class testLib {     public native int add (int a, int b);     public

每次新建Android项目都报样式找不到的错误?

============问题描述============ 如图 再网上找了下 说改为 <style name="AppBaseTheme" parent="android:Theme.Light">这样就行了 的确改为这样就ok了 但是如果每次都要这么改,不是很烦?有没有彻底解决这个问题的方法? 谢谢 ============解决方案1============ 新建的时候向导里面不是有选项么.不选则安之 ============解决方案2========

android 打开Eclipse之后android项目都报错怎么解决

错误原因: Debug Certificate 过期了的缘故. 解决办法: 1.进入:C:\Documents and Settings\Administrator\.android   删除:debug.keystore及 ddms.cfg   再重新打开Eclipse   再接着Clean清理一下就OK了 2.最后要把debug.keystore加进去就OK

(已解决)Eclipse报错:Could not find XXX.apk. 没有Android项目命名. There is no android project named

可能是你把当前项目设置为library项目了,按以下步骤切换回普通项目: 选择 Project->Properties 在左边的列表中,选择 Android 取消钩中"Is Library" 复选框 报错提示: 解决办法:

android studio不能创建android项目,或者不能识别android项目的解决方法

1.先点击file->close project回到android studio 欢迎界面,然后按如下图步骤添加相关的plugin就行了,然后重启. 2.如图 3. 4.

Android项目-几种常见的android应用架构

1.单个Activity架构 首页是Activity,里面页面为View 头部底部栏都是公用的,只是改变文字和图案. 底部导航栏为RadioButton 中部可以滑动切换 跳转很快,但是首页不流畅. 这种架构的好处是思路清晰,系统运行开销小.缺点是控制不好的话代码混乱,而且生命周期不好管理. 2.多个Activity-利用BaseActivity提供共性 3.多个Activity-利用TabActivity 子MainActivity(TabActivity)->Activity(BaseAct