Android Open Source - GhostStories Numbered Image View






From Project

Back to project page GhostStories.

License

The source code is released under:

GNU General Public License

If you think the Android project GhostStories 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.views;
//  w  w  w  .jav  a 2 s. c o  m
import com.views.listeners.DragTouchListener;

import games.ghoststories.R;
import games.ghoststories.utils.GameUtils;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * {@link ImageView} that overlays a number on top of the image. By default the
 * number is centered on the image and the image is made semi-transparent when
 * the number count reaches zero.  
 */
public class NumberedImageView extends ImageView {

   /**
    * Default drag listener for a numbered image view. This will not allow 
    * dragging if the number is 0.
    */
   public static class DefaultDragTouchListener extends DragTouchListener {
      /*
       * (non-Javadoc)
       * @see com.views.listeners.DragTouchListener#onTouch(android.view.View, android.view.MotionEvent)
       */
      @Override
      public boolean onTouch(View pView, MotionEvent pEvent) {
         if(pView instanceof NumberedImageView) {
            if(((NumberedImageView)pView).mNumber > 0) {
               return super.onTouch(pView, pEvent);
            }
         }
         
         return false;
      }
   }
   
   /**
    * Constructor
    * @param pContext The context the view is running in
    */
   public NumberedImageView(Context pContext) {
      super(pContext);
   }
   
   /**
    * Constructor 
    * @param pContext The context the view is running in
    * @param pAttrs The attributes of this view
    */
   public NumberedImageView(Context pContext, AttributeSet pAttrs) {
      this(pContext, pAttrs, 0);
   }
   
   /**
    * Constructor 
    * @param pContext The context used to build the view
    * @param pAttrs The attributes of the view
    * @param pDefStyle The default style applied to this view
    */
   public NumberedImageView(Context pContext, AttributeSet pAttrs, int pDefStyle) {
      this(pContext, pAttrs, pDefStyle, sDefaultXOffset, sDefaultYOffset, -1, sDefaultColor);      
   }
   
   /**
    * Constructor
    * @param pContext The context used to build the view
    * @param pAttrs The attributes of the view
    * @param pDefStyle The default style applied to this view
    * @param pDefaultXOffset The default value of the x offset if not set as one
    *                        of the attributes
    * @param pDefaultYOffset The default value of the y offset if not set as one
    *                        of the attributes
    * @param pDefaultTextSize The default value of the text size if not set as
    *                         one of the attributes
    */
   public NumberedImageView(Context pContext, AttributeSet pAttrs, int pDefStyle,
         float pDefaultXOffset, float pDefaultYOffset, int pDefaultTextSize,
         int pDefaultColor) {
      super(pContext, pAttrs, pDefStyle);
      mXOffset = pDefaultXOffset;
      mYOffset = pDefaultYOffset;          
      
      mPaint = new Paint();
      mPaint.setColor(pDefaultColor);
      mPaint.setStyle(Style.FILL);            
      mPaint.setTextSize(pDefaultTextSize);      
      mPaint.setTextAlign(Align.CENTER);
      mPaint.setTypeface(Typeface.SERIF);
      
      //Read in the attributes and set the values if specified
      TypedArray a = pContext.obtainStyledAttributes(pAttrs,
            R.styleable.NumberedImageView);
      for (int i = 0; i < a.getIndexCount(); ++i)
      {
         int attr = a.getIndex(i);
         switch (attr)
         {
         case R.styleable.NumberedImageView_text_size:            
            mPaint.setTextSize(a.getInteger(attr, pDefaultTextSize));            
            break;
         case R.styleable.NumberedImageView_x_offset:
            mXOffset = a.getFloat(attr, pDefaultXOffset);
            break;
         case R.styleable.NumberedImageView_y_offset:
            mYOffset = a.getFloat(attr, pDefaultYOffset);
            break;
         case R.styleable.NumberedImageView_text_color:
            mPaint.setColor(a.getInteger(attr, pDefaultColor));
            break;
         }
      }
      a.recycle();      
   }
   
   /**
    * Decrement the number by 1
    */
   public void decrement() {
      setNumber(getNumber() - 1);
   }
   
   /**
    * @return The text size of the number overlay
    */
   public int getTextSize() {
      return (int)(mPaint.getTextSize());
   }
   
   /**
    * @return The x offset of the number overlay
    */
   public float getXOffset() {
      return mXOffset;
   }
   
   /**
    * @return The y offset of the number overlay
    */
   public float getYOffset() {
      return mYOffset;
   }
   
   /**
    * @return The overlay number
    */
   public int getNumber() {
      return mNumber;
   }
   
   /**
    * Increment the number by 1
    */
   public void increment() {
      setNumber(getNumber()+1);
   }
   
   /**
    * Sets the number shown in the overlay
    * @param pNumber The number shown in the overlay
    */
   public void setNumber(int pNumber) {
      mNumber = pNumber;
      //Trigger a repaint of this component when the number changes
      GameUtils.invalidateView(this);  
   }
   
   /**
    * Sets whether or not to show the number
    * @param pShowNumber Whether or not to show the number
    */
   public void setShowNumber(boolean pShowNumber) {
      mShowNumber = pShowNumber;
      //Trigger a repaint of this component when the number changes
      GameUtils.invalidateView(this);
   }
   
   /**
    * Sets the color for the text
    * @param pColor The color of the text
    */
   public void setTextColor(int pColor) {
      mPaint.setColor(pColor);
      GameUtils.invalidateView(this);
   }
   
   /**
    * Sets the text size for the number overlay
    * @param pTextSize The text size of the number overlay
    */
   public void setTextSize(int pTextSize) {
      mPaint.setTextSize(pTextSize);      
      //Trigger a repaint of this component when the text size changes
      GameUtils.invalidateView(this);
   }
   
   /**
    * Sets the x offset for the number overlay. This is a percentage of the
    * total width of the component.
    * @param pXOffset The x offset for the number overlay
    */
   public void setXOffset(float pXOffset) {
      mXOffset = pXOffset;
      //Trigger a repaint of this component when the offset changes
      GameUtils.invalidateView(this);
   }
   
   /**
    * Sets the y offset for the number overlay. This is a percentage of the
    * total height of the component.
    * @param pYOffset The y offset for the number overlay
    */
   public void setYOffset(float pYOffset) {
      mYOffset = pYOffset;
      //Trigger a repaint of this component when the offset changes
      GameUtils.invalidateView(this);
   }
   
   /**
    * Called during the draw operation to update the alpha value based on the
    * current number. By default makes the image semi-transparent if the number
    * is zero.
    */
   protected void updateAlpha() {      
      //If 0 then make semi-transparent
      if(mNumber == 0) {
         setAlpha(sZeroAlpha);
      } else {
         setAlpha(1.0f);
      }
   }
   
   /*
    * (non-Javadoc)
    * @see android.widget.ImageView#onDraw(android.graphics.Canvas)
    */
   @Override
   protected void onDraw(Canvas pCanvas) {
      updateAlpha();
      super.onDraw(pCanvas);           
      
      //Draw the number on top of the image
      if(mShowNumber) {
         pCanvas.drawText(Integer.toString(mNumber), 
               getWidth() * mXOffset, getHeight() * mYOffset, mPaint);
      }
   } 
   
   /** The default text color **/
   protected static final int sDefaultColor = Color.WHITE;
   /** The paint to use for the number **/
   protected Paint mPaint;
   
   /** The default x offset **/
   private static final float sDefaultXOffset = 0.5f;
   /** The default y offset **/
   private static final float sDefaultYOffset = 0.5f;
   /** The alpha value when the number is zero **/
   private static final float sZeroAlpha = 0.3f;      
   /** The number to show in the overlay **/
   private int mNumber;   
   /** Whether or not to show the number **/
   private boolean mShowNumber = true;
   /** The x offset of the number overlay **/     
   private float mXOffset;
   /** The y offset of the number overlay **/
   private float mYOffset;   
}




Java Source Code List

com.drawable.shapes.GradientRectangle.java
com.interfaces.IDraggable.java
com.utils.AndroidUtils.java
com.utils.AnimationUtils2.java
com.utils.ImageLoadingTask.java
com.utils.ImageRotationTask.java
com.utils.ImageViewUtils.java
com.views.NumberedImageView.java
com.views.ToggledImageView.java
com.views.layouts.ScaledLinearLayout.java
com.views.layouts.ScaledRelativeLayout.java
com.views.layouts.SquareGridLayout.java
com.views.layouts.SquareLinearLayout.java
com.views.layouts.SquareTableLayout.java
com.views.layouts.ZoomableRelativeLayout.java
com.views.listeners.DragTouchListener.java
games.ghoststories.activities.GameLoadingActivity.java
games.ghoststories.activities.GameScreenActivity.java
games.ghoststories.activities.TitleActivity.java
games.ghoststories.controllers.GhostDeckController.java
games.ghoststories.controllers.HaunterController.java
games.ghoststories.controllers.PlayerBoardCardController.java
games.ghoststories.controllers.VillageTileController.java
games.ghoststories.controllers.combat.CombatAreaController.java
games.ghoststories.controllers.combat.DiceDragListener.java
games.ghoststories.controllers.combat.GhostDragListener.java
games.ghoststories.controllers.combat.TaoTokenDragListener.java
games.ghoststories.data.DragData.java
games.ghoststories.data.GameBoardData.java
games.ghoststories.data.GhostData.java
games.ghoststories.data.GhostDeckData.java
games.ghoststories.data.GhostGraveyardData.java
games.ghoststories.data.GhostStoriesBitmaps.java
games.ghoststories.data.GhostStoriesConstants.java
games.ghoststories.data.GhostStoriesGameManager.java
games.ghoststories.data.PlayerData.java
games.ghoststories.data.TokenSupplyData.java
games.ghoststories.data.interfaces.IGameBoardListener.java
games.ghoststories.data.interfaces.IGamePhaseListener.java
games.ghoststories.data.interfaces.IGameTokenListener.java
games.ghoststories.data.interfaces.IGhostDeckListener.java
games.ghoststories.data.interfaces.IGhostListener.java
games.ghoststories.data.interfaces.ITokenListener.java
games.ghoststories.data.interfaces.IVillageTileListener.java
games.ghoststories.data.village.BuddhistTempleTileData.java
games.ghoststories.data.village.CircleOfPrayerTileData.java
games.ghoststories.data.village.VillageTileDataFactory.java
games.ghoststories.data.village.VillageTileData.java
games.ghoststories.enums.EBoardLocation.java
games.ghoststories.enums.ECardLocation.java
games.ghoststories.enums.EColor.java
games.ghoststories.enums.ECombatPhase.java
games.ghoststories.enums.EDiceSide.java
games.ghoststories.enums.EDice.java
games.ghoststories.enums.EDifficulty.java
games.ghoststories.enums.EDragItem.java
games.ghoststories.enums.EGamePhase.java
games.ghoststories.enums.EGhostAbility.java
games.ghoststories.enums.EHaunterLocation.java
games.ghoststories.enums.EPlayerAbility.java
games.ghoststories.enums.ETileLocation.java
games.ghoststories.enums.EVillageTile.java
games.ghoststories.fragments.AuxAreaFragment.java
games.ghoststories.fragments.GameboardFragment.java
games.ghoststories.utils.BitmapUtils.java
games.ghoststories.utils.GameUtils.java
games.ghoststories.utils.XmlUtils.java
games.ghoststories.views.GameScreen.java
games.ghoststories.views.aux_area.CardInfoView.java
games.ghoststories.views.aux_area.GamePhaseDetailsView.java
games.ghoststories.views.aux_area.GamePhaseView.java
games.ghoststories.views.aux_area.GhostDeckView.java
games.ghoststories.views.aux_area.GhostGraveyardCardView.java
games.ghoststories.views.aux_area.PlayerInfoView.java
games.ghoststories.views.combat.CombatDamageView.java
games.ghoststories.views.combat.CombatDiceAreaView.java
games.ghoststories.views.combat.CombatDiceView.java
games.ghoststories.views.combat.CombatGhostView.java
games.ghoststories.views.combat.CombatInstructionsView.java
games.ghoststories.views.combat.CombatRollView.java
games.ghoststories.views.combat.CombatView.java
games.ghoststories.views.combat.ExtraCombatDiceView.java
games.ghoststories.views.combat.GhostHealthView.java
games.ghoststories.views.common.AbstractNumberedTokenView.java
games.ghoststories.views.common.BuddhaTokenView.java
games.ghoststories.views.common.QiTokenView.java
games.ghoststories.views.common.TaoTokenView.java
games.ghoststories.views.common.YinYangTokenView.java
games.ghoststories.views.gameboard.PlayerAreaView.java
games.ghoststories.views.gameboard.PlayerBoardCardView.java
games.ghoststories.views.gameboard.PlayerBoardView.java
games.ghoststories.views.gameboard.PlayerTokenAreaView.java
games.ghoststories.views.gameboard.VillageTileView.java
games.ghoststories.views.graveyard.GraveyardScrollView.java
games.ghoststories.views.graveyard.GraveyardView.java
games.ghoststories.views.title.TitleButton.java
games.ghoststories.views.title.TitleScreen.java
games.ghoststories.views.village.CircleOfPrayerView.java