为了账号安全,请及时绑定邮箱和手机立即绑定

imgview加载的三种方式

标签:
Android

1. [代码]普通加载网络方式     

?


public class NormalLoadPictrue {         private String uri;    private ImageView imageView;    private byte[] picByte;              public void getPicture(String uri,ImageView imageView){        this.uri = uri;        this.imageView = imageView;        new Thread(runnable).start();    }         @SuppressLint("HandlerLeak")    Handler handle = new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            if (msg.what == 1) {                if (picByte != null) {                    Bitmap bitmap = BitmapFactory.decodeByteArray(picByte, 0, picByte.length);                    imageView.setImageBitmap(bitmap);                }            }        }    };     Runnable runnable = new Runnable() {        @Override        public void run() {            try {                URL url = new URL(uri);                HttpURLConnection conn = (HttpURLConnection)url.openConnection();                conn.setRequestMethod("GET");                conn.setReadTimeout(10000);                                 if (conn.getResponseCode() == 200) {                    InputStream fis =  conn.getInputStream();                    ByteArrayOutputStream bos = new ByteArrayOutputStream();                    byte[] bytes = new byte[1024];                    int length = -1;                    while ((length = fis.read(bytes)) != -1) {                        bos.write(bytes, 0, length);                    }                    picByte = bos.toByteArray();                    bos.close();                    fis.close();                                         Message message = new Message();                    message.what = 1;                    handle.sendMessage(message);                }                                              }catch (IOException e) {                e.printStackTrace();            }        }    };     }

2. [代码]用ImageLoader加载图片     

?


public class ImageLoaderPicture {         private DisplayImageOptions options;     public ImageLoaderPicture(Context context) {                 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).threadPriority(Thread.NORM_PRIORITY - 2)        .denyCacheImageMultipleSizesInMemory()        .discCacheFileNameGenerator(new Md5FileNameGenerator())        .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging()         .memoryCache(new WeakMemoryCache())                                         .build();        ImageLoader.getInstance().init(config);                 options = new DisplayImageOptions.Builder()        .showStubImage(0)        .showImageForEmptyUri(0)        .showImageOnFail(0)        .cacheInMemory().cacheOnDisc()        .imageScaleType(ImageScaleType.IN_SAMPLE_INT)        .bitmapConfig(android.graphics.Bitmap.Config.RGB_565)        .build();    }     public DisplayImageOptions getOptions() {        return options;    }     public void setOptions(DisplayImageOptions options) {        this.options = options;    }          }

3. [代码]用Volley加载图片     

?


public class VolleyLoadPicture {         private ImageLoader mImageLoader = null;    private BitmapCache mBitmapCache;         private ImageListener one_listener;         public VolleyLoadPicture(Context context,ImageView imageView){        one_listener = ImageLoader.getImageListener(imageView, 0, 0);                 RequestQueue mRequestQueue = Volley.newRequestQueue(context);        mBitmapCache = new BitmapCache();        mImageLoader = new ImageLoader(mRequestQueue, mBitmapCache);    }     public ImageLoader getmImageLoader() {        return mImageLoader;    }     public void setmImageLoader(ImageLoader mImageLoader) {        this.mImageLoader = mImageLoader;    }     public ImageListener getOne_listener() {        return one_listener;    }     public void setOne_listener(ImageListener one_listener) {        this.one_listener = one_listener;    }         class BitmapCache implements ImageCache {        private LruCache<String, Bitmap> mCache;        private int sizeValue;                 public BitmapCache() {            int maxSize = 10 * 1024 * 1024;            mCache = new LruCache<String, Bitmap>(maxSize) {                @Override                protected int sizeOf(String key, Bitmap value) {                    sizeValue = value.getRowBytes() * value.getHeight();                    return sizeValue;                }                             };        }         @Override        public Bitmap getBitmap(String url) {            return mCache.get(url);        }         @Override        public void putBitmap(String url, Bitmap bitmap) {            mCache.put(url, bitmap);        }    }      }

4. [代码]Activity     

?


public class MainActivity extends Activity {         private ImageView imageView001,imageView002,imageView003;         public static final String picUrl = "http://img.quwenjiemi.com/2014/0701/thumb_420_234_20140701112917406.jpg";    //public static final String picUrl = "http://192.168.1.181:8081/AndroidSerivces/house.jpg";         @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imageView001 = (ImageView)findViewById(R.id.imageView001);        imageView002 = (ImageView)findViewById(R.id.imageView002);        imageView003 = (ImageView)findViewById(R.id.imageView003);                 //用普通方法加载图片        new NormalLoadPictrue().getPicture(picUrl,imageView001);                 //用ImageLoader加载图片        ImageLoader.getInstance().displayImage(picUrl, imageView002,new ImageLoaderPicture(this).getOptions(),new SimpleImageLoadingListener());                 //用Volley加载图片        VolleyLoadPicture vlp = new VolleyLoadPicture(this, imageView003);        vlp.getmImageLoader().get(picUrl, vlp.getOne_listener());    }      }

5. [代码]布局文件     

?


<ScrollView 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"    tools:context=".MainActivity" >     <RelativeLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:padding="10dp">         <TextView         android:id="@+id/textView001"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="1.用普通方法的加载图片"/>         <ImageView         android:id="@+id/imageView001"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/textView001"/>              <TextView         android:id="@+id/textView002"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/imageView001"         android:text="2.用Android-Universal-Image-Loader加载图片"/>         <ImageView         android:id="@+id/imageView002"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:layout_below="@+id/textView002"/>              <TextView         android:id="@+id/textView003"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/imageView002"         android:text="3.用Volley加载图片"/>         <ImageView         android:id="@+id/imageView003"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:layout_below="@+id/textView003"/>         </RelativeLayout> </ScrollView>

原文链接:http://www.apkbus.com/blog-839077-68111.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消