編輯:關於Android編程
private LruCachemMemoryCaches; // 獲取應用內存 int maxMemory = (int) Runtime.getRuntime().maxMemory(); // 分配cache int cacheSize = maxMemory / 10; mMemoryCaches = new LruCache (cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getByteCount(); } }; // 從LruCache獲取中獲取緩存對象 public Bitmap getBitmapFromMemoryCaches(String url) { return mMemoryCaches.get(url); } // 增加緩存對象到LruCache public void addBitmapToMemoryCaches(String url,Bitmap bitmap) { if (getBitmapFromMemoryCaches(url) == null) { mMemoryCaches.put(url, bitmap); } }
@Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { mImageLoader.loadImages(mStart, mEnd); } else { mImageLoader.cancelAllTasks(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mStart = firstVisibleItem; mEnd = firstVisibleItem + visibleItemCount; if (mFirstFlag && visibleItemCount > 0) { mImageLoader.loadImages(mStart, mEnd); mFirstFlag = false; } }
public void loadImages(int start, int end) { for (int i = start; i < end; i++) { String url = Images.IMAGE_URLS[i]; Bitmap bitmap = getBitmapFromMemoryCaches(url); if (bitmap == null) { ASyncDownloadImage task = new ASyncDownloadImage(url); mTasks.add(task); task.execute(url); } else { ImageView imageView = (ImageView) mListView.findViewWithTag(url); imageView.setImageBitmap(bitmap); } } }
private static Bitmap getBitmapFromUrl(String urlString) { Bitmap bitmap; InputStream is = null; try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(conn.getInputStream()); bitmap = BitmapFactory.decodeStream(is); conn.disconnect(); return bitmap; } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { } } return null; }
class ASyncDownloadImage extends AsyncTask{ private String url; public ASyncDownloadImage(String url) { this.url = url; } @Override protected Bitmap doInBackground(String... params) { url = params[0]; Bitmap bitmap = getBitmapFromUrl(url); if (bitmap != null) { addBitmapToMemoryCaches(url, bitmap); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); ImageView imageView = (ImageView) mListView.findViewWithTag(url); if (imageView != null && bitmap != null) { imageView.setImageBitmap(bitmap); } mTasks.remove(this); } }
package com.imooc.listviewacyncloader; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.LruCache; import android.widget.ImageView; import android.widget.ListView; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashSet; import java.util.Set; public class ImageLoaderWithCaches { private Set mTasks; private LruCachemMemoryCaches; private ListView mListView; public ImageLoaderWithCaches(ListView listview) { this.mListView = listview; mTasks = new HashSet<>(); int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheSize = maxMemory / 10; mMemoryCaches = new LruCache (cacheSize) { @Override protected int sizeOf(String key, Bitmap value) { return value.getByteCount(); } }; } public void showImage(String url, ImageView imageView) { Bitmap bitmap = getBitmapFromMemoryCaches(url); if (bitmap == null) { imageView.setImageResource(R.drawable.ic_launcher); } else { imageView.setImageBitmap(bitmap); } } public Bitmap getBitmapFromMemoryCaches(String url) { return mMemoryCaches.get(url); } public void addBitmapToMemoryCaches(String url,Bitmap bitmap) { if (getBitmapFromMemoryCaches(url) == null) { mMemoryCaches.put(url, bitmap); } } public void loadImages(int start, int end) { for (int i = start; i < end; i++) { String url = Images.IMAGE_URLS[i]; Bitmap bitmap = getBitmapFromMemoryCaches(url); if (bitmap == null) { ASyncDownloadImage task = new ASyncDownloadImage(url); mTasks.add(task); task.execute(url); } else { ImageView imageView = (ImageView) mListView.findViewWithTag(url); imageView.setImageBitmap(bitmap); } } } private static Bitmap getBitmapFromUrl(String urlString) { Bitmap bitmap; InputStream is = null; try { URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(conn.getInputStream()); bitmap = BitmapFactory.decodeStream(is); conn.disconnect(); return bitmap; } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { } } return null; } public void cancelAllTasks() { if (mTasks != null) { for (ASyncDownloadImage task : mTasks) { task.cancel(false); } } } class ASyncDownloadImage extends AsyncTask { private String url; public ASyncDownloadImage(String url) { this.url = url; } @Override protected Bitmap doInBackground(String... params) { url = params[0]; Bitmap bitmap = getBitmapFromUrl(url); if (bitmap != null) { addBitmapToMemoryCaches(url, bitmap); } return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); ImageView imageView = (ImageView) mListView.findViewWithTag(url); if (imageView != null && bitmap != null) { imageView.setImageBitmap(bitmap); } mTasks.remove(this); } } }
package com.imooc.listviewacyncloader; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import java.util.List; public class MyAdapterUseCaches extends BaseAdapter implements AbsListView.OnScrollListener { private LayoutInflater mInflater; private ListmData; private ImageLoaderWithCaches mImageLoader; private int mStart = 0, mEnd = 0; private boolean mFirstFlag; public MyAdapterUseCaches(Context context, List data, ListView listView) { this.mData = data; mInflater = LayoutInflater.from(context); mImageLoader = new ImageLoaderWithCaches(listView); mImageLoader.loadImages(mStart, mEnd); mFirstFlag = true; listView.setOnScrollListener(this); } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return mData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { String url = mData.get(position); ViewHolder viewHolder = null; if (convertView == null) { viewHolder = new ViewHolder(); convertView = mInflater.inflate(R.layout.listview_item, null); viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_lv_item); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.imageView.setTag(url); viewHolder.imageView.setImageResource(R.drawable.ic_launcher); mImageLoader.showImage(url, viewHolder.imageView); return convertView; } @Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { mImageLoader.loadImages(mStart, mEnd); } else { mImageLoader.cancelAllTasks(); } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { mStart = firstVisibleItem; mEnd = firstVisibleItem + visibleItemCount; if (mFirstFlag && visibleItemCount > 0) { mImageLoader.loadImages(mStart, mEnd); mFirstFlag = false; } } public class ViewHolder { public ImageView imageView; } }
現在大部分使用的都是SVN,也有一部分遷移了Git,雖然挺好的,不過還有其它很多版本控制的工具,並沒有誰最好用,最重要的是適合自己的公司與團隊,效率和團隊是成正比了,重要
一、 View 的基礎知識View 是 Android 中所有空間的基類。1、 View 的位置參數View 的位置主要有四個頂點決定的, top、left、right、
有時候因公司需求,要求合並兩個APP 使用裡面的功能。 平台:Studio 小白鼠:二維碼掃描 和自己項目 具體步驟: /** * 1.將解壓後的android
1.Toast源碼分析老規矩,我們先去看Toast的源碼。Toast有兩種顯示布局方式,一種最常見調用Toast.makeText() ,看源碼是這樣寫的pu