編輯:Android開發實例
1.普通的下載方式
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="@drawable/icon"
android:layout_width="wrap_content"
android:id="@+id/imgPic"
android:layout_gravity="center|center_vertical"
android:layout_height="fill_parent">
</ImageView>
</LinearLayout>
java文件:
public class DownloadImage extends Activity {
private ImageView imgPic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download_image);
imgPic = (ImageView) findViewById(R.id.imgPic);
String url = "圖片文件地址"
loadRmoteImage(url);
}
/**
* @param imgUrl
* 遠程圖片文件的URL
*
* 下載遠程圖片
*/
private void loadRmoteImage(String imgUrl) {
URL fileURL = null;
Bitmap bitmap = null;
try {
fileURL = new URL(imgUrl);
} catch (MalformedURLException err) {
err.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileURL
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
int length = (int) conn.getContentLength();
if (length != -1) {
byte[] imgData = new byte[length];
byte[] buffer = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(buffer)) > 0) {
System.arraycopy(buffer, 0, imgData, destPos,
readLen);
destPos += readLen;
}
bitmap = BitmapFactory.decodeByteArray(imgData, 0,
imgData.length);
}
} catch (IOException e) {
e.printStackTrace();
}
imgPic.setImageBitmap(bitmap);
}
2.帶進度條的下載
有時候網絡差,或者是圖片太大,會出現黑屏的情況,用戶體驗比較差,那麼增加一個進度條是提高用戶體驗的好方法
/**
* @version: 創建時間:2011-7-27 下午02:55:56
* 說 明: android中下載圖片
* 修改歷史:
*/
public class DownloadImage extends Activity {
private ImageView imgPic;
private ProgressBar progressBar;
private int totalSize=0;
private int size=0;
private Handler mHandler;
String url = "http://android.tgbus.com/Android/UploadFiles_4504/201107/20110729103701841.jpg";
private Bitmap bitmap=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.download_image);
imgPic = (ImageView) findViewById(R.id.imgPic);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setProgress(getProgressInt(progressBar.getMax()));
mHandler = new Handler() {
public void handleMessage(Message msg) {
progressBar.setProgress(getProgressInt(progressBar.getMax
()));
if(bitmap!=null){
imgPic.setImageBitmap(bitmap);
}
}
};
new Thread(){
public void run(){
loadRmoteImage(url);
}
}.start();
}
/**
* @param imgUrl
* 遠程圖片文件的URL
*
* 下載遠程圖片
*/
private void loadRmoteImage(String imgUrl) {
URL fileURL = null;
try {
fileURL = new URL(imgUrl);
} catch (MalformedURLException err) {
err.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileURL
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
int length = (int) conn.getContentLength();
totalSize=length;
if (length != -1) {
byte[] imgData = new byte[length];
byte[] buffer = new byte[512];
int readLen = 0;
int destPos = 0;
while ((readLen = is.read(buffer)) > 0) {
System.arraycopy(buffer, 0, imgData, destPos,
readLen);
destPos += readLen;
size=destPos;
mHandler.sendEmptyMessage(1);
Thread.sleep(100);
}
bitmap = BitmapFactory.decodeByteArray(imgData, 0,
imgData.length);
mHandler.sendEmptyMessage(1);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private int getProgressInt(int max) {
int result = (totalSize > 0) ? (int) (size * max * 1.0 / totalSize) : 0;
return result;
}
}
可以顯示在的Android任務,通過加載進度條的進展。進度條有兩種形狀。加載欄和加載微調(spinner)。在本章中,我們將討論微調(spinner)。Spinner 用
以前在線性代數中學習了矩陣,對矩陣的基本運算有一些了解,前段時間在使用GDI+的時候再次學習如何使用矩陣來變化圖像,看了之後在這裡總結說明。首先大家看看下面這個3
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
1.圖片處理 1.圓角圖片代碼如下:/** * 轉換成圓角 * &