2012-05-10 41 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; 
    } 
}