Android Open Source - SeeKampf Ocean View






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf 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 net.avedo.seekampf.core;
/*from ww  w  .  ja va2s  .  c o m*/
import net.avedo.seekampf.R;
import net.avedo.seekampf.models.Island;
import net.avedo.seekampf.utils.Constants;

import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;

public class OceanView extends View {
    private GestureDetectorCompat gestureDetector;
  private OverScroller scroller;

  private int viewWidth;
  private int viewHeight;

  private int positionX = 0;
  private int positionY = 0;
  
  private Paint oceanPaint = new Paint();
  private Paint islandPaint = new Paint();
  private Paint ownIslandPaint = new Paint();
  private Paint alliIslandPaint = new Paint();
  private Paint reservedIslandPaint = new Paint();
  private Paint borderPaint = new Paint();
  private Paint backgroundPaint = new Paint();
  private Paint textPaint = new Paint();
  private Paint textBoxPaint = new Paint();
  private Paint boxMarkPaint = new Paint();

  private int gridSize;
  private int cellSize;
  private int islandDim;
  private int oceanColor;
  private int borderColor;
  private int backgroundColor;
  private int islandColor;
  private int ownIslandColor;
  private int alliIslandColor;
  private int reservedIslandColor;
  private int textSize;
  private int textColor;
  private int textBoxColor;
  private int boxMarkColor;
  
  private Island[] islands;
  
  private Island islandInfo;
  private String coord;
  
  private final GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
    @Override
    public boolean onDown(MotionEvent e) {
      // Aborts any active scroll animations and invalidates.
      scroller.forceFinished(true);
      ViewCompat.postInvalidateOnAnimation(OceanView.this);
      return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
      scroller.forceFinished(true);
      scroller.fling(positionX, positionY,
          (int) -velocityX, (int) -velocityY, 
          0, getMaxHorizontal(), 0, getMaxVertical());
      ViewCompat.postInvalidateOnAnimation(OceanView.this);
      return true;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
        float distanceX, float distanceY) {
      scroller.forceFinished(true);
      
      int dx = (int) distanceX;
      int dy = (int) distanceY;
      
      int newPositionX = positionX + dx;
      int newPositionY = positionY + dy;
      
      if(newPositionX < 0) {
        dx -= newPositionX;
      } else if(newPositionX > getMaxHorizontal()) {
        dx -= (newPositionX - getMaxHorizontal());
      }
      
      if(newPositionY < 0) {
        dy -= newPositionY;
      } else if(newPositionY > getMaxVertical()) {
        dy -= (newPositionY - getMaxVertical());
      }
      
      scroller.startScroll(positionX, positionY, dx, dy, 0);
      ViewCompat.postInvalidateOnAnimation(OceanView.this);
      
      return true;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
            // Fetch the target island ...
            fetchIslandByCoords(e);

            // ... and update the ocean view.
      OceanView.this.invalidate();
      OceanView.this.requestLayout();
      
      return true;
    }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // Fetch the target island ...
            fetchIslandByCoords(e);

            // ... and send the intent to the corresponding activity.
            Intent intent = new Intent(getContext(), RestDetailsActivity.class);
            intent.putExtra(Constants.INTENT_EXTRA_ID, islandInfo.getId());
            intent.putExtra(Constants.INTENT_EXTRA_FRAGMENT, Constants.FRAGMENT_ISLAND_DETAILS);
            getContext().startActivity(intent);

      return true;
        }

        private void fetchIslandByCoords(MotionEvent e) {
            // Fetch the click coordinates,
            int x = (int) (e.getX());
            int y = (int) (e.getY());

            // ... calculate the actual coordinates ...
            int viewPosX = positionX + x - getPaddingLeft();
            int viewPosY = positionY + y - getPaddingTop();

            int cellDim = getCellSize() * getIslandDim() + 1;

            // ... and reconstruct the map coordinates.
            int xc = (viewPosX - (viewPosX % cellDim)) / cellDim + 1;
            int yc = (viewPosY - (viewPosY % cellDim)) / cellDim + 1;
            int cc = ((viewPosX % cellDim) / getIslandDim())
                    + getCellSize() * ((viewPosY % cellDim) / getIslandDim()) + 1;

            coord = "" + xc + ":" + yc + ":" + cc;

            // Find the corresponding island to the coordinates.
            islandInfo = null;

            for(int i = 0; islands != null && i < islands.length; i++) {
                if(islands[i].getKoordinaten().equals("" + xc + ":" + yc + ":" + cc)) {
                    islandInfo = islands[i];
                    break;
                }
            }
        }
  };

  public OceanView(Context context, AttributeSet attr) {
    super(context, attr);
    
    TypedArray localTypedArray = context.getTheme()
        .obtainStyledAttributes(attr,
            R.styleable.OceanView, 0, 0);
    try {
      this.oceanColor = localTypedArray.getInteger(0, Color.BLUE);
      this.oceanPaint.setColor(this.oceanColor);
      this.oceanPaint.setStyle(Paint.Style.FILL);
      
      this.islandColor = localTypedArray.getInteger(1, Color.WHITE);
      this.islandPaint.setColor(this.islandColor);
      this.islandPaint.setStyle(Paint.Style.FILL);
      
      this.ownIslandColor = localTypedArray.getInteger(2, Color.YELLOW);
      this.ownIslandPaint.setColor(this.ownIslandColor);
      this.ownIslandPaint.setStyle(Paint.Style.FILL);
      
      this.alliIslandColor = localTypedArray.getInteger(3, Color.GREEN);
      this.alliIslandPaint.setColor(this.alliIslandColor);
      this.alliIslandPaint.setStyle(Paint.Style.FILL);
      
      this.reservedIslandColor = localTypedArray.getInteger(4, Color.RED);
      this.reservedIslandPaint.setColor(this.reservedIslandColor);
      this.reservedIslandPaint.setStyle(Paint.Style.FILL);
      
      this.borderColor = localTypedArray.getInteger(5, Color.GREEN);
      this.borderPaint.setColor(this.borderColor);
      this.borderPaint.setStyle(Paint.Style.FILL);
      
      this.backgroundColor = localTypedArray.getInteger(6, Color.WHITE);
      this.backgroundPaint.setColor(this.backgroundColor);
      this.backgroundPaint.setStyle(Paint.Style.FILL);
      
      this.gridSize = localTypedArray.getInteger(7, 25);
      this.cellSize = localTypedArray.getInteger(8, 5);
      this.islandDim = localTypedArray.getInteger(9, 10);
      
      this.textSize = localTypedArray.getInteger(10, 11);
      this.textColor = localTypedArray.getInteger(11, Color.WHITE);
      
      this.textPaint.setColor(this.textColor);
      this.textPaint.setStyle(Paint.Style.FILL);
      this.textPaint.setTextSize(this.textSize);
      this.textPaint.setTypeface(Typeface.DEFAULT_BOLD);
      this.textPaint.setAntiAlias(true);
      
      this.textBoxColor = localTypedArray.getInteger(12, Color.DKGRAY);
      this.textBoxPaint.setColor(this.textBoxColor);
      this.textBoxPaint.setStyle(Paint.Style.FILL);
      this.textBoxPaint.setAntiAlias(true);
      
      this.boxMarkColor = localTypedArray.getInteger(13, Color.CYAN);
      this.boxMarkPaint.setColor(this.boxMarkColor);
      this.boxMarkPaint.setStyle(Paint.Style.FILL);
    } finally {
      localTypedArray.recycle();
    }
    
        // Sets up interactions
        gestureDetector = new GestureDetectorCompat(context, gestureListener);
        scroller = new OverScroller(context);
  }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean retVal = gestureDetector.onTouchEvent(event);
        return retVal || super.onTouchEvent(event);
    }
    
    @Override
    public void computeScroll() {
      super.computeScroll();
      
      if(scroller.computeScrollOffset()) {
        positionX = scroller.getCurrX();
        positionY = scroller.getCurrY();
        scrollTo(positionX, positionY);
      } else {
        scroller.springBack(
            positionX, positionY, 
            0, getMaxHorizontal(), 
            0, getMaxVertical());
      }
    }
    
  private int getContentWidth() {
    return this.getGridSize() * this.getCellSize() * this.getIslandDim() + (this.getGridSize() - 1);
  }
    
  private int getContentHeight() {
    return this.getGridSize() * this.getCellSize() * this.getIslandDim() + (this.getGridSize() - 1);
  }

  private int getMaxHorizontal() {
    return getContentWidth() + getPaddingLeft() + getPaddingRight() - viewWidth;
  }

  private int getMaxVertical() {
    return getContentHeight() + getPaddingTop() + getPaddingBottom() - viewHeight;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    // Call the parent draw method.
    super.onDraw(canvas);
    
    // Draw the background color including the
    canvas.drawRect(0, 0, getPaddingLeft() + getContentWidth()
        + getPaddingRight(), getPaddingTop() + getContentHeight()
        + getPaddingBottom(), this.backgroundPaint);

    // Fill the complete bitmap with the ocean color.
    canvas.drawRect(getPaddingLeft(), getPaddingTop(), getPaddingLeft()
        + getContentWidth(), getPaddingTop() + getContentHeight(),
        this.oceanPaint);
    
    // Draw the vertical ...
    for (int x = 1; x < this.getGridSize(); ++x) {
      canvas.drawLine(
          getPaddingLeft() + shiftGridCells(x), 
          getPaddingTop(),
          getPaddingLeft() + shiftGridCells(x), 
          getPaddingTop() + getContentHeight(),
          this.borderPaint);
    }

    // ... and the horizontal lines.
    for (int y = 1; y < this.getGridSize(); ++y) {
      canvas.drawLine(
          getPaddingLeft(), 
          getPaddingTop() + shiftGridCells(y),
          getPaddingLeft() + getContentWidth(), 
          getPaddingTop() + shiftGridCells(y),
          this.borderPaint);
    }
    
    // Finally draw the islands.
    if(islands != null) {
      for(int i = 0; i < islands.length; i++) {
        String[] coords = islands[i].getKoordinaten().split(":");
        Integer x = Integer.valueOf(coords[0]) - 1;
        Integer y = Integer.valueOf(coords[1]) - 1;
        Integer c = Integer.valueOf(coords[2]) - 1;
        
        float worldX = 
            getPaddingLeft()
            + shiftGridCells(x) // shifting full grid cells
            + (c % getCellSize()) * this.getIslandDim(); // shifting of island positions
        
        float worldY = 
            getPaddingTop()
            + shiftGridCells(y) // shifting full grid cells
            + ((c - (c % getCellSize())) / getCellSize()) * this.getIslandDim(); // shifting of island positions
        
        canvas.drawRect(
            worldX, worldY, worldX + this.getIslandDim(), worldY + this.getIslandDim(), 
            this.islandPaint);
      }
    }
    
    if(islandInfo != null) {
      // Calculate the position of the island within the view ...
      String[] coords = islandInfo.getKoordinaten().split(":");
      Integer x = Integer.valueOf(coords[0]) - 1;
      Integer y = Integer.valueOf(coords[1]) - 1;
      Integer c = Integer.valueOf(coords[2]) - 1;
      
      float worldX = 
          getPaddingLeft()
          + shiftGridCells(x) // shifting full grid cells
          + (c % getCellSize()) * this.getIslandDim(); // shifting of island positions
      
      float worldY = 
          getPaddingTop()
          + shiftGridCells(y) // shifting full grid cells
          + ((c - (c % getCellSize())) / getCellSize()) * this.getIslandDim(); // shifting of island positions
      
      // ... setup the island description, ...
      String desc = islandInfo.getInselname() + " (" +  islandInfo.getKoordinaten() + ")";
      
      // ... fetch the text dimensions, ...
      int textWidth = (int) textPaint.measureText(desc, 0, desc.length());
      int textHeight = (int) Math.abs(textPaint.descent() + textPaint.ascent());
      
      // ... calculate the box offset ...
      int offsetX = (int) (worldX + textHeight + this.getIslandDim());
      int offsetY = (int) (worldY - (4 * textHeight));
      
      if(offsetX + textWidth + (2 * textHeight) > getContentWidth() + getPaddingLeft()) {
        offsetX = (int) (worldX - textWidth - (3 * textHeight));
      }
      
      if(offsetY - (3 * textHeight) < getPaddingTop()) {
        offsetY = (int) (worldY + textHeight + this.getIslandDim());
      }
      
      // ... and draw the island info box.
      RectF messageBox = new RectF(offsetX, offsetY, offsetX + textWidth + (2 * textHeight), offsetY + (3 * textHeight));
      canvas.drawRoundRect(messageBox, 7.0f, 7.0f, this.textBoxPaint);
      canvas.drawText(desc, offsetX + textHeight, offsetY + textHeight + textHeight, textPaint);
    }
    
    if(coord != null) {
      String[] coords = coord.split(":");
      Integer x = Integer.valueOf(coords[0]) - 1;
      Integer y = Integer.valueOf(coords[1]) - 1;
      Integer c = Integer.valueOf(coords[2]) - 1;
      
      float worldX = 
          getPaddingLeft()
          + shiftGridCells(x) // shifting full grid cells
          + (c % getCellSize()) * this.getIslandDim(); // shifting of island positions
      
      float worldY = 
          getPaddingTop()
          + shiftGridCells(y) // shifting full grid cells
          + ((c - (c % getCellSize())) / getCellSize()) * this.getIslandDim(); // shifting of island positions
      
      canvas.drawRect(
          worldX, worldY, worldX + this.getIslandDim(), worldY + this.getIslandDim(), 
          this.boxMarkPaint);
    }
  }
  
  private int shiftGridCells(int n) {
    return n * this.getCellSize() * this.getIslandDim() + n;
  }
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
    viewWidth = measureWidth(widthMeasureSpec);
    viewHeight = measureHeight(heightMeasureSpec);
    
    setMeasuredDimension(viewWidth, viewHeight);
  }

  private int measureWidth(int widthMeasureSpec) {
    return Math.max(getSuggestedMinimumWidth(),
        resolveSize(this.getContentWidth() + getPaddingLeft() + getPaddingRight(),
                        widthMeasureSpec));
  }

  private int measureHeight(int heightMeasureSpec) {
    return Math.max(getSuggestedMinimumHeight(),
        resolveSize(this.getContentHeight() + getPaddingTop() + getPaddingBottom(),
                    heightMeasureSpec));
  }

  public int getGridSize() {
    return gridSize;
  }

  public void setGridSize(int gridSize) {
    this.gridSize = gridSize;
  }

  public int getCellSize() {
    return cellSize;
  }

  public void setCellSize(int cellSize) {
    this.cellSize = cellSize;
  }

  public int getIslandDim() {
    return islandDim;
  }

  public void setIslandDim(int islandDim) {
    this.islandDim = islandDim;
  }
  
  public int getOceanColor() {
    return oceanColor;
  }

  public void setOceanColor(int oceanColor) {
    this.oceanColor = oceanColor;
  }

  public int getBorderColor() {
    return borderColor;
  }

  public void setBorderColor(int borderColor) {
    this.borderColor = borderColor;
  }

  public int getIslandColor() {
    return islandColor;
  }

  public void setIslandColor(int islandColor) {
    this.islandColor = islandColor;
  }

  public int getOwnIslandColor() {
    return ownIslandColor;
  }

  public void setOwnIslandColor(int ownIslandColor) {
    this.ownIslandColor = ownIslandColor;
  }

  public int getAlliIslandColor() {
    return alliIslandColor;
  }

  public void setAlliIslandColor(int alliIslandColor) {
    this.alliIslandColor = alliIslandColor;
  }

  public int getReservedIslandColor() {
    return reservedIslandColor;
  }

  public void setReservedIslandColor(int reservedIslandColor) {
    this.reservedIslandColor = reservedIslandColor;
  }

  public void setIslands(Island[] islands) {
    this.islands = islands;
    invalidate();
    requestLayout();
  }
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java