Android使用开源框架加载图片

Android开发时,有时候需要们来加载网络图片,我们可以通过api的方式进行加载,但是前几天做的时候,发现了一个优秀的开源框架,可以帮助我们非常简单便捷的进行图片的加载,所以记录一下。

我所用的是:

android-smart-image-view

在github上的地址是:https://github.com/loopj/android-smart-image-view,我们可以直接进行搜索,github对于我们程序员来说简直是宝库啊,一定要能够擅长应用。

下载下来后,我们把其目录下的src下的以com开头的文件夹拷贝到我们工程中,这样我们就能引用相应的方法了。

由于我们要显示一整图片,所以这里在main.xml中我们需要定义的组件是。

这里要看清楚,我的imageview不是系统api下的imageview而是引用的刚才下载的开源的smartimageview,所以这里的组件定义方式步以前的不一样,这要能记着。

    <com.loopj.android.image.SmartImageView
        android:id="@+id/siv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:text=""
        android:id="@+id/et_path"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入图片位置" />
    
    <Button
        android:onClick="click"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="浏览" />

然后再acvity中,我们这样进行按钮点击时间的实现。

这里调用的方法setImageUrl(String url, final Integer fallbackResource, final Integer loadingResource)

url:获取网络图片的地址。

fallbackResource:下载失败显示的图片

loadingResource:下载中显示的图片。

  public void click(View view){
        SmartImageView siv = (SmartImageView) findViewById(R.id.siv);
        siv.setImageUrl(et_path.getText().toString().trim()
                ,R.drawable.ic_launcher,R.drawable.ic_launcher);
    }

这样很简单的,我们就实现了,网络图片的加载。

作者:Darren

微博:@IT_攻城师

github:@Darren90

出处:http://www.cnblogs.com/fengtengfei/

原文地址:https://www.cnblogs.com/fengtengfei/p/3970598.html