Android Open Source - rpg-droid View_ Character Sheet_ Field






From Project

Back to project page rpg-droid.

License

The source code is released under:

Apache License

If you think the Android project rpg-droid 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.thing.rpg_droid.app;
//  ww w .j  a  v  a  2  s  .  c om
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import com.thing.rpg_droid.res.R;

/**
 * Created by jon on 22/06/14.
 */
public class View_CharacterSheet_Field extends View
{
    private String mLabel;

    private int mLabelFontSize;

    private int mLabelWidth;

    private String mValue;

    private int mValueFontSize;

    private int mBorderRadius = 0;

    private int mLabelColor = 0xFF000000;

    private int mValueColor = 0xFF000000;

    private Paint mBorderStroke;

    private Paint mTextBrush = new Paint();

    private RectF mBorderRect;

    private int mInternalPadding;

    private Rect mTextRect = new Rect();

    public View_CharacterSheet_Field(Context pContext)
    {
        this(pContext, null);
    }

    public View_CharacterSheet_Field(Context pContext, AttributeSet pAttrs)
    {
        this(pContext, pAttrs, 0);
    }

    public View_CharacterSheet_Field(Context pContext, AttributeSet pAttrs, int pDefStyleAttr)
    {
        super(pContext, pAttrs, pDefStyleAttr);

        Init(pContext, pAttrs, pDefStyleAttr);
    }

    public String getValue()
    {
        return mValue;
    }

    public void setValue(String pValue)
    {
        mValue = pValue;

        this.invalidate();
    }

    private void Init(Context pContext, AttributeSet pAttrs, int pDefStyleAttr)
    {
        DisplayMetrics dm = getResources().getDisplayMetrics();

        TypedArray aTypedArray = pContext.obtainStyledAttributes(pAttrs, R.styleable.View_CharacterSheet_Field, pDefStyleAttr, 0);

        mValueColor = aTypedArray.getColor(R.styleable.View_CharacterSheet_Field_valueColor, mValueColor);

        mValueFontSize = (int)(Math.round(12 * dm.scaledDensity));
        mValueFontSize = aTypedArray.getDimensionPixelSize(R.styleable.View_CharacterSheet_Field_valueFontSize, mValueFontSize);

        mLabelColor = aTypedArray.getColor(R.styleable.View_CharacterSheet_Field_labelColor, mLabelColor);

        mLabelFontSize = (int)(Math.round(8 * dm.scaledDensity));
        mLabelFontSize = aTypedArray.getDimensionPixelSize(R.styleable.View_CharacterSheet_Field_labelFontSize, mLabelFontSize);

        mBorderRadius = aTypedArray.getDimensionPixelSize(R.styleable.View_CharacterSheet_Field_cornerRadius, mBorderRadius);

        mInternalPadding = (int)(Math.round(5 * dm.scaledDensity));

        mLabel = aTypedArray.getString(R.styleable.View_CharacterSheet_Field_label);
        mValue = aTypedArray.getString(R.styleable.View_CharacterSheet_Field_value);

        mBorderRect = new RectF();

        int lBorderStrokeWidth = aTypedArray.getDimensionPixelSize(R.styleable.View_CharacterSheet_Field_borderWidth, -1);
        int lBorderStrokeColor = aTypedArray.getColor(R.styleable.View_CharacterSheet_Field_borderColor, 0xFFFFFFFF);

        if (lBorderStrokeWidth >= 0)
        {
            mBorderStroke = new Paint();
            mBorderStroke.setStyle(Paint.Style.STROKE);
            mBorderStroke.setColor(lBorderStrokeColor);
            mBorderStroke.setStrokeWidth(lBorderStrokeWidth);
        }
        else
        {
            mBorderStroke = null;
        }

        aTypedArray.recycle();
    }

    @Override
    protected void onMeasure (int pWidthMeasureSpec, int pHeightMeasureSpec)
    {
        int lWidth = getSuggestedMinimumWidth();
        int lHeight = Math.max(getSuggestedMinimumHeight(), Math.max(mLabelFontSize, mValueFontSize));
        float lBorderWidth = 0;

        if (mBorderStroke != null) //catering for hairline divider
        {
            lBorderWidth = mBorderStroke.getStrokeWidth();
            if (lBorderWidth == 0)
                lBorderWidth = 1;
        }

        if ((mLabel != null) && (!mLabel.equals("")))
        {
            mTextBrush.setTextSize(mLabelFontSize);
            mLabelWidth = (int)Math.ceil(mTextBrush.measureText(mLabel));

            lWidth = Math.max(mLabelWidth + mInternalPadding, lWidth);
        }

        if ((mValue != null) && (!mValue.equals("")))
        {
            mTextBrush.setTextSize(mValueFontSize);
            lWidth += (int) Math.ceil(mTextBrush.measureText(mValue));
        }

        lWidth += getPaddingLeft() + getPaddingRight() + 2 * lBorderWidth;

        lHeight += getPaddingTop() + getPaddingBottom() + 2 * lBorderWidth;

        lWidth = resolveSizeAndState(lWidth, pWidthMeasureSpec, 0);
        lHeight = resolveSizeAndState(lHeight, pHeightMeasureSpec, 0);

        setMeasuredDimension(lWidth, lHeight);
    }

    @Override
    protected void onDraw (Canvas pCanvas)
    {
        super.onDraw(pCanvas);

        float lBorderWidth = 0;

        if (mBorderStroke != null) //catering for hairline divider
        {
            lBorderWidth = mBorderStroke.getStrokeWidth();
            if (lBorderWidth == 0)
                lBorderWidth = 1;
        }

        float lPosX = getPaddingLeft() + lBorderWidth;

        float lVertPadding = getPaddingTop() + getPaddingBottom() + 2 * lBorderWidth;

        if ((mLabel != null) && (!mLabel.equals("")))
        {
            mTextBrush.setColor(mLabelColor);
            mTextBrush.setTextSize(mLabelFontSize);
            mTextBrush.getTextBounds(mLabel,0,mLabel.length(),mTextRect);

            pCanvas.drawText(mLabel, lPosX, getPaddingTop() + lBorderWidth - mTextBrush.ascent() - mTextBrush.descent(), mTextBrush);

            lPosX += mLabelWidth + mInternalPadding;
        }

        if ((mValue != null) && (!mValue.equals("")))
        {
            mTextBrush.setColor(mValueColor);
            mTextBrush.setTextSize(mValueFontSize);

            lPosX = getWidth() - (lBorderWidth + getPaddingRight()) - (int)Math.ceil(mTextBrush.measureText(mValue));

            pCanvas.drawText(mValue, lPosX, (getHeight() - (mTextBrush.ascent() + mTextBrush.descent())) / 2, mTextBrush);
        }

        if (mBorderStroke != null)
        {
            mBorderRect.set(0, 0, getWidth()-1, getHeight()-1);

            if (mBorderRadius != 0)
                pCanvas.drawRoundRect(mBorderRect, mBorderRadius, mBorderRadius, mBorderStroke);
            else
                pCanvas.drawRect(mBorderRect, mBorderStroke);
        }
        //paint divider
    }

}




Java Source Code List

com.thing.rpg_droid.Utils.DieRoller.java
com.thing.rpg_droid.Utils.DieType.java
com.thing.rpg_droid.app.Activity_CharacterList.java
com.thing.rpg_droid.app.Activity_Charsheet.java
com.thing.rpg_droid.app.CharSheet_PageInfo.java
com.thing.rpg_droid.app.Fragment_CharacterList.java
com.thing.rpg_droid.app.ICharacter.java
com.thing.rpg_droid.app.PagerAdapter_Charsheet.java
com.thing.rpg_droid.app.ViewBinder.java
com.thing.rpg_droid.app.View_CharacterSheet_Field.java
com.thing.rpg_droid.app.dummy.DummyContent.java
com.thing.rpg_droid.pathfinder.Ability.java
com.thing.rpg_droid.pathfinder.Adapters.java
com.thing.rpg_droid.pathfinder.Appearance.java
com.thing.rpg_droid.pathfinder.ArmorClass.java
com.thing.rpg_droid.pathfinder.BodySlot.java
com.thing.rpg_droid.pathfinder.Character.java
com.thing.rpg_droid.pathfinder.Fragment_Basic.java
com.thing.rpg_droid.pathfinder.Fragment_Gear.java
com.thing.rpg_droid.pathfinder.Fragment_Skills.java
com.thing.rpg_droid.pathfinder.Fragment_Spells_Abilities.java
com.thing.rpg_droid.pathfinder.Fragment_Status.java
com.thing.rpg_droid.pathfinder.IInventoryLocation.java
com.thing.rpg_droid.pathfinder.Initiative.java
com.thing.rpg_droid.pathfinder.SavingThrow.java
com.thing.rpg_droid.pathfinder.SizeModifier.java
com.thing.rpg_droid.pathfinder.Skill.java
com.thing.rpg_droid.pathfinder.Spell.java
com.thing.rpg_droid.pathfinder.Status.java