图片加载框架Glide使用教程

时间:2020-12-10 09:17:11

Gradle:

compile 'com.github.bumptech.glide:glide:3.7.0'


MainActivity布局:

LinearLayout

<ListView

    android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>




TwoActivity布局:

<RelativeLayout

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_marginTop="24dp"
app:srcCompat="@mipmap/ic_launcher" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView"
android:layout_marginLeft="37dp"
android:layout_marginStart="37dp"
android:layout_toEndOf="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:text="张三" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21"
android:layout_alignBottom="@+id/imageView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
</RelativeLayout>


item布局:

<LinearLayout

 <ImageView
android:id="@+id/img_2"
android:src="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="zhangsan"
android:id="@+id/tv_1"
android:textSize="30sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_2"
android:textSize="30sp"
android:text="524"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity代码:

public class MainActivity extends AppCompatActivity {

ListView listView;
List<Bean> list = new ArrayList<>();
String[] imageUrls = {"http://img.my.csdn.net/uploads/201309/01/1378037235_3453.jpg",
"http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg",
"http://img.my.csdn.net/uploads/201309/01/1378037235_9280.jpg",
"http://img.my.csdn.net/uploads/201309/01/1378037234_3539.jpg",};
String[] namee={"徐良","许嵩","汪苏泷","小贱"};
String[] agee={"21","43","54","65"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView= (ListView) findViewById(R.id.listview);
for (int i = 0; i < 4; i++) {
Bean bean = new Bean();
bean.setName(namee[i]);
bean.setAge(agee[i]);
bean.setImg(imageUrls[i]);
list.add(bean);

}

MyAdapter adapter = new MyAdapter(MainActivity.this,list);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(MainActivity.this,TwoActivity.class);
intent.putExtra("name",list.get(i).getName());
intent.putExtra("age",list.get(i).getAge());
intent.putExtra("image",list.get(i).getImg());

startActivity(intent);
}
});

}
}

TwoActivity代码:
public class TwoActivity extends AppCompatActivity {

private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
imageView= (ImageView) findViewById(R.id.imageView);
String st = getIntent().getStringExtra("image");
Glide.with(this).load(st).into(imageView);

}
}

MyAdapter代码:
public class MyAdapter extends BaseAdapter {    Context context;    List<Bean> list;    public MyAdapter(Context context, List<Bean> list) {        this.context = context;        this.list = list;    }    @Override    public int getCount() {        return list.size();    }    @Override    public Object getItem(int i) {        return list.get(i);    }    @Override    public long getItemId(int i) {        return i;    }    @Override    public View getView(int i, View view, ViewGroup viewGroup) {        ViewHolder holder;        if (view==null){            holder=new ViewHolder();            view=LayoutInflater.from(context).inflate(R.layout.item,null);            holder.names=view.findViewById(R.id.tv_1);            holder.ages=view.findViewById(R.id.tv_2);            holder.images=view.findViewById(R.id.img_2);            view.setTag(holder);        }else {            holder= (ViewHolder) view.getTag();        }        holder.names.setText(list.get(i).getName());        holder.ages.setText(list.get(i).getAge());        Glide.with(context).load(list.get(i).getImg()).into(holder.images);        return view;    }    class ViewHolder{        TextView names,ages;        ImageView images;    }}

Bean类代码:

public class Bean {
String name;
String age;
String img;
public Bean(){

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getImg() {
return img;
}

public void setImg(String img) {
this.img = img;
}
}