Home | History | Annotate | Download | only in xy
      1 /*
      2  * Copyright 2012 AndroidPlot.com
      3  *
      4  *    Licensed under the Apache License, Version 2.0 (the "License");
      5  *    you may not use this file except in compliance with the License.
      6  *    You may obtain a copy of the License at
      7  *
      8  *        http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  *    Unless required by applicable law or agreed to in writing, software
     11  *    distributed under the License is distributed on an "AS IS" BASIS,
     12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  *    See the License for the specific language governing permissions and
     14  *    limitations under the License.
     15  */
     16 
     17 package com.androidplot.xy;
     18 
     19 import android.graphics.Canvas;
     20 import android.graphics.Color;
     21 import android.graphics.Paint;
     22 import android.graphics.PointF;
     23 import com.androidplot.util.PixelUtils;
     24 
     25 public class PointLabelFormatter {
     26     private static final float DEFAULT_H_OFFSET_DP = 0;
     27     private static final float DEFAULT_V_OFFSET_DP = -4;
     28     private static final float DEFAULT_TEXT_SIZE_SP = 12;
     29     private Paint textPaint;
     30     public float hOffset;
     31     public float vOffset;
     32 
     33     public PointLabelFormatter() {
     34         this(Color.WHITE);
     35     }
     36 
     37     public PointLabelFormatter(int textColor) {
     38         this(textColor, PixelUtils.dpToPix(DEFAULT_H_OFFSET_DP),
     39                 PixelUtils.dpToPix(DEFAULT_V_OFFSET_DP));
     40     }
     41 
     42     /**
     43      *
     44      * @param textColor
     45      * @param hOffset Horizontal offset of text in pixels.
     46      * @param vOffset Vertical offset of text in pixels.  Offset is in screen coordinates;
     47      *                positive values shift the text further down the screen.
     48      */
     49     public PointLabelFormatter(int textColor, float hOffset, float vOffset) {
     50         initTextPaint(textColor);
     51         this.hOffset = hOffset;
     52         this.vOffset = vOffset;
     53     }
     54 
     55     public Paint getTextPaint() {
     56         return textPaint;
     57     }
     58 
     59     public void setTextPaint(Paint textPaint) {
     60         this.textPaint = textPaint;
     61     }
     62 
     63     protected void initTextPaint(Integer textColor) {
     64         if (textColor == null) {
     65             setTextPaint(null);
     66         } else {
     67             setTextPaint(new Paint());
     68             getTextPaint().setAntiAlias(true);
     69             getTextPaint().setColor(textColor);
     70             getTextPaint().setTextAlign(Paint.Align.CENTER);
     71             getTextPaint().setTextSize(PixelUtils.spToPix(DEFAULT_TEXT_SIZE_SP));
     72             //textPaint.setStyle(Paint.Style.STROKE);
     73         }
     74     }
     75 }
     76