加载图片防止错位需注意

来源:互联网 发布:linux .sh 里面的函数 编辑:程序博客网 时间:2024/06/04 19:43

1。定义接口

public interface jiekou {    public void zhi(Bitmap b, ImageView imageView);}
2.在加载图片的类中赋值
private ImageView image;public void showima(final ImageView image, final String path, final jiekou jiekou){    AsyncTask<Void, Void, Bitmap> asyncTask = new AsyncTask<Void, Void, Bitmap>() {        @Override        protected Bitmap doInBackground(Void... voids) {            try {                URL url = new URL(path);                HttpClient client=new DefaultHttpClient();                HttpGet hg=new HttpGet(path);                HttpResponse execute = client.execute(hg);                int code = execute.getStatusLine().getStatusCode();                if (code== 200){                    InputStream inputStream = execute.getEntity().getContent();                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);                    return  bitmap;                }            } catch (Exception e) {                e.printStackTrace();            }            return null;        }        @Override        protected void onPostExecute(Bitmap s) {           jiekou.zhi(s,image);        }    };    //调用    asyncTask.execute();}
3.adp中接收值
 new imaHolder().showima(vh.ima,data.get(i).getPic_url(), new jiekou() {      @Override     public void zhi(Bitmap b, ImageView imageView) {               if (vh.ima.getTag().equals(data.get(i).getPic_url()))               {                   imageView.setImageBitmap(b);          }      }});