开源项目smartImageView
2016-04-24 22:19
429 查看
在https://github.com/网址上可以下载很多开源项目源代码
smartImageView继承了安卓的ImageView,增强了它的功能,例如直接通过url显示图片(内部使用线程去GET图片)
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);
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初接触
- Android之使用SmartImageView加载图片
- android_26_AlphaListDemo
- android_24_下载图片2_SmartImageView
- 获取互联网上的图片并显示在界面上
- 新闻客户端——SmartImageView——AsyncHttpClient
- Android Studio导入Eclipse开源项目(SmartImageView为例)
- SmartImageView
- 一个算法题目的两种不同效率级别的算法对比
- static
- [改善Java代码]使用package-info类为包服务
- PYTHON之路(十一) - sqlalchemy
- xhprof 安装使用
- 冲刺阶段第七天
- 20145105 《Java程序设计》第8周学习总结
- 20145307第八周JAVA学习报告
- 冲刺第七天
- MySQL 数据库
- ASP.NET 使用Ajax 请求 aspx.cs 中的方法
- 【树】树的重量