Android Open Source - Android-FactsGame Divider View






From Project

Back to project page Android-FactsGame.

License

The source code is released under:

Apache License

If you think the Android project Android-FactsGame 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 ie.iamshanedoyle.factsgame.views;
/* w  w w  .  java 2s .  c om*/
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import ie.iamshanedoyle.factsgame.R;

/**
 * This custom view is a divider.
 */
public class DividerView extends View {

    private int mColor;
    private float mThickness;
    private boolean mIsVertical;
    private Paint mPaint;

    public DividerView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.DividerView, 0, 0);

        if (typedArray == null) {
            return;
        }

        try {
            mColor = typedArray.getColor(0, android.R.color.darker_gray);
            mThickness = typedArray.getDimension(1, 2);
            mIsVertical = typedArray.getInt(2, 0) == 0;
        } finally {
            typedArray.recycle();
        }

        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mThickness);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // Get the initial positions.
        int startX = 0;
        int stopX = isVertical() ? 0 : getRight();
        int startY = 0;
        int stopY = isVertical() ? getTop() : 0;

        // Adjust for Padding
        startX += getPaddingLeft();
        startY += getPaddingBottom();
        stopX -= getPaddingRight();
        stopY -= getPaddingTop();

        canvas.drawLine(startX, startY, stopX, stopY, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = widthMeasureSpec;
        int height = heightMeasureSpec;

        if (isVertical()) {
            width = (int) getThickness();
        } else {
            height = (int) getThickness();
        }

        setMeasuredDimension(width, height);
    }

    public int getColor() {
        return mColor;
    }

    public float getThickness() {
        return mThickness;
    }

    public boolean isVertical() {
        return mIsVertical;
    }

    public void setThickness(float mWidth) {
        this.mThickness = mWidth;
        invalidate();
        requestLayout();
    }

    public void setColor(int mColor) {
        this.mColor = mColor;
        invalidate();
        requestLayout();
    }

    public void setIsVertical(boolean mIsVertical) {
        this.mIsVertical = mIsVertical;
        invalidate();
        requestLayout();
    }
}




Java Source Code List

ie.iamshanedoyle.factsgame.Constants.java
ie.iamshanedoyle.factsgame.activities.GameActivity.java
ie.iamshanedoyle.factsgame.activities.HomeActivity.java
ie.iamshanedoyle.factsgame.activities.HowToPlayActivity.java
ie.iamshanedoyle.factsgame.activities.ScoresActivity.java
ie.iamshanedoyle.factsgame.adapters.ScoresAdapter.java
ie.iamshanedoyle.factsgame.models.Question.java
ie.iamshanedoyle.factsgame.models.Score.java
ie.iamshanedoyle.factsgame.views.CountdownTimerView.java
ie.iamshanedoyle.factsgame.views.DividerView.java