Home | History | Annotate | Download | only in ui
      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.ui;
     18 
     19 import android.graphics.RectF;
     20 import com.androidplot.util.PixelUtils;
     21 
     22 /**
     23  * Encapsulates sizing preferences associated with a Widget; how/if it scales etc.
     24  */
     25 public class SizeMetrics {
     26     private SizeMetric heightMetric;
     27     private SizeMetric widthMetric;
     28 
     29     /**
     30      * Convenience constructor.  Wraps {@link #SizeMetrics(SizeMetric, SizeMetric)}.
     31      * @param height Height value used algorithm to calculate the height of the associated widget(s).
     32      * @param heightLayoutType Algorithm used to calculate the height of the associated widget(s).
     33      * @param width Width value used algorithm to calculate the width of the associated widget(s).
     34      * @param widthLayoutType Algorithm used to calculate the width of the associated widget(s).
     35      */
     36     public SizeMetrics(float height, SizeLayoutType heightLayoutType, float width, SizeLayoutType widthLayoutType) {
     37         heightMetric = new SizeMetric(height, heightLayoutType);
     38         widthMetric = new SizeMetric(width, widthLayoutType);
     39     }
     40 
     41     /**
     42      * Creates a new SizeMetrics instance using the specified size layout algorithm and value.
     43      * See {@link SizeMetric} for details on what can be passed in.
     44      * @param heightMetric
     45      * @param widthMetric
     46      */
     47     public SizeMetrics(SizeMetric heightMetric, SizeMetric widthMetric) {
     48         this.heightMetric = heightMetric;
     49         this.widthMetric = widthMetric;
     50     }
     51 
     52     public SizeMetric getHeightMetric() {
     53         return heightMetric;
     54     }
     55 
     56     public void setHeightMetric(SizeMetric heightMetric) {
     57         this.heightMetric = heightMetric;
     58     }
     59 
     60     public SizeMetric getWidthMetric() {
     61         return widthMetric;
     62     }
     63 
     64     /**
     65      * Calculates a RectF with calculated width and height.  The top-left corner is set to 0,0.
     66      * @param canvasRect
     67      * @return
     68      */
     69     public RectF getRectF(RectF canvasRect) {
     70         return new RectF(
     71                 0,
     72                 0,
     73                 widthMetric.getPixelValue(canvasRect.width()),
     74                 heightMetric.getPixelValue(canvasRect.height()));
     75     }
     76 
     77     /**
     78      * Same as getRectF but with edges rounded to the nearest full pixel.
     79      * @param canvasRect
     80      * @return
     81      */
     82     public RectF getRoundedRect(RectF canvasRect) {
     83         return PixelUtils.nearestPixRect(0, 0, widthMetric.getPixelValue(canvasRect.width()),
     84                 heightMetric.getPixelValue(canvasRect.height()));
     85     }
     86 
     87     public void setWidthMetric(SizeMetric widthMetric) {
     88         this.widthMetric = widthMetric;
     89     }
     90 }
     91