• Android开源框架——Picasso


    开篇——介绍Picasso

    (Picasso是什么?)Picasso:A Powerfull Image Downloading and Caching Library for Android,即Android平台的网络图片下载和缓存框架。

    (Picasso如何使用?)框架嘛!既然牛人能够写出这个框架,自然使用流畅。不用担心,很简单,但深入源代码就需要花点功夫。

    (为什么会出现Picasso框架?)Android开发中,常需要从远程服务器端获取图片;一般情况下,我们会使用HttpURLConnection(java.net.HttpURLConnection)和AsyncTask(android.os.AsyncTask)结合实现该功能;但还需要考虑其他情况:图片缓存和下载管理(??),因此聪明的做法是:封装成自己的工具类,或者是使用第三方框架(比如这里的Picasso)。

    热身运动——HttpUrlConnection和AsyncTask实现网络图片下载

    在使用Picasso框架之前,先来一个热身运动:使用HttpUrlConnection(java.net.HttpUrlConnection)和AsyncTask(android.os.AsyncTask)实现网络图片加载。

    activity_main.xml文件方面,LinearLayout的垂直布局中布置两个控件:ImageView和Button;Button的点击事件触发网络图片请求和加载内存(即:显示图片)。

    <LinearLayout 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:orientation="vertical"
        tools:context=".MainActivity" >
    
        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="200dp"
            android:layout_height="200dp" />
    
        <Button
            android:onClick="loadImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="loading..." />
    
    </LinearLayout>

    AndroidManifest.xml文件方面,添加网络请求的用户权限。

    <uses-permission android:name="android.permission.INTERNET"/>

    MainActivity.java方面:

    package com.fenix.picassodemo;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.view.View;
    import android.widget.ImageView;
    
    public class MainActivity extends Activity {
    
        private ImageView    mIView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            mIView = (ImageView) findViewById(R.id.iv_image);
        }
    
        private class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
    
            ImageView    iv;
    
            public ImageDownloadTask(ImageView iv) {
                this.iv = iv;
            }
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }
    
            @Override
            protected Bitmap doInBackground(String... imgAddress) {
                URL imgUrl = null;
                HttpURLConnection connection = null;
                InputStream inputStream = null;
                Bitmap bitmap = null;
    
                try {
                    imgUrl = new URL(imgAddress[0]); // 将网络图片路径转化为URL实例
    
                    connection = (HttpURLConnection) imgUrl.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
    
                    inputStream = connection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(inputStream);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                return bitmap;
            }
    
            @Override
            protected void onPostExecute(Bitmap result) {
                super.onPostExecute(result);
    
                iv.setImageBitmap(result);
            }
        }
    
        public void loadImage(View v) {
            new ImageDownloadTask(mIView).execute("http://pic32.nipic.com/20130829/12906030_124355855000_2.png");
        }
    
    }

    点击布局文件中的Button,执行loadImage(),即网络图片请求;网络请求不应该被写在主线程中,因此需要借助AsyncTask工具(封装Handler和Thread);一旦点击Button,即执行ImageDownloadTask。

    图片资源来自:www.baidu.com的图片库(右键-》复制图片地址)。

    AsyncTask结合HttpURLConnection,确实可以实现网络图片的请求,但问题是:Android应用程序(从网络图片角度),还比如:管理图片下载请求,合理使用内存等方面都需要考虑(Google工程师——极致体验的代言人)。这就是为什么出现了这么多类似于Picasso图片请求框架的缘故!当然还有很多开源框架需要熟悉(后续再说呗)。

    Picasso框架——初体验

  • 相关阅读:
    自定义UITableViewCell实现类似iBooks图书列表形式
    苹果有默认的手势识别,左右移动
    ios4 private framework
    svn 重定向
    代码合集
    CHM无法显示内容
    全面优化Windows2003
    .Net 2003使用数据库事务对象注意
    什么是托管代码和托管数据
    概念:反射和序列化
  • 原文地址:https://www.cnblogs.com/CVstyle/p/5628640.html
Copyright © 2020-2023  润新知