android Gallery實現非同步載入網路圖片 並只載入當前停止頁面圖
之前在網上找了很多都沒有這方面的資料,大概的效果是當Gallery滑動時不下載圖片,當Gallery滑動停止時載入當前頁面圖片,自己花了一點時間大概的實現了,如果各位有更好的意見歡迎說出來大家一起學習。
先上圖看看效果:
這圖是在載入圖片時顯示的預設圖片,當圖片載入完成則替換。
圖片載入完成效果圖
複製程式碼
複製程式碼
attrs.xml
複製程式碼
先上圖看看效果:
這圖是在載入圖片時顯示的預設圖片,當圖片載入完成則替換。
圖片載入完成效果圖
-
import java.util.ArrayList;
-
import java.util.HashMap;
-
import java.util.List;
-
import android.app.Activity;
-
import android.graphics.Bitmap;
-
import android.graphics.BitmapFactory;
-
import android.os.AsyncTask;
-
import android.os.Bundle;
-
import android.os.Handler;
-
import android.os.Message;
-
import android.util.Log;
-
import android.view.KeyEvent;
-
import android.view.View;
-
import android.widget.AdapterView;
-
import android.widget.AdapterView.OnItemClickListener;
-
import android.widget.AdapterView.OnItemSelectedListener;
-
import android.widget.Gallery;
-
public class MyActivity extends Activity implements OnItemClickListener{
-
public static HashMap<String,Bitmap> imagesCache=new HashMap<String, Bitmap>(); //圖片快取
-
private Gallery images_ga;
-
public static ImageAdapter imageAdapter;
-
private int num=0;
-
List<String> urls = new ArrayList<String>(); //所有圖片地址List
-
List<String> url = new ArrayList<String>(); //需要下載圖片的url地址
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.gallery_1);
-
Files.mkdir(this);
-
init();
-
}
-
private void init(){
-
Bitmap image= BitmapFactory.decodeResource(getResources(),R.drawable.default_movie_post);
-
imagesCache.put("background_non_load",image); //設定快取中預設的圖片
-
images_ga = (Gallery) findViewById(R.id.gallery);
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/f603918fa0ec08fabf7a641659ee3d6d55fbda0d.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/43a7d933c895d143d011bf9273f082025aaf071f.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/63d0f703918fa0ec2ebf584b269759ee3d6ddb7f.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/5ab5c9ea15ce36d31ed8387f3af33a87e850b1a5.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/8601a18b87d6277f6e46217628381f30e924fc2c.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/b48f8c54acf9964c3a29350e.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/bd3eb13533fa828b48da6aabfd1f4134960a5af9.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/29381f30e924b899da3ce5706e061d950a7bf672.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/bd3eb13533fa828b48da6aabfd1f4134960a5af9.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/4bed2e738bd4b31cd73d63fd87d6277f9e2ff877.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/caef76094b36acaf92b619b87cd98d1001e99c24.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/8435e5dde71190efd6154d95ce1b9d16fcfa608a.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/b3de9c824ba1d4cd6d81190f.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/e0fe9925cc2c683834a80f11.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/0bd162d9f2d3572c65911a988a13632762d0c307.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/ac6eddc451da81cb2ac1708a5266d01609243155.jpg");
-
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/1bd5ad6e8416d98080cb4a48.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/3c6d55fbb2fb43169d0508ca20a4462309f7d36c.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/faedab64034f78f0daf3664a79310a55b2191c8a.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/2fdda3cc7cd98d10b05af088213fb80e7aec90f9.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/b8014a90f603738d9536f39bb31bb051f819ec0f.jpg");
-
urls.add("http://hiphotos.baidu.com/baidu/pic/item/2fdda3cc7cd98d10b05af088213fb80e7aec90f9.jpg");
-
imageAdapter = new ImageAdapter(urls, this);
-
images_ga.setAdapter(imageAdapter);
-
images_ga.setOnItemClickListener(this);
-
images_ga.setOnItemSelectedListener(new OnItemSelectedListener() {
-
@Override
-
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
-
num=arg2;
-
Log.i("mahua", "ItemSelected=="+arg2);
-
GalleryWhetherStop();
-
}
-
@Override
-
public void onNothingSelected(AdapterView<?> arg0) {
-
// TODO Auto-generated method stub
-
-
}
-
});
-
}
-
-
-
@Override
-
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
-
Log.i("GOLF", "第"+arg2+"個被點選了");
-
}
-
-
/**
-
* 判斷Gallery滾動是否停止,如果停止則載入當前頁面的圖片
-
*/
-
private void GalleryWhetherStop() {
-
Runnable runnable = new Runnable() {
-
public void run() {
-
try {
-
int index =0;
-
index = num;
-
Thread.sleep(1000);
-
if (index == num) {
-
url.add(urls.get(num));
-
if(num!=0 && urls.get(num-1)!=null) {
-
url.add(urls.get(num-1));
-
}
-
if(num!=urls.size()-1 && urls.get(num+1)!=null) {
-
url.add(urls.get(num+1));
-
}
-
Message m = new Message();
-
m.what = 1;
-
mHandler.sendMessage(m);
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
};
-
new Thread(runnable).start();
-
}
-
-
-
// 載入圖片的非同步任務
-
class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
-
@Override
-
protected void onCancelled() {
-
// TODO Auto-generated method stub
-
super.onCancelled();
-
}
-
@Override
-
protected void onPostExecute(Bitmap result) {
-
// TODO Auto-generated method stub
-
super.onPostExecute(result);
-
}
-
-
@Override
-
protected Bitmap doInBackground(String... params) {
-
Bitmap bitmap = null;
-
try {
-
String url = params[0];
-
boolean isExists = Files.compare(url); //這裡是自己寫的工具類,判斷本地快取是否已經下載過圖片
-
if (isExists == false) {//如果不存在就去下載圖片
-
Net net = new Net();
-
byte[] data = net.downloadResource(MyActivity.this, url);
-
bitmap = BitmapFactory
-
.decodeByteArray(data, 0, data.length);
-
imagesCache.put(url, bitmap); // 把下載好的圖片儲存到快取中
-
Files.saveImage(url, data);
-
} else {//如果存在直接讀取快取圖片
-
byte[] data = Files.readImage(url);
-
bitmap = BitmapFactory
-
.decodeByteArray(data, 0, data.length);
-
imagesCache.put(url, bitmap); // 把下載好的圖片儲存到快取中
-
}
-
Message m = new Message();//圖片載入完成通知重新載入
-
m.what = 0;
-
mHandler.sendMessage(m);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return bitmap;
-
}
-
-
}
-
-
-
private Handler mHandler = new Handler() {
-
public void handleMessage(Message msg) {
-
try {
-
switch (msg.what) {
-
case 0: {
-
imageAdapter.notifyDataSetChanged();
-
break;
-
}
-
case 1: {
-
for(int i=0; i<url.size(); i++) {
-
LoadImageTask task = new LoadImageTask();//非同步載入圖片
-
task.execute(url.get(i));
-
Log.i("mahua", url.get(i));
-
}
-
url.clear();
-
}
-
}
-
super.handleMessage(msg);
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
-
};
-
@Override
-
public boolean onKeyDown(int keyCode, KeyEvent event) {
-
if(keyCode==KeyEvent.KEYCODE_BACK){
-
System.exit(0);
-
}
-
return super.onKeyDown(keyCode, event);
-
}
- }
-
import java.util.List;
-
import android.content.Context;
-
import android.content.res.AssetManager;
-
import android.content.res.TypedArray;
-
import android.graphics.Bitmap;
-
import android.view.View;
-
import android.view.ViewGroup;
-
import android.widget.BaseAdapter;
-
import android.widget.Gallery;
-
import android.widget.ImageView;
-
-
public class ImageAdapter extends BaseAdapter {
-
public static final BaseAdapter Adapter = null;
-
private List<String> imageUrls; // 圖片地址list
-
private Context context;
-
int mGalleryItemBackground;
-
-
public ImageAdapter(List<String> imageUrls, Context context) {
-
this.imageUrls = imageUrls;
-
this.context = context;
-
// /*
-
// * 使用在res/values/attrs.xml中的<declare-styleable>定義 的Gallery屬性.
-
// */
-
TypedArray a = context.obtainStyledAttributes(R.styleable.Gallery1);
-
/* 取得Gallery屬性的Index id */
-
mGalleryItemBackground = a.getResourceId(
-
R.styleable.Gallery1_android_galleryItemBackground, 0);
-
/* 讓物件的styleable屬效能夠反覆使用 */
-
a.recycle();
-
}
-
public int getCount() {
-
return imageUrls.size();
-
}
-
public Object getItem(int position) {
-
return imageUrls.get(position);
-
}
-
public long getItemId(int position) {
-
return position;
-
}
-
public View getView(int position, View convertView, ViewGroup parent) {
-
Bitmap image;
-
ImageView view = new ImageView(context);
-
image = MyActivity.imagesCache.get(imageUrls.get(position));
-
// 從快取中讀取圖片
-
if (image == null) {
-
image = MyActivity.imagesCache.get("background_non_load");
-
}
-
// 設定所有圖片的資源地址
-
view.setImageBitmap(image);
-
view.setScaleType(ImageView.ScaleType.FIT_XY);
-
view.setLayoutParams(new Gallery.LayoutParams(240, 320));
-
view.setBackgroundResource(mGalleryItemBackground);
-
/* 設定Gallery背景圖 */
-
return view;
-
}
- }
attrs.xml
-
<?xml version="1.0" encoding="utf-8"?>
-
<resources>
-
<declare-styleable name="TogglePrefAttrs">
-
<attr name="android:preferenceLayoutChild" />
-
</declare-styleable>
-
-
<!-- These are the attributes that we want to retrieve from the theme
-
in view/Gallery1.java -->
-
<declare-styleable name="Gallery1">
-
<attr name="android:galleryItemBackground" />
-
</declare-styleable>
-
-
<declare-styleable name="LabelView">
-
<attr name="text" format="string" />
-
<attr name="textColor" format="color" />
-
<attr name="textSize" format="dimension" />
-
</declare-styleable>
- </resources>
相關文章
- Android實現圖片非同步載入操作Android非同步
- Android 載入網路圖片 以及實現圓角圖片效果Android
- 頁面圖片預載入與懶載入策略
- 圖片懶載入實現
- ajax實現頁面非同步載入非同步
- 載入本地圖片模糊,Glide載入網路圖片卻很清晰地圖IDE
- 小米手機載入h5頁面載入不出圖片H5
- 滾動載入圖片(懶載入)實現原理
- 圖片懶載入js實現JS
- 網路圖片載入的封裝封裝
- Android 圖片載入框架Android框架
- android 載入大量圖片Android
- android ListView非同步載入圖片(雙快取)AndroidView非同步快取
- 實現圖片懶載入(throttle, debounce)
- [譯] 原生實現圖片懶載入
- React如何實現圖片懶載入React
- 實現圖片懶載入(Lazyload)
- 如何實現圖片預載入和載入進度條
- 使用AFNetworking載入網路圖片
- Android 高效安全載入圖片Android
- 封裝並實現統一的圖片載入架構封裝架構
- Android 載入大圖片,不壓縮圖片Android
- 關於用jquery.masonry.js實現動態載入效果(當頁面滾動條拉到底部時時重新載入圖片)jQueryJS
- 實現java讀取網頁內容並下載網頁中出現的圖片Java網頁
- AlamofireImage 使用 – swift載入網路圖片,縮放圖片,生成圓形圖片Swift
- 圖片預載入和懶載入
- 如何實現一個圖片載入框架框架
- JavaScript實現圖片的延遲載入JavaScript
- Flutter 圖片載入Flutter
- 圖片懶載入
- 預載入圖片
- 圖片載入事件事件
- 載入圖片方式
- Android ImageLoader框架之圖片載入與載入策略Android框架
- IIS網站圖片不能載入網站
- ListView 之非同步載入圖片亂序View非同步
- ListView效能優化非同步載入圖片View優化非同步
- 小說APP原始碼的圖片載入方式,懶載入和預載入的實現APP原始碼