SmartImageView的設計初衷是來取代Android自帶的ImgageView組件,但它的功能遠不只imageview這麼簡單,它還提供了一些ImageView遠遠沒有卻常常在Android應用中經常用到的功能,如:
(1)支持通過URL來加載圖片;
(2)支持從電話簿中加載圖片;
(3)異步加載圖片;
(4)圖片被緩存在內存,以便下次快速加載顯示;
(5)SmartImageView類可以被很容易擴展成對其它資源的調用使用;
在做一些需要從網上獲取圖片的APP時,就難免要做很多處理。
這個項目就是針對這些做了很多處理。
Github項目地址:https://github.com/loopj/android-smart-image-view
作者地址:http://loopj.com/android-smart-image-view/
XML添加一個控件
- <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 void setImageUrl(String url, finalInteger fallbackResource, final Integer loadingResource,SmartImageTask.OnCompleteListener completeListener) {
setImage(new WebImage(url),fallbackResource, loadingResource, completeListener);
}
SmartImageView確實很方便,能解決大部分問題。有不符合自己要求的地方,還可以根據源碼去修改。
項目運行示意圖:第1張爲開始界面,第二張爲網絡瀏覽,第三張爲通訊錄瀏覽
下面是項目實現:
項目目錄結構圖:
其中主佈局文件activity_main.xml爲
- <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文件爲:
- 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