Android Open Source - FindtheMines Image Adapter






From Project

Back to project page FindtheMines.

License

The source code is released under:

MIT License

If you think the Android project FindtheMines listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.stealthness.findthemines;
// www  .  j a v a 2 s  .c o  m
import java.util.ArrayList;

import findtheminecore.Game;
import findtheminecore.Position;
import findtheminecore.RuleSet;
import findtheminecore.Tile;
import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private static final String TAG = "com.stealthness.findthemines.ImageView";
  private Context mContext;
  private Game game;
  private ArrayList<Integer> mThumbIds ;

    public ImageAdapter(Context c,Game game) {
        mContext = c;
        this.game = game;
        mThumbIds = new ArrayList<Integer>();
        setGrid();
    }

    private void setGrid() {
      Log.d(TAG, "setGrid");
    for(int row = 0;row<game.getBoard().getNoOfRows();row++ ){
      for (int col = 0;col<game.getBoard().getNoOfCols();col++ ){
        Position pos = new Position(row,col);
        setImage(pos);  
      }
    }  
    
  }
    
    public static int getImageResource(Position pos,Game game){
      Tile tile = game.getPos(pos);
      if (tile!=null){
      if (tile.isMarked()){
        return (R.drawable.marked_blue);
      }else if(tile.isMine() && !tile.isSelected() && !tile.isHidden()){
        return (R.drawable.mine_blue);
      }else if(tile.isExploded()){
        return (R.drawable.explode_blue);
      }else if(tile.isSelected() && !tile.isHidden()){
        if (game.getRuleSet()==RuleSet.STANDARD){
          return getImageNumber(tile);
        } else if (game.getRuleSet()==RuleSet.TRAFFICLIGHTS){
          switch(tile.getValue()){      
          case 0:
          case 1:
            return (R.drawable.green_blue);
          case 2:
            return (R.drawable.orange_blue);
          default:
            return R.drawable.red_blue;
          }
        } else {
          return R.drawable.selected_blue;
        }
      }else{
        return (R.drawable.empty_blue);
      }        
      }
      // TO BE FIXED
      return (R.drawable.empty_blue);

    }
    

  private static int getImageNumber(Tile tile){
    switch(tile.getValue()){
    case 0:
      return (R.drawable.selected_blue);
    case 1:
      return (R.drawable.one_blue);
    case 2:
      return (R.drawable.two_blue);
    case 3:
      return (R.drawable.three_blue);
    case 4:
      return (R.drawable.four_blue);
    case 5:
      return (R.drawable.five_blue);
    case 6:
      return (R.drawable.six_blue);
    case 7:
      return (R.drawable.seven_blue);
    case 8:
      return (R.drawable.eight_blue);
    default:
      return (R.drawable.eight_blue);
    }
      
    }

    
    
    public void setImage(Position pos){
      mThumbIds.add(getImageResource(pos,game));

    }

  public int getCount() {
        return mThumbIds.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;
        //Log.d(TAG, "getView");
        if (convertView == null) {  // if it's not recycled, initialize some attributes
          //Log.d(TAG, "ConvertView == null");
            imageView = new ImageView(mContext);
            DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
            int widthSize = metrics.widthPixels/game.getBoard().getNoOfCols();
            imageView.setLayoutParams(new GridView.LayoutParams(widthSize, widthSize));
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds.get(position));
        return imageView;
    }


   
}




Java Source Code List

com.stealthness.findthemines.ImageAdapter.java
com.stealthness.findthemines.MainActivity.java
com.stealthness.findthemines.SplashScreen.java
findtheminecore.BoardSize.java
findtheminecore.Board.java
findtheminecore.Game.java
findtheminecore.Position.java
findtheminecore.RuleSet.java
findtheminecore.TileType.java
findtheminecore.Tile.java