HiveCell.java :  » Contact » fastcall-android » com » sfeir » fastcall » layout » Android Open Source

Android Open Source » Contact » fastcall android 
fastcall android » com » sfeir » fastcall » layout » HiveCell.java
package com.sfeir.fastcall.layout;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by :
 * 
 *     @author SFEIR
 */

public class HiveCell extends AbsoluteLayoutSfeir {

  protected Paint mTextPaint = new Paint();
  protected Paint mTextPaint2 = new Paint();
  private Paint mCellPaint = new Paint();
  private Paint mLinePaint = new Paint();
  private int size;
  private float halfHeight;
  private Callback callback;
  private float density = 0.0f;

  public HiveCell(Context context) {
    super(context);

    final DisplayMetrics dm = new DisplayMetrics();
    final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    display.getMetrics(dm);
    density = dm.density;
    
    mCellPaint.setAlpha(190);
    mCellPaint.setAntiAlias(true);
    mCellPaint.setColor(Color.rgb(255, 255, 255));
    mLinePaint.setAntiAlias(true);
    
    setSize((int) (63 * density));
    invalidate();
  }

  public void setTextColor(int color) {
    this.mTextPaint.setColor(color);
  }

  public void setCellColor(int color) {
    mCellPaint.setColor(color);
  }

  public int getSize() {
    return size;
  }

  public void setSize(int size) {
    this.size = (int) (size * density);
    halfHeight = ((float) Math.sqrt((double) (size * size - (size / 2) * (size / 2))));
  }

  public boolean checkPointInside(float x, float y) {

    if (y < (-(halfHeight * 2) / size) * x + halfHeight)
      return true;

    if (y > ((halfHeight * 2) / size) * x + halfHeight)
      return true;

    if (y > (-(halfHeight * 2) / size) * x + 5 * halfHeight)
      return true;

    if (y < ((halfHeight * 2) / size) * x - 3 * halfHeight)
      return true;

    if (y < 0)
      return true;

    if (y > (halfHeight * 2))
      return true;

    return false;
  }

  public Callback getCallback() {
    return callback;
  }

  public void setCallback(Callback callback) {
    this.callback = callback;
  }

  /**
   * Mthode onTouchEvent Permet lors d'un click sur les hive d'associer un
   * CallBack
   */
  @Override
  public boolean onTouchEvent(MotionEvent event) {

    if (checkPointInside(event.getX(), event.getY()))
      return false;

    switch (event.getAction()) {
    case MotionEvent.ACTION_UP:
      if (callback != null)
        callback.onClick(this);
      break;

    default:
      break;
    }
    return true;
  }

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

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension((int) (measureWidth(widthMeasureSpec)* density),(int) (measureHeight(heightMeasureSpec)* density));
  }

  private int measureWidth(int measureSpec) {
    return 2 * size;
  }

  private int measureHeight(int measureSpec) {
    int height = 2 * ((int) Math.sqrt((double) (size * size - (size / 2) * (size / 2))));
    return height;
  }

  public Paint getMCellPaint() {
    return mCellPaint;
  }

  public void setMCellPaint(Paint cellPaint) {
    mCellPaint = cellPaint;
  }

  public Paint getMTextPaint() {
    return mTextPaint;
  }

  public void setMTextPaint(Paint textPaint) {
    mTextPaint = textPaint;
  }

  public float getHalfHeight() {
    return halfHeight;
  }

  public void setHalfHeight(float halfHeight) {
    this.halfHeight = halfHeight;
  }

  public Paint getMLinePaint() {
    return mLinePaint;
  }

  public void setMLinePaint(Paint linePaint) {
    mLinePaint = linePaint;
  }
  
  public void setAlphaCell(int alpha) {
    mCellPaint.setAlpha(alpha);
    invalidate();
  }

  @Override
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // On recupere dernier fils
    final View child = getChildAt(getChildCount() - 1);
    // Nous creons le layout fils
    child.layout((int) (17 * density), (int) (10 * density),(int) (110 * density), (int) (100 * density));
    // Coloration test du layout fils
    child.setBackgroundColor(Color.TRANSPARENT);
  }

  @Override
  protected void dispatchDraw(Canvas canvas) {
    final RectF mBigOval = new RectF((int) (17 * density), (int) (5 * density), (int) (110 * density), (int) (105 * density));
    canvas.drawArc(mBigOval, 0, 360, false, mCellPaint);
    super.dispatchDraw(canvas);
  }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.