android-smart-image-view图片加载简单使用

这个工具类是带缓存功能的图片加载工具类,它自己会在新线程中进行操作。

下载地址:工具类下载地址

1.首先将工具类的com文件夹拷贝到工程中。

2.图片组件不再用ImageView,而是使用com.loopj.android.image.SmartImageView

3.调用contentIV.setImageUrl("","","")进行加载图片     也可以不设置后面的两个参数

    第一个参数:图片的Url

    第二个参数:图片地址错误时候显示的图片

    第三个参数:图片正在加载的时候显示的图片  

4.如果使用自定义加载图片工具类的话,记得配置外存储设备的写权限,因为缓存的时候会是将图片缓存到SD卡上了。如果使用SmartImageView就不需要配置了,因为它缓存到了当前应用包下。

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


import com.loopj.android.image.SmartImageView;

import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private EditText pathET;
//	private ImageView contentIV;
	private NetService service;
	private Handler handler = new Handler();
	private SmartImageView contentIV;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		service = new NetService();
		pathET = (EditText) findViewById(R.id.pathET);
		contentIV = (SmartImageView) findViewById(R.id.contentIV);
	}
	
	public void go(View view){
		/*
		 * 第一个参数:图片的Url
		 * 第二个参数:图片地址错误时候显示的图片
		 * 第三个参数:图片正在加载的时候显示的图片  
		 */
		contentIV.setImageUrl(pathET.getText().toString(),android.R.drawable.ic_delete,android.R.drawable.ic_popup_sync);
		//注释是使用自定义加载图片的工具类,如果使用SmartImageView  上面一句话即可。
		/*new Thread(){
			public void run() {
				try {
					String path = pathET.getText().toString();
					final Bitmap bitmap = service.getImage(path);
					handler.post(new Runnable() {
						@Override
						public void run() {
							contentIV.setImageBitmap(bitmap);
						}
					});
				} catch (Exception e) {
					Toast.makeText(getApplicationContext(), "服务器忙!", Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}
			};
		}.start();*/
	}
}



使用工具类加载图片这个类就不需要写了,这个类是自定义加载图片的工具类
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * 自定义缓存并显示图片的类
 * @author Administrator
 *
 */
public class NetService {

	public Bitmap getImage(String path) throws Exception {
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		File file = new File("/mnt/sdcard/"+URLEncoder.encode(path,"UTF-8"));
		if(file.exists()){//如果该文件存在,则在消息头上加上此文件最后修改日期,让它和服务器上的文件的最后修改日期进行对比,如果不一样则要进行加载,如果一样则使用缓存
			conn.setIfModifiedSince(file.lastModified());
		}
		
		int code = conn.getResponseCode();
		if(code==200){
			InputStream is = conn.getInputStream();
			int len = 0;
			byte[] buff = new byte[1024];
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			while((len=is.read(buff))!=-1){
				bos.write(buff, 0, buff.length);
			}
			Bitmap bitmap = BitmapFactory.decodeByteArray(bos.toByteArray(), 0, bos.toByteArray().length);
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(bos.toByteArray());
			fos.close();
			bos.close();
			is.close();
			return bitmap;
		}else if(code==304){//如果最后修改日期一样,则使用本地缓存文件
			return BitmapFactory.decodeFile(file.getAbsolutePath());
		}
		throw new RuntimeException("返回图片异常");
	}
}


布局

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xxc.nettext.MainActivity$PlaceholderFragment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/pathET"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textUri" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="go"
            android:text="GO" />
    </LinearLayout>

    <com.loopj.android.image.SmartImageView
        android:id="@+id/contentIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值