編輯:Android開發實例
在listview中有很多時候,都因為圖片太大造成內存溢出的問題,下面這個demo用10M大小的圖片測試並沒有出現內存溢出和卡頓現象。
項目截圖
主要代碼:
- package com.example.listview;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.widget.ListView;
- public class MainActivity extends Activity {
- ListAdapter mAdapter;
- ListView mListView;
- int count = 100;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mListView = (ListView) findViewById(R.id.listView1);
- mAdapter = new ListAdapter(this,mListView);
- mAdapter.setData(count);
- mListView.setAdapter(mAdapter);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- }
adapter:
- package com.example.listview;
- import java.util.ArrayList;
- import android.content.Context;
- import android.graphics.Bitmap;
- 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;
- public class ListAdapter extends BaseAdapter {
- int mData;
- Context mContext;
- ListView mListView;
- SyncImageLoader mSyncImageLoader;
- ArrayList<String> mlist;
- public ListAdapter(Context context,ListView listView) {
- super();
- this.mContext = context;
- this.mListView=listView;
- this.mListView.setOnScrollListener(onScrollListener);
- mSyncImageLoader=new SyncImageLoader();
- mlist=new ArrayList<String>();
- }
- public void setData(int data) {
- this.mData = data;
- }
- @Override
- public int getCount() {
- // TODO Auto-generated method stub
- return mData;
- }
- @Override
- public Object getItem(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public long getItemId(int arg0) {
- // TODO Auto-generated method stub
- return arg0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup arg2) {
- ViewHolder holder = null;
- if (convertView == null) {
- holder = new ViewHolder();
- LayoutInflater inflater = LayoutInflater.from(mContext);
- convertView = inflater.inflate(R.layout.list_item, null);
- holder.iv01 = (ImageView) convertView.findViewById(R.id.imageView1);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.iv01.setId(0x7f021000+position);
- if(mlist.contains("/sdcard/pic_01.jpg")){
- holder.iv01.setImageBitmap(MyBitmap.readBitMap("/sdcard/pic_01.jpg"+"1.jpg",1));
- }else{
- holder.iv01
- .setImageBitmap(MyBitmap.readBitMap(mContext, R.drawable.load));
- mSyncImageLoader.loadImage(position, "/sdcard/pic_01.jpg", imageLoadListener);
- mlist.add("/sdcard/pic_01.jpg");
- }
- return convertView;
- }
- class ViewHolder {
- public ImageView iv01;
- }
- AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- }
- public void onScrollStateChanged(final AbsListView view, int scrollState) {
- //滑動的三種狀態,當滑動停止之後加載圖片
- switch (scrollState) {
- case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
- System.out.println("SCROLL_STATE_FLING");
- mSyncImageLoader.lock();
- break;
- case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
- System.out.println("SCROLL_STATE_IDLE");
- loadImage();
- break;
- case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
- System.out.println("SCROLL_STATE_TOUCH_SCROLL");
- mSyncImageLoader.lock();
- break;
- default:
- break;
- }
- }
- };
- private void loadImage(){
- int start = mListView.getFirstVisiblePosition();
- int end = mListView.getLastVisiblePosition();
- if (end >= getCount()) {
- end = getCount() - 1;
- }
- mSyncImageLoader.setLoadLimit(start, end);
- mSyncImageLoader.unlock();
- }
- SyncImageLoader.OnImageLoadListener imageLoadListener = new SyncImageLoader.OnImageLoadListener() {
- public void OnImageLoad(Integer t, Bitmap bitmap) {
- ImageView iv01 =(ImageView) mListView.findViewById(0x7f021000+t);
- if(iv01 !=null){
- iv01.setImageBitmap(bitmap);
- }
- }
- };
- }
SyncImageLoader:
- package com.example.listview;
- import java.util.ArrayList;
- import android.graphics.Bitmap;
- import android.os.Handler;
- public class SyncImageLoader {
- private Object lock = new Object();
- // 是否允許加載圖片
- private boolean mAllowLoad = true;
- // 開始加載圖片的位置
- private int mStartLoadLimit = 0;
- // 停止加載圖片的位置
- private int mStopLoadLimit = 0;
- ArrayList<String> list=new ArrayList<String>();
- Bitmap mBitmap;
- Handler handler = new Handler();
- public interface OnImageLoadListener {
- public void OnImageLoad(Integer id, Bitmap bitmap);
- }
- public void setLoadLimit(int startLoadLimit, int stopLoadLimit) {
- mStartLoadLimit = startLoadLimit;
- mStopLoadLimit = stopLoadLimit;
- }
- public void lock() {
- mAllowLoad = false;
- }
- public void unlock() {
- mAllowLoad = true;
- synchronized (lock) {
- lock.notifyAll();
- }
- }
- public void loadImage(Integer t, String imageUrl,
- OnImageLoadListener listener) {
- final OnImageLoadListener mListener = listener;
- final String mImageUrl = imageUrl;
- final Integer mt = t;
- new Thread(new Runnable() {
- public void run() {
- if (!mAllowLoad) {
- synchronized (lock) {
- try {
- lock.wait();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- if (mStopLoadLimit == 0 && mStartLoadLimit == 0) {
- loadImage(mImageUrl, mt, mListener);
- }
- if (mAllowLoad && mt <= mStopLoadLimit && mt >= mStartLoadLimit) {
- System.out.println("loadImage");
- loadImage(mImageUrl, mt, mListener);
- }
- }
- }).start();
- }
- private void loadImage(final String mImagePath, final Integer mt,
- final OnImageLoadListener mListener) {
- try {
- if(list.contains(mImagePath)){
- mBitmap = MyBitmap.readBitMap(mImagePath + "1.jpg",1);
- }else{
- mBitmap = MyBitmap.readBitMap(mImagePath,20);
- MyBitmap.saveFile(mBitmap, mImagePath + "1.jpg");
- list.add(mImagePath);
- }
- handler.post(new Runnable() {
- public void run() {
- if (mAllowLoad) {
- mListener.OnImageLoad(mt, mBitmap);
- }
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
MyBitmap:
- package com.example.listview;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- public class MyBitmap {
- public static Bitmap readBitMap(String fileName,int n) {
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inPreferredConfig = Bitmap.Config.RGB_565;
- opt.inSampleSize = n; // width,hight設為原來的十分一
- opt.inPurgeable = true;
- opt.inInputShareable = true; // 獲取資源圖片
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(fileName);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return BitmapFactory.decodeStream(fis, null, opt);
- }
- public static Bitmap readBitMap(Context context, int resId) {
- BitmapFactory.Options opt = new BitmapFactory.Options();
- opt.inPreferredConfig = Bitmap.Config.RGB_565;
- // opt.inSampleSize = 10; //width,hight設為原來的十分一
- opt.inPurgeable = true;
- opt.inInputShareable = true; // 獲取資源圖片
- InputStream is = context.getResources().openRawResource(resId);
- return BitmapFactory.decodeStream(is, null, opt);
- }
- public static void saveFile(Bitmap bm, String fileName) throws IOException {
- File myCaptureFile = new File(fileName);
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(myCaptureFile));
- bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
- bos.flush();
- bos.close();
- }
- }
csdn下載地址:http://download.csdn.net/detail/wenwei19861106/4865907
Android應用程序可以在許多不同地區的許多設備上運行。為了使應用程序更具交互性,應用程序應該處理以適合應用程序將要使用的語言環境方面的文字,數字,文件等。在本章中,我
最近寫了一個簡單的朋友圈程序,包含了朋友圈的列表實現,視頻的錄制、預覽與上傳,圖片可選擇拍照或者從相冊選取,從相冊選取可以一次選擇多張照片,並且限制照片的張數,想擁有真正
TCP和UDP在網絡傳輸中非常重要,在Android開發中同樣重要。 首先我們來看一下什麼是TCP和UDP。 什麼是TCP? TCP:Transmission C
上一節的顯示賬單明細 上中,賬單明細的顯示已經基本實現,本文主要整理下代碼,實