Butter Knife、Picasso初体验

安装

在Android Studio模块中的build.gradle文件dependencies子句中加入:

compile 'com.squareup.picasso:picasso:2.3.3'
compile 'com.jakewharton:butterknife:6.1.0'

点击同步gradle文件按钮,系统会自动下载这两个库到External Libraries。

使用

1.添加获取网络权限

2.activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

    <Button
        android:id="@+id/btn_load_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading Image"
        android:layout_below="@id/image_view"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {


    @InjectView(R.id.image_view)
    ImageView imageView;
    @InjectView(R.id.btn_load_image)
    Button btnLoadImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.inject(this);

    }

    @OnClick(R.id.btn_load_image)
    public void loadImage()
    {
        Picasso.with(getApplication())
                .load("https://cms-assets.tutsplus.com/uploads/users/21/posts/19431/featured_image/CodeFeature.jpg")
                .resize(300, 300)
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.ic_launcher)
                .into(imageView);
    }

}

你可能感兴趣的:(Butter Knife、Picasso初体验)