2

我遇到的第一个问题是我从 Internet MarkableImageView 类下载了一个标记到我已经选择的图像。课程如下:

public class MarkableImageView extends ImageView {
// Por defecto falso
private boolean checked = false;

public MarkableImageView(Context context) {
    super(context);
}

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

public MarkableImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public void setChecked(boolean checked) {
    this.checked = checked;
    invalidate();
}

public boolean isChecked() {
    return checked;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if(checked) {
        Bitmap check = BitmapFactory.decodeResource(
                getResources(), R.drawable.checkbox_on_background);
        int width = check.getWidth();
        int height = check.getHeight();
        //int margin = 15;
        int marginWidth = (int)(canvas.getWidth()/15);
        int marginHeight = (int)(canvas.getHeight()/20);
        int x = canvas.getWidth() - width - marginWidth;
        //int y = canvas.getHeight() - height - margin;
        int y = marginHeight;
        canvas.drawBitmap(check, canvas.getWidth()-check.getWidth(), 1, new Paint());
    }
}
}

我使用 Gridview 来显示我所有的照片,并且我想标记一些。我使用 BaseAdapter 来加载数据,这是我的方法 getView():

public View getView(int position, View convertView, ViewGroup parent) {
    MarkableImageView imageView;
    if(convertView == null) {
        imageView = new MarkableImageView(mContext);

        imageView.setLayoutParams(new GridView.LayoutParams((int)(Preferencias.anchoPantalla/3.5), (int)(Preferencias.altoPantalla/4)));

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);


    } else {
        imageView = (MarkableImageView) convertView;
    }
    if(nivel == -1) {

        if(!Preferencias.getIdsImagenesSeleccionadasJuegoMemoria().isEmpty()) {
            if(Preferencias.getIdsImagenesSeleccionadasJuegoMemoria().size() > position) {
                imageView.setImageResource(
                    Preferencias.imagenesTodas[Preferencias.getIdsImagenesSeleccionadasJuegoMemoria().get(position)]);
            }
        }
    } else if(nivel == 0) {
        if(!listaIdsDescubiertas.isEmpty()) {
            if(listaIdsDescubiertas.size() > position) {
                imageView.setImageResource(Preferencias.imagenesTodasPequeñas[listaIdsDescubiertas.get(position)]);
            }       
        }
    } else { 
        switch(nivel) {
            case 1:
                imageView.setImageResource(Preferencias.imagenesNivel1[position]);
                break;
            case 2: 
                imageView.setImageResource(Preferencias.imagenesNivel2[position]);
                break;
        }

        if(!listaIdsDescubiertas.isEmpty()) {
            for(Integer pos : listaIdsDescubiertas) {
                if(calcularPosicion(position, nivel) == pos) {
                    imageView.setChecked(true);
                }

            }
        }
    }

    return imageView;
}

一切正常,但问题是,当我开始在屏幕上移动时,我想停止播放的照片中看起来像是品牌,但某些位置的屏幕标记被删除了。然后再次触摸屏幕再次出现。我怎样才能让它永远出现?

我的第二个问题是我生成了一个网格,其中每个图像都是一个名为 TileJuegoMemeria 的类,它扩展了 ImageView。

for (int r = 0; r < rows; r++) {
        LinearLayout row = new LinearLayout(this);
        row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        row.setGravity(Gravity.CENTER);
        for (int c = 0; c < columns; c++) {
            TileJuegoMemoria tile = new TileJuegoMemoria(this, tilebackResource,
                    tilesImagenesRepetidas.get(position), position, sideLength, sideLength);

            tile.setClickable(true);
            tile.setId(position);
            tile.setOnClickListener(tileOnClickListener);

            row.addView(tile);
            position++;
        }
        memoryBoardLayout.addView(row);
    }

我将每个对象添加到侦听器以单击它。Listener 有它的 onClick () 方法,如下所示:

tileOnClickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {

            TileJuegoMemoria tempTile = (TileJuegoMemoria) v;

            if (tempTile.getVisibility() == TileJuegoMemoria.INVISIBLE || (!tempTile.isBackVisible)) {
                return;
            }
            int move = gameEngine.doMove(tempTile);
            if (move == -1) {
                return;
            }
            gameEngine.selectedTile = tempTile;
            gameEngine.selectedTile.flipOver();

            if (move == 2) {
                final Handler handler = new Handler();
                Timer t = new Timer();
                t.schedule(new TimerTask() {
                    public void run() {
                        handler.post(new Runnable() {
                            public void run() {             
                                TileJuegoMemoria precedentMoveselectedTile = gameEngine.movesArray[0];

                                if (gameEngine.selectedTile.tileFace.equals(precedentMoveselectedTile.tileFace)) {   
                                    if(eliminarAcertados) {
                                        gameEngine.selectedTile.hide();
                                        precedentMoveselectedTile.hide();
                                    }

                                } else {
                                    gameEngine.selectedTile.flipOver();
                                    precedentMoveselectedTile.flipOver();

                                    turnoJugador1 = !turnoJugador1;

                                }

                                gameEngine.clearMoveArray();
                            }
                        });
                    }
                }, 1000);
            }
        }
};
}

该游戏适用于 2 名玩家,他们轮流玩两个变化的屏幕图像。我的问题是,在轮到玩家 1 之后,它是如何做到的,是计算机点击了两个随机图像。凡想到性别的图像保存在一个列表中,每个图像的坐标到PerformClick()就可以了。我可以做什么?谢谢!

4

0 回答 0