/* Copyright (C) 2012 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.example.android.customviews.charting;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.*;
import android.os.Build;
import android.util.AttributeSet;
import android.view.*;
import android.widget.Scroller;
import com.example.android.customviews.R;
import java.lang.Math;
import java.lang.Override;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;
/**
* Custom view that shows a pie chart and, optionally, a label.
*/
public class PieChart extends ViewGroup {
private List<Item> mData = new ArrayList<Item>();
private float mTotal = 0.0f;
private RectF mPieBounds = new RectF();
private Paint mPiePaint;
private Paint mTextPaint;
private Paint mShadowPaint;
private boolean mShowText = false;
private float mTextX = 0.0f;
private float mTextY = 0.0f;
private float mTextWidth = 0.0f;
private float mTextHeight = 0.0f;
private int mTextPos = TEXTPOS_LEFT;
private float mHighlightStrength = 1.15f;
private float mPointerRadius = 2.0f;
private float mPointerX;
private float mPointerY;
private int mPieRotation;
private OnCurrentItemChangedListener mCurrentItemChangedListener = null;
private int mTextColor;
private PieView mPieView;
private Scroller mScroller;
private ValueAnimator mScrollAnimator;
private GestureDetector mDetector;
private PointerView mPointerView;
// The angle at which we measure the current item. This is
// where the pointer points.
private int mCurrentItemAngle;
// the index of the current item.
private int mCurrentItem = 0;
private boolean mAutoCenterInSlice;
private ObjectAnimator mAutoCenterAnimator;
private RectF mShadowBounds = new RectF();
/**
* Draw text to the left of the pie chart
*/
public static final int TEXTPOS_LEFT = 0;
/**
* Draw text to the right of the pie chart
*/
public static final int TEXTPOS_RIGHT = 1;
/**
* The initial fling velocity is divided by this amount.
*/
public static final int FLING_VELOCITY_DOWNSCALE = 4;
/**
*
*/
public static final int AUTOCENTER_ANIM_DURATION = 250;
/**
* Interface definition for a callback to be invoked when the current
* item changes.
*/
public interface OnCurrentItemChangedListener {
void OnCurrentItemChanged(PieChart source, int currentItem);
}
/**
* Class constructor taking only a context. Use this constructor to create
* {@link PieChart} objects from your own code.
*
* @param context
*/
public PieChart(Context context) {
super(context);
init();
}
/**
* Class constructor taking a context and an attribute set. This constructor
* is used by the layout engine to construct a {@link PieChart} from a set of
* XML attributes.
*
* @param context
* @param attrs An attribute set which can contain attributes from
* {@link com.example.android.customviews.R.styleable.PieChart} as well as attributes inherited
* from {@link android.view.View}.
*/
public PieChart(Context context, AttributeSet attrs) {
super(context, attrs);
// attrs contains the raw values for the XML attributes
// that were specified in the layout, which don't include
// attributes set by styles or themes, and which may have
// unresolved references. Call obtainStyledAttributes()
// to get the final values for each attribute.
//
// This call uses R.styleable.PieChart, which is an array of
// the custom attributes that were declared in attrs.xml.
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.PieChart,
0, 0
);
try {
// Retrieve the values from the TypedArray and store into
// fields of this class.
//
// The R.styleable.PieChart_* constants represent the index for
// each custom attribute in the R.styleable.PieChart array.
mShowText = a.getBoolean(R.styleable.PieChart_showText, false);
mTextY = a.getDimension(R.styleable.PieChart_labelY, 0.0f);
mTextWidth = a.getDimension(R.styleable.PieChart_labelWidth, 0.0f);
mTextHeight = a.getDimension(R.styleable.PieChart_labelHeight, 0.0f);
mTextPos = a.getInteger(R.styleable.PieChart_labelPosition, 0);
mTextColor = a.getColor(R.styleable.PieChart_labelColor, 0xff000000);
mHighlightStrength = a.getFloat(R.styleable.PieChart_highlightStrength, 1.0f);
mPieRotation = a.getInt(R.styleable.PieChart_pieRotation, 0);
mPointerRadius = a.getDimension(R.styleable.PieChart_pointerRadius, 2.0f);
mAutoCenterInSlice = a.getBoolean(R.styleable.PieChart_autoCenterPointerInSlice, false);
} finally {
// release the TypedArray so that it can be reused.
a.recycle();
}
init();
}
/**
* Returns true if the text label should be visible.
*
* @return True if the text label should be visible, false otherwise.
*/
public boolean getShowText() {
return mShowText;
}
/**
* Controls whether the text label is visible or not. Setting this property to
* false allows the pie chart graphic to take up the entire visible area of
* the control.
*
* @param showText true if the text label should be visible, false otherwise
*/
public void setShowText(boolean showText) {
mShowText = showText;
invalidate();
}
/**
* Returns the Y position of the label text, in pixels.
*
* @return The Y position of the label text, in pixels.
*/
public float getTextY() {
return mTextY;
}
/**
* Set the Y position of the label text, in pixels.
*
* @param textY the Y position of the label text, in pixels.
*/
public void setTextY(float textY) {
mTextY = textY;
invalidate();
}
/**
* Returns the width reserved for label text, in pixels.
*
* @return The width reserved for label text, in pixels.
*/
public float getTextWidth() {
return mTextWidth;
}
/**
* Set the width of the area reserved for label text. This width is constant; it does not
* change based on the actual width of the label as the label text changes.
*
* @param textWidth The width reserved for label text, in pixels.
*/
public void setTextWidth(float textWidth) {
mTextWidth = textWidth;
invalidate();
}
/**
* Returns the height of the label font, in pixels.
*
* @return The height of the label font, in pixels.
*/
public float getTextHeight() {
return mTextHeight;
}
/**
* Set the height of the label font, in pixels.
*
* @param textHeight The height of the label font, in pixels.
*/
public void setTextHeight(float textHeight) {
mTextH