我也有类似的问题,绝不是真正的表现感到满意。这一切都是当我将我的比赛场地扩大到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();
}
}
如果我理解你在正确的方式比赛的想法,与imageviews网格是静态 - 这意味着图像视图的数量总是相同的,对吗?为什么不更新imageView背景或图像而不是重新创建它们?在我看来,游泳池的想法并不好 - 如果你需要实现某种方式滚动的列表或网格视图,可能会有用 – MP23
对不起,我还没有提到它,但我正在移动ImageViews周围(使用Tween引擎进行动画处理),因此它们在每一轮中都没有相同的位置。否则,我完全同意你的观点,只有改变图像源才是明智之举。此外,我还有更多ImageView用于动画化两张牌的合并。希望对你有意义。 –
好的,那么我建议使用某种集合,例如列表,每一个没有更多必要的图像视图应该去那里,如果需要一些新的ImageView,那么你应该首先检查你的列表是否不包含一个,并更新其背景和位置。当然,如果你使用它,你应该从列表中删除它。仅当列表为空时才创建新的ImageView。我认为它是简单的缓存机制之一,但它在例如ListView中正常工作(重用行视图) –
MP23