SmartImageView
2016-12-21 12:06
190 查看
SmartImageView的设计初衷是来取代Android自带的ImgageView组件,但它的功能远不只imageview这么简单,它还提供了一些ImageView远远没有却常常在Android应用中经常用到的功能,如:
(1)支持通过URL来加载图片;
(2)支持从电话簿中加载图片;
(3)异步加载图片;
(4)图片被缓存在内存,以便下次快速加载显示;
(5)SmartImageView类可以被很容易扩展成对其它资源的调用使用;
在做一些需要从网上获取图片的APP时,就难免要做很多处理。
这个项目就是针对这些做了很多处理。
4000
Github项目地址:https://github.com/loopj/android-smart-image-view
作者地址:http://loopj.com/android-smart-image-view/
XML添加一个控件
[html] view
plain copy
print?
<com.loopj.android.image.SmartImageView
android:id="@+id/smart_image"
android:layout_above="@id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
/>
获取引用
mImageView = (SmartImageView)findViewById(R.id.smart_image);
获取网络图片,这个过程本身就是异步。不必再进行处理,也不必担心线程阻塞
网络获取到的图片都进行了缓存的处理。会在程序的cache目录下建/web_image_cache/,图片存在这里,如下图所示:
下次使用的时候,如果缓存图片已经存在,则不再从网络获取图片
mImageView.setImageUrl("http://d.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f5a4c0234334e251f95ca5f72.jpg");
有一些功能,作者主页并没有说明,但是查看源码可以看到
先看.setImageUrl都有什么方法
1、最普通的一个,直接设置图片地址
// Helpers to set image by URL
public void setImageUrl(String url) {
setImage(new WebImage(url));
}
2、有一个接口,完成下载的时候调用
public void setImageUrl(String url,SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),completeListener);
}
3、从字面意思可以看出,是一个备用的资源。如果从网络获取图片失败,则使用备用资源
public void setImageUrl(String url, finalInteger fallbackResource) {
setImage(new WebImage(url),fallbackResource);
}
4、类似上面
public void setImageUrl(String url, finalInteger fallbackResource, SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),fallbackResource, completeListener);
}
5、多了一个loadingResource,就是正在下载的时候展示的图片
public void setImageUrl(String url, finalInteger fallbackResource, final Integer loadingResource) {
setImage(new WebImage(url),fallbackResource, loadingResource);
}
6、类似上面
public
1c140
void setImageUrl(String url, finalInteger fallbackResource, final Integer loadingResource,SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),fallbackResource, loadingResource, completeListener);
}
SmartImageView确实很方便,能解决大部分问题。有不符合自己要求的地方,还可以根据源码去修改。
项目运行示意图:第1张为开始界面,第二张为网络浏览,第三张为通讯录浏览
下面是项目实现:
项目目录结构图:
其中主布局文件activity_main.xml为
[html] view
plain copy
print?
<strong><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:orientation="vertical" >
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/net_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/net_button_text"
android:onClick="onClick"
style=""
/>
<Button
android:id="@+id/phone_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone_button_text"
android:onClick="onClick"
style=""
/>
<TextView
android:id="@+id/contact_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<com.loopj.android.image.SmartImageView
android:id="@+id/smart_image"
android:layout_above="@id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
/>
</RelativeLayout></strong>
主Activity的java文件为:
[java] view
plain copy
print?
package com.shen.smartimageview;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.loopj.android.image.SmartImageView;
public class MainActivity extends Activity {
private static final String mWebPath = "http://d.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f5a4c0234334e251f95ca5f72.jpg";
private static final Uri CONTACTS_URI = ContactsContract.Contacts.CONTENT_URI;
private static final String _ID = ContactsContract.Contacts._ID;
private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
private SmartImageView mImageView;
private TextView mIdName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 开源代码实现好的SmartImageView
mImageView = (SmartImageView) findViewById(R.id.smart_image);
mIdName = (TextView)findViewById(R.id.contact_id);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.net_button:
// setImageUrl(String url, Integer fallbackResource, Integer
// loadingResource)
// fallbackResource:图片下载失败时显示的图片 loadingResource:图片正在下载显示的图片
mImageView.setImageUrl(mWebPath, R.drawable.ic_launcher,
R.drawable.ic_launcher);
break;
case R.id.phone_button:
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(CONTACTS_URI, null, null, null, null);
c.moveToFirst();
long _id = c.getLong(c.getColumnIndex(_ID));
String displayName = c.getString(c.getColumnIndex(DISPLAY_NAME));
Log.d("id", String.valueOf(_id) + displayName);
mIdName.setText("name:" + displayName);
mImageView.setImageContact(_id,R.drawable.ic_launcher,R.drawable.ic_launcher);
break;
default:
break;
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
其他的文件可以从上面github或作者的网站上进行下载在这里就不多说了
下面介绍一下对smartImageView的认识
转自:http://blog.csdn.NET/cqtddt/article/details/42044875
(1)支持通过URL来加载图片;
(2)支持从电话簿中加载图片;
(3)异步加载图片;
(4)图片被缓存在内存,以便下次快速加载显示;
(5)SmartImageView类可以被很容易扩展成对其它资源的调用使用;
在做一些需要从网上获取图片的APP时,就难免要做很多处理。
这个项目就是针对这些做了很多处理。
4000
Github项目地址:https://github.com/loopj/android-smart-image-view
作者地址:http://loopj.com/android-smart-image-view/
XML添加一个控件
[html] view
plain copy
print?
<com.loopj.android.image.SmartImageView
android:id="@+id/smart_image"
android:layout_above="@id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
/>
获取引用
mImageView = (SmartImageView)findViewById(R.id.smart_image);
获取网络图片,这个过程本身就是异步。不必再进行处理,也不必担心线程阻塞
网络获取到的图片都进行了缓存的处理。会在程序的cache目录下建/web_image_cache/,图片存在这里,如下图所示:
下次使用的时候,如果缓存图片已经存在,则不再从网络获取图片
mImageView.setImageUrl("http://d.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f5a4c0234334e251f95ca5f72.jpg");
有一些功能,作者主页并没有说明,但是查看源码可以看到
先看.setImageUrl都有什么方法
1、最普通的一个,直接设置图片地址
// Helpers to set image by URL
public void setImageUrl(String url) {
setImage(new WebImage(url));
}
2、有一个接口,完成下载的时候调用
public void setImageUrl(String url,SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),completeListener);
}
3、从字面意思可以看出,是一个备用的资源。如果从网络获取图片失败,则使用备用资源
public void setImageUrl(String url, finalInteger fallbackResource) {
setImage(new WebImage(url),fallbackResource);
}
4、类似上面
public void setImageUrl(String url, finalInteger fallbackResource, SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),fallbackResource, completeListener);
}
5、多了一个loadingResource,就是正在下载的时候展示的图片
public void setImageUrl(String url, finalInteger fallbackResource, final Integer loadingResource) {
setImage(new WebImage(url),fallbackResource, loadingResource);
}
6、类似上面
public
1c140
void setImageUrl(String url, finalInteger fallbackResource, final Integer loadingResource,SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),fallbackResource, loadingResource, completeListener);
}
SmartImageView确实很方便,能解决大部分问题。有不符合自己要求的地方,还可以根据源码去修改。
项目运行示意图:第1张为开始界面,第二张为网络浏览,第三张为通讯录浏览
下面是项目实现:
项目目录结构图:
其中主布局文件activity_main.xml为
[html] view
plain copy
print?
<strong><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:orientation="vertical" >
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="@+id/net_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/net_button_text"
android:onClick="onClick"
style=""
/>
<Button
android:id="@+id/phone_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone_button_text"
android:onClick="onClick"
style=""
/>
<TextView
android:id="@+id/contact_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<com.loopj.android.image.SmartImageView
android:id="@+id/smart_image"
android:layout_above="@id/button_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
/>
</RelativeLayout></strong>
主Activity的java文件为:
[java] view
plain copy
print?
package com.shen.smartimageview;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.loopj.android.image.SmartImageView;
public class MainActivity extends Activity {
private static final String mWebPath = "http://d.hiphotos.baidu.com/image/pic/item/838ba61ea8d3fd1f5a4c0234334e251f95ca5f72.jpg";
private static final Uri CONTACTS_URI = ContactsContract.Contacts.CONTENT_URI;
private static final String _ID = ContactsContract.Contacts._ID;
private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
private SmartImageView mImageView;
private TextView mIdName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 开源代码实现好的SmartImageView
mImageView = (SmartImageView) findViewById(R.id.smart_image);
mIdName = (TextView)findViewById(R.id.contact_id);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.net_button:
// setImageUrl(String url, Integer fallbackResource, Integer
// loadingResource)
// fallbackResource:图片下载失败时显示的图片 loadingResource:图片正在下载显示的图片
mImageView.setImageUrl(mWebPath, R.drawable.ic_launcher,
R.drawable.ic_launcher);
break;
case R.id.phone_button:
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(CONTACTS_URI, null, null, null, null);
c.moveToFirst();
long _id = c.getLong(c.getColumnIndex(_ID));
String displayName = c.getString(c.getColumnIndex(DISPLAY_NAME));
Log.d("id", String.valueOf(_id) + displayName);
mIdName.setText("name:" + displayName);
mImageView.setImageContact(_id,R.drawable.ic_launcher,R.drawable.ic_launcher);
break;
default:
break;
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
其他的文件可以从上面github或作者的网站上进行下载在这里就不多说了
下面介绍一下对smartImageView的认识
转自:http://blog.csdn.NET/cqtddt/article/details/42044875
相关文章推荐
- SmartImageView使用
- 【Java&Android开源库代码剖析】のandroid-smart-image-view
- Android Studio导入Eclipse开源项目(SmartImageView为例)
- github开源项目smart-image-view
- Android核心基础-7.Android 网络通信-3.SmartImageView请求网络图片
- SmartImageView的使用
- GET 与 POST,Handler,JSON 解析,Tomcat,导入第三方包 SmartImageView
- 【Java&Android开源库代码剖析】のandroid-smart-image-view
- SmartImageView重新实现网络图片的加载
- SmartImageView
- Android图像开源视图:SmartImageView
- SmartImageView的简单使用
- Android之使用SmartImageView加载图片
- Android Smart Image View
- 安卓开源项目一1 SmartImageView
- Android图像开源视图:SmartImageView
- android-smart-image-view源码分析
- Android学习(35) -- smart-image-view 获取网络图片
- android_24_下载图片2_SmartImageView
- Android--Android图像开源视图:SmartImageView