Glide的使用

 首先添加依赖:

compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.jakewharton:butterknife:7.0.1'//Android View设计的绑定注解
compile 'com.github.bumptech.glide:glide:3.7.0'//专注于平滑滚动的图片加载和缓存库

 MainActivity :


public class MainActivity extends AppCompatActivity {

    @Bind(R.id.gridview)
    GridView gridview;

    ImageView imageView;

    String[] strings = new String[]{ "https://img-my.csdn.net/uploads/201407/26/1406383299_1976.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383291_6518.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383291_8239.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383290_9329.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383290_1042.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383275_3977.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383264_3954.jpg",
            "https://img-my.csdn.net/uploads/201407/26/1406383264_4787.jpg",}; //存储网址

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

        gridview.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return strings.length;
            }

            @Override
            public Object getItem(int position) {
                return strings[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);

                imageView = (ImageView) convertView.findViewById(R.id.imageView);
                Glide.with(MainActivity.this).load(strings[position])
                        .placeholder(R.mipmap.ic_launcher).into(imageView);

                return convertView;
            }
        });
    }
}

xml文件:


xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3" />


适配器 xml文件 : 

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

            android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="70dp" />

记得在配置文件上 添加联网权限:


android:name="android.permission.INTERNET"/>

效果图:


Glide的使用_第1张图片


 -----------------------------------------------------------------------------------


看到别人大神的 博客 讲解的很清楚: http://www.cnblogs.com/whoislcj/p/5558168.html


你可能感兴趣的:(Glide的使用)