开源项目smartImageView

来源:互联网 发布:c语言贪吃蛇程序代码 编辑:程序博客网 时间:2024/06/05 19:58

在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;//定义一个 handlerprivate Handler handler = new Handler(){//重写handleMessage 方法 public void handleMessage(android.os.Message msg) {    intmsgg = 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();}}



0 0
原创粉丝点击