您的位置:首页 > 其它

开源项目smartImageView

2016-04-24 22:19 429 查看
https://github.com/网址上可以下载很多开源项目源代码

smartImageView继承了安卓的ImageView,增强了它的功能,例如直接通过url显示图片(内部使用线程去GET图片)

使用方法

1、把smartImageView的源代码/src/com文件夹拷到自己的代码的src目录中

2、布局文件中添加一个smartImageView控件,注意要写全名(包含包名)

<--包名要写完整-->
<com.itheima.smartimageview.SmartImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv" />

3、获取控件并调用image.setImageUrl(item.getImage())方法加载图片
smartImageView smart_img = (smartImageViewfindViewById(R.id.smart_img);

//直接通过url显示图片

//smart_img.setImagUrl("http://img0.bdstatic.com/img/image/imglog_detailHLT.gif");

//直接通过url显示图片,如果url图片得不到,则显示本地图片

smart_img.setImagUrl("http://img0.bdstatic.com/img/image/imglog_detailHLT.gif",R.drawable.mydefault);

代码示例

package com.itheima.custom.smartimgview;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.ImageView;

/**
* 自定义 smartimageview
*
* @author Administrator
*
*/
public class MySmartImageView extends ImageView {

private Bitmap bitmap;
//定义一个 handler
private Handler handler = new Handler(){

//重写handleMessage 方法
public void handleMessage(android.os.Message msg) {
int	msgg = msg.what;

switch (msgg) {
case 0:
MySmartImageView.this.setImageBitmap(bitmap);
break;

case 1:
int errorResource = (Integer) msg.obj;
MySmartImageView.this.setImageResource(errorResource);
default:
break;
}

};
};

public MySmartImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}

// 设置 图片的url
public void setImagUrl(final String path){

new Thread(){

public void run() {

try {
URL url = new URL(path);
//打开一个url连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置conn 的参数
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//获取服务器返回的状态码
int code = conn.getResponseCode();
if (code == 200) { //200 请求服务器资源全部返回成功     //206 请求部分服务器资源返回成功
InputStream inputStream = conn.getInputStream(); //获取服务器返回的数据
//获取到bitmap
bitmap = BitmapFactory.decodeStream(inputStream);
handler.sendEmptyMessage(0);  //发送一条消息
}

} catch (Exception e) {

e.printStackTrace();
}

};}.start();

}

// 设置 图片的url
public void setImagUrl(final String path,final int errorResource){

new Thread(){

public void run() {

try {
URL url = new URL(path);
//打开一个url连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//设置conn 的参数
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
//获取服务器返回的状态码
int code = conn.getResponseCode();
if (code == 200) { //200 请求服务器资源全部返回成功     //206 请求部分服务器资源返回成功
InputStream inputStream = conn.getInputStream(); //获取服务器返回的数据
//获取到bit map

bitmap = BitmapFactory.decodeStream(inputStream);

handler.sendEmptyMessage(0);  //发送一条消息

}else {
Message msg = Message.obtain();
msg.obj = errorResource;
msg.what = 1;
handler.sendMessage(msg);

}

} catch (Exception e) {

e.printStackTrace();
}

};}.start();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  smartImageView