編輯:關於Android編程
思路:先為ListView或者GridView等設置默認圖片,然後啟用異步線程下載網絡圖片然後更新adapter,刷新UI,代碼如下:
MainActivity:
package com.home.asynload; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.graphics.BitmapFactory; import android.os.Bundle; import android.widget.ListView; public class MainActivity extends Activity { private Listlist = new ArrayList (); private ImageAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initImage(); // 啟動異步線程加載網絡圖片 new ImageLoadTask(adapter).execute(); } /** * 設置初始圖片 */ private void initImage() { for (int i = 0; i < 10; i++) { ImageBean bean = new ImageBean(); bean.setBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)); list.add(bean); } adapter = new ImageAdapter(MainActivity.this, list); ListView listView = new ListView(MainActivity.this); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); setContentView(listView); } }
ImageAdapter:
package com.home.asynload; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ProgressBar; public class ImageAdapter extends BaseAdapter { private Context context; private Listlist = new ArrayList (); public ImageAdapter(Context context, List list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup arg2) { LinearLayout layout = new LinearLayout(context); ImageView imageView = new ImageView(context); layout.addView(imageView); ImageBean bean = list.get(position); if (bean.getBitmap() != null) { imageView.setImageBitmap(bean.getBitmap()); } if (bean.isNeedBar()) { ProgressBar bar = new ProgressBar(context); layout.addView(bar); } return layout; } }
ImageLoadTask:
package com.home.asynload; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; public class ImageLoadTask extends AsyncTask{ private ImageAdapter adapter; public ImageLoadTask(ImageAdapter adapter) { this.adapter = adapter; } @Override protected Void doInBackground(Void... params) { for (int i = 0; i < adapter.getCount(); i++) { ImageBean bean = (ImageBean) adapter.getItem(i); Bitmap bitmap = BitmapFactory .decodeStream(getStream("http://avatar.profile.csdn.net/A/E/5/2_piaopiaohu123.jpg")); bean.setBitmap(bitmap); bean.setNeedBar(false); publishProgress(); // 通知去更新UI } return null; } public void onProgressUpdate(Void... voids) { if (isCancelled()) { return; } // 更新UI adapter.notifyDataSetChanged(); } public InputStream getStream(String urlStr) { InputStream inStream = null; try { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setConnectTimeout(10 * 1000); inStream = conn.getInputStream(); } catch (Exception e) { e.printStackTrace(); } return inStream; } }
ImageBean:
package com.home.asynload; import android.graphics.Bitmap; public class ImageBean { private Bitmap bitmap; private boolean isNeedBar = true; public ImageBean() { super(); } public boolean isNeedBar() { return isNeedBar; } public void setNeedBar(boolean isNeedBar) { this.isNeedBar = isNeedBar; } public Bitmap getBitmap() { return bitmap; } public void setBitmap(Bitmap bitmap) { this.bitmap = bitmap; } }
寫在前面的廢話 下載文件,幾乎是所有APP都會用到的功能!算了,還是不廢話了,直接開寫吧。。。 簡單使用 完成一個下載任務只需要4行代碼,什
為了在SurfaceView上畫圖,我們定義一個MySurfaceView類,該類繼承SurfaceView並且實現SurfaceHolder.Callback接口。在s
1.盡量使用線性布局(LinearLayout)和相對布局(RelativeLayout),不要使用絕對布局。 2.盡量使用dip和sp,不要使用px。 3.為不同的分辨
本文介紹九宮格程序的設計代碼。一.相關介紹(一)效果顯示1.程序剛運行時的效果:2.在頁面上點擊選擇並滑動時的畫面3.選擇密碼之後的顯示(二)功能介紹1.點擊某圓圈後,在