2014-03-26 24 views
0

以我的Android应用我创建表示9×9的视野中的视图(参见简化屏幕截图):在Android中处理ImageViews的对象池?

enter image description here

在视图中每个字段由一个ImageView的表示(示出图片代替矩形)。一些ImageViews将被删除,将其设置为null并将可见性设置为View.GONE,并且每次将新元素添加到该字段时都会创建新的ImageView。这意味着我可以在很短的时间内创建大量的ImageView。关键是,游戏在我的HTC One上运行良好,但在一段时间后(我认为垃圾收集正在运行时)滞后,然后停止滞后并再次运行得很好。

这让我想到使用Pool来管理我的对象,例如,通过回收“删除的ImageViews”并通过设置位置和更改图像源来重复使用它们。我正在开发纯Android,但我想使用LibGDX框架。

我的问题是:关于如何在普通Java/Android中实现Pool的问题,您有什么建议吗?我发现this后感兴趣,以为我可以去与Apache Commons ObjectPool。 Android或LibGDX有更好的方法吗?

注意: ImageView的每一轮都不在同一位置。我使用吐温引擎移动它们。但是,图像视图的数量是不变的。意思是在游戏的某些时候,我有81个ImageView(9 * 9)加上几个ImageView用于动画特殊事件(也许是+10)。

我会很感激任何意见/建议。

最好的问候,

吉米

+0

如果我理解你在正确的方式比赛的想法,与imageviews网格是静态 - 这意味着图像视图的数量总是相同的,对吗?为什么不更新imageView背景或图像而不是重新创建它们?在我看来,游泳池的想法并不好 - 如果你需要实现某种方式滚动的列表或网格视图,可能会有用 – MP23

+0

对不起,我还没有提到它,但我正在移动ImageViews周围(使用Tween引擎进行动画处理),因此它们在每一轮中都没有相同的位置。否则,我完全同意你的观点,只有改变图像源才是明智之举。此外,我还有更多ImageView用于动画化两张牌的合并。希望对你有意义。 –

+0

好的,那么我建议使用某种集合,例如列表,每一个没有更多必要的图像视图应该去那里,如果需要一些新的ImageView,那么你应该首先检查你的列表是否不包含一个,并更新其背景和位置。当然,如果你使用它,你应该从列表中删除它。仅当列表为空时才创建新的ImageView。我认为它是简单的缓存机制之一,但它在例如ListView中正常工作(重用行视图) – MP23

回答

1

据吉米的建议,我张贴我的意见作为答案

我建议使用一些集合类如List<ImageView>的,

  • 每个图像认为是不更有必要的应该去那里

  • 如果一些新的ImageView是必要的,那么你应该首先检查你的列表是否包含一个,并更新其背景和位置。

  • 当然,如果你使用它,你应该从列表中删除它。
  • 仅当列表为空时才创建新的ImageView。

我认为这是最简单的缓存机制之一,但它工作正常在如ListView控件(行观的再利用)

+0

或者我会使用两个列表..一个用于可见的ImageView,一个用于不可见的ImageView。正如我告诉你的,明天我会让你知道的。等不及要实现:) –

+0

它使用两个列表视图很好用:'列表 cacheViews'和'列表 currentViews'。当'cacheViews'为空时,我创建一个新的ImageView,并将引用存储在'currentViews'中,如果它不是空的,我通过将'cacheViews'的视图移动到另一个列表,设置位置和可见性来重用视图。回收很简单,只需将'View.GONE'的可见性设置为cacheViews即可。感谢您的想法/提醒:-D –

+0

很好听,rupps的答案也很好,但它是完全不同的方法 – MP23

1

我也有类似的问题,绝不是真正的表现感到满意。这一切都是当我将我的比赛场地扩大到30 * 30时,我获得了900个ImageViews。我尝试了很多优化,但是性能和内存在设备上很高且不可预测。

所以我做的是创建一个自定义视图。只有一个。这个视图然后在画布上绘制正方形。我很惊讶,现在我可以成千上万的正方形(100x100),并且性能非常流畅。

我在这里发布我的观点的骨架与所有废话删除的灵感,我强烈建议你按照这种方法。

/** 
    * ChequeredView is a view that displays a 2D square matrix, where each square can be individually selected. 
    * For high performance, It only uses one view regardless of the matrix size (everything is drawn in a canvas) 
    * @author rodo 13 march 2014 <[email protected]> 
    */ 

    public class ChequeredView extends View { 

     private final String TAG="ChequeredView"; 

     private static final int 
      DEFAULT_MATRIX_SIZE_COLS=20, 
      DEFAULT_MATRIX_SIZE_COLS=20, 
      DEFAULT_SQUARE_SIZE=100; 

     private int mCols=DEFAULT_MATRIX_SIZE_COLS, 
        mRows=DEFAULT_MATRIX_SIZE_ROWS; 

     /* Save touch press */ 
     private int mTouchX=0, mTouchY=0; 

     ///////////////// VIEW CODE 

     public ChequeredView(Context context, AttributeSet attrs) { super(context, attrs); } 
     public ChequeredView(Context context) { super(context); } 

     /** 
     * Report a size of your view that is: SquareSize * NUM_COLS x SquareSize * NUM_ROWS. You will paint it later. 
     */ 

     @Override 
     protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

      // calculate optimum square size 
      mStyleSquareSize=MeasureSpec.getSize(widthMeasureSpec)/mSquaresPerCanvas; 

      // report total size 
      setMeasuredDimension(DEFAULT_MATRIX_SIZE_COLS * mStyleSquareSize, DEFAULT_MATRIX_SIZE_ROWS * mStyleSquareSize); 
     } 

     @Override 
     public void onDraw(Canvas canvas) { 
      render(canvas); 
     } 


     @Override 
     public boolean onTouchEvent(android.view.MotionEvent event) { 

      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       // I execute the action in ACTION_UP so I can put this inside a scrollview and touch doesn't interferre the scroll. 
       mTouchX=(int) event.getX(); 
       mTouchY=(int) event.getY(); 
       return true; 

      case MotionEvent.ACTION_UP: 

       // only process touch if finger has not moved very much (if it has, it's a fling on parent) 

       if (isApprox(event.getX(), mTouchX, 5) && (isApprox(event.getY(), mTouchY, 5))) 
         processTouch((int)event.getX(), (int)event.getY()); 

       break; 

      } 
      return false; 
     }; 

     /** 
     * Check if a value is close to another one 
     * @param value Value to check 
     * @param ref Reference value 
     * @param threshold Threshold 
     * @return true if |val-ref|<threshold 
     */ 

     private boolean isApprox(float value, int ref, int threshold) { 
      float result=Math.abs(value-ref); 
      return (result<threshold); 
     } 

     ///////////////// VIEW METHODS 


     public void setMatrixSize(int numx, int numy) { 
      mRows=numx; 
      mCols=numy; 
      invalidate(); 
     } 

     ///////////////// VIEW INTERNALS 


     /** 
     * Renders the whole squaremap 
     * @param canvas 
     */ 

     private void render(Canvas canvas) { 
      if (canvas==null) return; 
      for (int x=0; x<mCols; x++) { 
       for (int y=0; y<mRows; y++) { 
        render_square(canvas, x, y); 
       } 
      } 
     } 

     /** 
     * Renders one of the squares 
     * @param canvas Canvas where to draw 
     * @param nCol The column 
     * @param nRow The row 
     */ 

     private void render_square(Canvas canvas, int nCol, int nRow) { 


      String text=null, transition=null; 
      int delay=0; 
      Paint paint=null; 

      int cx=nCol*mStyleSquareSize, cy=nRow*mStyleSquareSize; 

      canvas.save(); 
      canvas.translate(cx, cy); 
      canvas.drawRect(mStyleSquareMargin, mStyleSquareMargin, mStyleSquareSize-2*mStyleSquareMargin, mStyleSquareSize-2*mStyleSquareMargin, paint); 

      // this draws an square (I use vectorial squares with text rather than images, but just change drawRect to drawBitmap) 
      // just change it for drawBitmap() to draw one bitmap 

      canvas.restore(); 
     } 

     /** 
     * Process a touch on the map area 
     * @param x raw x coordinate 
     * @param y raw y coordinate 
     */ 

     private void processTouch(int x, int y) { 
      int nx=x/mStyleSquareSize, ny=y/mStyleSquareSize; 
      mSelectedX=nx; 
      mSelectedY=ny; 
      if (mSquareListener!=null) { 
       mSquareListener.onSquareSelected(nx, ny, data); 
      } 
      invalidate(); 
     } 
    } 
+0

你有什么建议,我可以如何使用ImageView的这个骨架?我不绘制矩形 - 每个ImageView代表另一个图像源(但有些将相同)。不过,我可以想象,一次又一次的绘画可能会更快。 –

+1

忘记ImageView,只是在我指出的地方使用drawBitmap。 drawBitmap可以在画布上绘制任何位图。 ImageView内部正在做这个!看看函数render_square! – rupps

+0

好点,明天我会试一试。 –