Android Open Source - PTPageView P T Page View






From Project

Back to project page PTPageView.

License

The source code is released under:

MIT License

If you think the Android project PTPageView 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.ttpho.ptpageview;
//w w  w.j  ava  2s.  c  o  m
import com.ttpho.ptpagecontrols.R;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class PTPageView extends View {

  /**
   * Cc thu?c tnh dng attribute -S? l??ng item -mu nomal -mu state -size
   * item width -size item height
   **/

  private int mState = 0;
  private int mSize = 1;
  private Paint mPaintNormalState;
  private Paint mPaintState;
  private RectF mRectF = new RectF();
  /* Kch th??c chi??u cao t?i thi?u c?a ViewPagerState (m?c ??nh 16dp) */
  public static float HEIGHT_SIZE;
  /*
   * Kch th??c chi??u r?ng(RECT) hay ????ng knh (CIRCLE) cc item state c?a
   * ViewPagerState (m?c ??nh 10dp)
   */
  public static float WIDTH_STATE_SIZE;
  /* Kho?ng cch c?a cc item state ViewPagerState (m?c ??nh 5dp) */
  public static float SPACE;
  /*
   * Kch th??c chi??u cao (RECT) cc item state c?a ViewPagerState (m?c ??nh
   * 4dp)
   */
  public static float HEIGHT_STATE_SIZE;

  /* ViewPagerState d?ng hnh ch? nh?t */
  public static final int RECT = 1;
  /* ViewPagerState d?ng hnh trn (m?c ??nh) */
  public static final int CIRCLE = 0;
  private int mStype = CIRCLE;

  public int getStype() {
    return mStype;
  }

  public void setStype(int stype) {
    if (mStype != stype)
      this.mStype = stype;
    invalidate();
  }

  private static float OVAL;;
  private int mColorState = Color.parseColor("#e0e1e2");
  private int mColorNormalState = Color.parseColor("#e14b40");
  private float mWidthItem;
  private float mHeightItem;

  public PTPageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
    readFromAttribute(context, attrs, defStyle);
  }

  public PTPageView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public PTPageView(Context context) {
    this(context, null, 0);
  }

  // ???c t? attribute
  private void readFromAttribute(Context context, AttributeSet attrs,
      int defStyle) {
    if (attrs != null) {
      TypedArray a = context.obtainStyledAttributes(attrs,
          R.styleable.PTPageView, defStyle, 0);
      mColorNormalState = a.getColor(
          R.styleable.PTPageView_pageIndicatorTintColor,
          mColorNormalState);
      mColorState = a.getColor(
          R.styleable.PTPageView_currentPageIndicatorTintColor,
          mColorState);
      mPaintNormalState.setColor(mColorNormalState);
      mPaintState.setColor(mColorState);

      mSize = a.getInt(R.styleable.PTPageView_numberOfPages, 1);
      mWidthItem = a.getFloat(R.styleable.PTPageView_widthItem,
          mWidthItem);
      mHeightItem = a.getFloat(R.styleable.PTPageView_heightItem,
          mWidthItem);
      if (mWidthItem > 0 && mHeightItem > 0) {

        if (mWidthItem == mHeightItem) {
          this.mWidthItem = Math.min(mWidthItem, WIDTH_STATE_SIZE);
          this.mHeightItem = this.mWidthItem;
          mStype = PTPageView.CIRCLE;

        } else {
          this.mWidthItem = Math.min(mWidthItem, WIDTH_STATE_SIZE);
          this.mHeightItem = Math.min(mHeightItem, HEIGHT_STATE_SIZE);
          mStype = PTPageView.RECT;
        }
      } else {
        mWidthItem = WIDTH_STATE_SIZE;
        mHeightItem = HEIGHT_STATE_SIZE;
      }
      a.recycle();
    }
  }

  private void init() {
    float one_dp = Resources.getSystem().getDisplayMetrics().densityDpi / 160.0f;
    HEIGHT_SIZE = 16 * one_dp;
    WIDTH_STATE_SIZE = 10 * one_dp;
    SPACE = 10 * one_dp;
    HEIGHT_STATE_SIZE = 4 * one_dp;
    OVAL = one_dp;
    mPaintNormalState = new Paint();
    mPaintNormalState.setAntiAlias(true);
    mPaintState = new Paint();
    mPaintState.setAntiAlias(true);
    mWidthItem = WIDTH_STATE_SIZE;
    mHeightItem = HEIGHT_STATE_SIZE;

  }

  /**
   * Tr? v?? chi??u r?ng c?a cc item Trong tr???ng h??p l item hnh trn ?y l
   * ????ng knh c?a Item
   * 
   * @return
   */
  public float getWidthItem() {
    return mWidthItem;
  }

  /**
   * Kh?i gn kch th??c c?a cc Item. <b>Trong t???ng h?p mWidthItem ==
   * mHeightItem th item ny s? l hnh trn, ??ng th??i ?y c?ng l chi??u di
   * ????ng knh</b>
   * 
   * @param mWidthItem
   *            : chi??u r?ng
   * @param mHeightItem
   *            :chi??u cao
   */
  public void setSizeItem(float mWidthItem, float mHeightItem) {
    if (mWidthItem > 0 && mHeightItem > 0) {

      if (mWidthItem == mHeightItem) {
        this.mWidthItem = Math.min(mWidthItem, WIDTH_STATE_SIZE);
        this.mHeightItem = this.mWidthItem;
        mStype = PTPageView.CIRCLE;

      } else {
        this.mWidthItem = Math.min(mWidthItem, WIDTH_STATE_SIZE);
        this.mHeightItem = Math.min(mHeightItem, HEIGHT_STATE_SIZE);
        mStype = PTPageView.RECT;
      }
      invalidate();
    }
  }

  /**
   * Tr? v?? chi??u cao c?a cc item Trong tr???ng h??p l item hnh trn ?y l
   * ????ng knh c?a Item
   * 
   * @return
   */
  public float getHeightItem() {
    return mHeightItem;
  }

  /**
   * gn mu th? hi?n tr?ng/ v? tr Pager, m?c ??nh
   * Color.parseColor("#e0e1e2")
   * 
   * @param colorState
   *            l m mu theo integer
   */
  public void setColorState(int colorState) {
    if (mColorState != colorState) {
      mColorState = colorState;
      mPaintState.setColor(mColorState);
      invalidate();
    }
  }

  /**
   * gn mu n??n c?a cc tr?ng thi khng th? hi?n v? tr Pager, m?c ??nh
   * Color.parseColor("#e14b40")
   * 
   * @param colorNormalState
   *            l m mu theo integer
   */
  public void setColorNormalState(int colorNormalState) {
    if (mColorNormalState != colorNormalState) {
      mColorNormalState = colorNormalState;
      mPaintNormalState.setColor(mColorNormalState);
      invalidate();
    }
  }

  /**
   * @return tr? v?? mu th? hi?n tr?ng/ v? tr Pager, m?c ??nh
   *         Color.parseColor("#e0e1e2")
   */
  public int getColorState() {
    return mColorState;
  }

  /**
   * @return tr? v?? mu n??n c?a cc tr?ng thi khng th? hi?n v? tr Pager,
   *         m?c ??nh Color.parseColor("#e14b40")
   */
  public int getColorNormalState() {
    return mColorNormalState;
  }

  /**
   * 
   * @return tr?ng thi/v? tr th? hi?n c?a Pager
   */
  public int getState() {
    return mState;
  }

  /**
   * Ci ??t v? tr? th? hi?n tr?ng thi
   * 
   * @param k
   *            (0<=k <= getSize()-1
   */
  public void setState(int k) {
    mState = k;
    if (k < 0 && k >= mSize) {
      mState = 0;
    }

    invalidate();
  }

  /**
   * 
   * @return s? l??ng tr?ng thi / hay s? l??ng pages
   */
  public int getSize() {
    return mSize;
  }

  /**
   * Gn s? tr?ng thi khi kh?i t?o
   * 
   * @param k
   */
  public void setSize(int k) {
    mSize = k;
    if (k <= 0) {
      mSize = 1;
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(widthMeasureSpec, (int) (HEIGHT_SIZE * Resources
        .getSystem().getDisplayMetrics().density));
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    float left = getWidth() - (getPaddingLeft() + getPaddingRight());
    float top = getHeight() - (getPaddingTop() + getPaddingBottom());
    left = (left - (mSize * mWidthItem + (mSize - 1) * SPACE)) / 2;
    top = (top - mHeightItem) / 2;

    switch (mStype) {
    case RECT:
      drawRectSate(canvas, top, left);
      break;
    default:
      drawRoundSate(canvas, top, left);
      break;
    }
  }

  private void drawRoundSate(Canvas canvas, float top, float left) {
    float r = mWidthItem / 2;
    float cy = top + r;
    float cx = left + r;

    for (int i = 0; i < mSize; i++) {
      if (i == mState) {
        canvas.drawCircle(cx, cy, r, mPaintState);
      } else {
        canvas.drawCircle(cx, cy, r, mPaintNormalState);
      }
      cx += mWidthItem + SPACE;
    }
  }

  private void drawRectSate(Canvas canvas, float top, float left) {
    mRectF.left = left;
    mRectF.top = top;
    mRectF.right = left + mWidthItem;
    mRectF.bottom = top + mHeightItem;
    for (int i = 0; i < mSize; i++) {
      if (i == mState) {
        canvas.drawRoundRect(mRectF, OVAL, OVAL, mPaintState);
      } else {
        canvas.drawRoundRect(mRectF, OVAL, OVAL, mPaintNormalState);
      }
      left += mWidthItem + SPACE;
      mRectF.left = left;
      mRectF.right = left + mWidthItem;
    }
  }

}




Java Source Code List

com.ttpho.ptpagecontrols.PTPageControlsActivity.java
com.ttpho.ptpageview.PTPageView.java