2012-05-10 46 views
0

我需要一些建議如何在我的應用程序中實現這種情況。Android更新陣列位圖/隊列

我有一排bitmpaps,我用它來存儲我的Canvas的不同狀態,所以我可以在將來使用它們。這裏是我正在使用的代碼:

private Bitmap[] temp; 
// on user click happens this -> 
if(index<5){ 
      temp[index] = Bitmap.createBitmap(mBitmap); 
      index++; 
} 

所以基本上我只想保存最後5個位圖,具體取決於用戶的操作。我想學的東西是我如何更新我的數組,以便我可以始終擁有最後5個位圖。

這裏是我的意思是:

位圖[1,2,3,4,5] - >用戶點擊後,我想刪除第一個位圖,再順序排列並保存新一個作爲最後..所以我的數組應該看起來像這樣:Bitmaps [2,3,4,5,6];

任何意見/建議這是做到這一點的最佳方式?

在此先感謝!

回答

2

我剛纔寫的...... 使用此代碼初始化:

Cacher cach = new Cacher(5); 
//when you want to add a bitmap 
cach.add(yourBitmap); 
//get the i'th bitmap using 
cach.get(yourIndex); 

記住,你可以重新實現該功能get返回第i個「老」位圖

public class Cacher { 
    public Cacher(int max) { 

     this.max = max; 
     temp = new Bitmap[max]; 
     time = new long[max]; 
     for(int i=0;i<max;i++) 
      time[i] = -1; 
    } 
    private Bitmap[] temp; 
    private long[] time; 
    private int max = 5; 
    public void add(Bitmap mBitmap) { 
     int index = getIndexForNew(); 
     temp[index] = Bitmap.createBitmap(mBitmap); 

    } 
    public Bitmap get(int i) { 
     if(time[i] == -1) 
      return null; 
     else 
      return temp[i]; 
    } 
    private int getIndexForNew() { 
     int minimum = 0; 
     long value = time[minimum]; 
     for(int i=0;i<max;i++) { 
      if(time[i]==-1) 
       return i; 
      else { 
       if(time[i]<value) { 
        minimum = i; 
        value = time[minimum]; 
       } 
     } 
     return minimum; 
    } 
}