Glide、Picasso框架載入圖片——ImageView中、ListView中
阿新 • • 發佈:2019-01-04
新增許可權
<uses-permission android:name="android.permission.INTERNET"/>
//引入glide
compile 'com.github.bumptech.glide:glide:3.7.0'
//引入picasso
compile 'com.squareup.picasso:picasso:2.5.2'
listViewAdapter
package com.zdsoft.glide0112; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import com.bumptech.glide.Glide; import java.net.URL; import java.util.List; /** * ListView載入圖片Adapter * Created by Administrator on 2017/1/12. */ public class ListAdapter extends BaseAdapter { private List<URL> list; private Context context; public ListAdapter(Context context,List<URL> list){ this.context=context; this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder=null; if (convertView==null){ holder=new ViewHolder(); convertView=LayoutInflater.from(context).inflate(R.layout.list_item,null); holder.iv_list= (ImageView) convertView.findViewById(R.id.iv_list); convertView.setTag(holder); }else { holder= (ViewHolder) convertView.getTag(); } String url= String.valueOf(list.get(position)); Glide.with(context) .load(url) .centerCrop() .placeholder(R.drawable.a1) .into(holder.iv_list); return convertView; } private class ViewHolder{ ImageView iv_list; } }
activity
package com.zdsoft.glide0112; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import android.widget.ListView; import com.bumptech.glide.Glide; import com.squareup.picasso.Picasso; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ImageView iv_show; private ListView lv_show; private List<URL> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initDate(); //Glide載入網路圖片 Glide.with(this) .load("http://images.quanjing.com/mhrf005/high/mhrf-dspd54195f2.jpg") .placeholder(R.drawable.a1)//載入過程中圖片展示 .error(R.drawable.a2)//載入失敗圖片展示 .centerCrop()//影象位於檢視的中央 .crossFade()//淡入淡出 .skipMemoryCache(true)//true表示Glide 跳過記憶體快取,預設為false .dontAnimate()//取消動畫 .into(iv_show); //Picasso載入網路圖片 // Picasso.with(this) // .load("http://images.quanjing.com/mhrf005/high/mhrf-dspd54195f20.jpg") // .into(iv_show); } private void initView() { iv_show = (ImageView) findViewById(R.id.iv_show); lv_show = (ListView) findViewById(R.id.lv_show); } /** * ListView載入圖片 */ private void initDate() { ListAdapter adapter = new ListAdapter(MainActivity.this, getList()); lv_show.setAdapter(adapter); } /** * 資料來源 * * @return */ private List<URL> getList() { list = new ArrayList<URL>(); try { URL url1 = new URL("https://imgsa.baidu.com/baike/crop%3D0%2C2%2C900%2C594%3Bc0%3D" + "baike116%2C5%2C5%2C116%2C38/sign=52da68a46859252db758474409ab2f09" + "/d62a6059252dd42aa3178b150b3b5bb5c9eab876.jpg"); URL url2 = new URL("https://imgsa.baidu.com/baike/c0%3Dbaike92%2C5%2C5%2C92%2C30" + "/sign=9de722ea4934970a537e187df4a3baad/29381f30e924b8999c3593fd66061" + "d950b7bf6ee.jpg"); URL url3 = new URL("http://images.quanjing.com/mhrf005/high/mhrf-dspd54195f20.jpg"); list.add(url1); list.add(url2); list.add(url3); } catch (MalformedURLException e) { e.printStackTrace(); } return list; } }