第三方开源组件
com.loopj.android.image.SmartImageView,
开源项目,支持从URL和通讯录中获取图像,可以替代Android标准的ImageView
功能:
根据URL地址装载图像;
支持装载通讯录中的图像;
支持异步装载;
支持缓存;
在项目开发中,加载图片不可避免,自己不得不写图片从网上加载的方法,还要写缓存,过程相对复杂。能否有一种组件能封装好这些通用功能,今天介绍的开源项目,smartImageVIew就是这样一种控件。
项目运行截图
直接看实例 代码
布局文件html_picture.xml
<RelativeLayout 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: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=".NavActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="watch"
android:text="Button" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:lines="1"
android:text="http://g.hiphotos.baidu.com/image/w%3D2048/sign=514125d97e3e6709be0042ff0fff9d3d/962bd40735fae6cd8d4e729f0eb30f2443a70f52.jpg" >
<com.loopj.android.image.SmartImageView
android:id="@+id/iv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_alignParentTop="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
NetPicWatcher.class;
package com.example.netpicwatcher;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.loopj.android.image.SmartImageView;
/**
*
* @ClassName: NetPicWatcher
* @Description: TODO(图片查看器)
* @author daodao ( dao.dev@qq.com )
* @date 2014年11月10日 下午4:38:18
*
*/
public class NetPicWatcher extends Activity {
protected static final int CHANGE_UI = 0;
protected static final int ERROR = 1;
EditText eturl;
SmartImageView siv; //开源框架
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.html_picture);
eturl = (EditText) findViewById(R.id.editText1);
siv = (SmartImageView) findViewById(R.id.iv);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void watch(View view) {
final String strUrl = eturl.getText().toString().trim();
if ((TextUtils.isEmpty(strUrl))) {
Toast.makeText(NetPicWatcher.this, "输入路径为空!!!", 1).show();
return;
}
else {
siv.setImageUrl(strUrl, R.drawable.ic_launcher,
R.drawable.ic_launcher);
}
}
}
作者的项目主页,里面有使用方法。见http://loopj.com/android-smart-image-view/
下载作者的jar包导入项目后,在xml中加入控件
<com.loopj.android.image.SmartImageView android:id="@+id/my_image" />
代码里找到该控件
SmartImageView myImage = (SmartImageView) this.findViewById(R.id.my_image);
使用控件
通过url加载图片
myImage.setImageUrl("http://www.awesomeimages.com/myawesomeimage.jpg");
加载通讯录的图片
myImage.setImageContact(contactAddressBookId);
github上面有源码,需要的可以看看:https://github.com/loopj/android-smart-image-view