How to make onClick and mark image in Gridview

Put your problem here if it does not fit any of the other categories.

How to make onClick and mark image in Gridview

Postby reben » Fri Apr 19, 2013 11:54 am

Hello! The first question that I have is that I have downloaded from the Internet MarkableImageView class to add a marker to an image I have already selected. The class is as follows:

Code: Select all

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());
        }
    }
}



I use a Gridview to show all my photos and I want to marked some. I use a BaseAdapter to load the data, here is my method getView ():

Code: Select all

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;
   }



Everything works perfectly but the problem is that when I start to move around the screen looks brand in the photos that I want to stop playing but the screen in certain positions marks are removed. Then touch the screen again reappears. How I can fix it to appear forever?


My second question is that I generate a grid where each image is a class called TileJuegoMemoria extending ImageView.

Code: Select all

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);
        }



I add each object to a listener to click on it. The Listener has its method onClick () like this:

Code: Select all

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);
                }
            }
   };
   }



The game is for 2 players where they take turns playing two screen images that change. My question is how it might do so after Player 1's turn, was the computer who did click on two random images.
Where gender had thought the images saved in a list the coordinates of each image to PerformClick () on it. What I can do? Thank you!
reben
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 19, 2013 11:42 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 14 guests