SmartImageView主要是爲了加速從網上加載圖片,支持根據URL地址加載圖片,支持異步加載圖片,支持圖片緩存等。
下載地址 http://loopj.com/android-smart-image-view/
下載完成後拷貝到項目下的libs文件下即可。
在xml文件下添加一個SmartImageView控件
<LinearLayout 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" >
<com.loopj.android.image.SmartImageView
android:id="@+id/sv_1"
android:layout_width="fill_parent"
android:layout_height="300dp" />
</LinearLayout>
MainActivity.class
package com.example.smartimageview;
import com.loopj.android.image.SmartImageView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private SmartImageView sv_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv_1 = (SmartImageView) findViewById(R.id.sv_1);
sv_1.setImageUrl("http://s1.dwstatic.com/lushicard/Lyra_the_Sunshard.png",//加載指定地址的圖片
R.drawable.ic_launcher,//指定圖片沒找到時顯示的圖片
R.drawable.ic_launcher);//正在加載中顯示的圖片
}
@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;
}
}
添加訪問網絡的權限 android.permission.INTERNET
使用Eclipse千萬別忘了權限,權限,權限。AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smartimageview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.smartimageview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
就只是簡單的使用下。