Android Picture Loading Cache Framework Glide
- 2021-06-28 09:40:55
- OfStack
Glide Open Source Framework is Google's recommended picture loading and mitigation framework. Its open source address on Github is https://github.com/bumptech/glide
Of course, the framework recommended by Google is Volley.
The main development tool for Android is AndroidStudio. There are detailed instructions on how AndroidStudio uses Glide, https://github.com/bumptech/glide.
Because I just changed my job soon, the company and Eclipse, so I learned the Eclipse development tools that I temporarily still use.
Steps:
Add the glide.jar package to the project, and the jar package can be downloaded online.
Write code
public class MainActivity extends Activity {
private ImageView glide_iv;
private ListView glide_lv;
private static final String URL =" Write pictures here url";
private List<String> urls = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDatas();
glide_iv = (ImageView) findViewById(R.id.glide_iv);
// Load a Web picture into the ImageView Medium, very convenient
//Glide Of with Method not only accepts Context And accept Activity and Fragment Wait, Context They are automatically retrieved and easy to use
Glide.with(this).load(URL).into(glide_iv);
glide_lv = (ListView) findViewById(R.id.glide_lv);
glide_lv.setAdapter(new BaseAdapter() {
@Override
public View getView(int arg0, View contentView, ViewGroup arg2) {
ViewHolder holder=null;
if (contentView == null) {
holder=new ViewHolder();
contentView= LayoutInflater.from(
MainActivity.this).inflate(R.layout.my_image_view,
null);
holder.itemIv = contentView.findViewById(R.id.item_iv);
contentView.setTag(holder);
} else {
holder=(ViewHolder) contentView.getTag();
}
String url = urls.get(arg0);
// stay ListView Load list pictures in
Glide.with(MainActivity.this).load(url).centerCrop()
.placeholder(R.drawable.ic_launcher).crossFade()
.into(holder.itemIv);
return contentView;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return urls.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return urls.size();
}
class ViewHolder{
ImageView itemIv;
}
});
}
/**
* Add data
*/
private void initDatas() {
for (int i = 0; i < 5; i++) {
urls.add(URL);
}
}
}
Layout file:
<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:padding="20dp" >
<ImageView
android:id="@+id/glide_iv"
android:layout_width="80dp"
android:layout_height="80dp" />
<ListView
android:id="@+id/glide_lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/glide_iv">
</ListView>
</RelativeLayout>
Finally, remember to add network permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" >
This is the whole content of this article, and I hope it will be helpful for everyone to learn.