Draw Android pie Chart With Animation.

Draw Android pie Chart With Animation.

—>  AndroidManifest.xml




IN Format Folder

activity_main.xml

MyUtils.java

bundle com.instance.barchart;

import android.content material Context;

public class MyUtils {

public static int dip2px(Context context, float dipValue){
closing float scale = context.getResources().getDisplayMetrics().density;
return (int)(dipValue * scale + 0.5f);
}

public static int px2dip(Context context, float pxValue){
closing float scale = context.getResources().getDisplayMetrics().density;
return (int)(pxValue / scale + 0.5f);
}

public static int sp2px(Context context, float spValue) {
closing float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}

}

MainActivity.java

bundle com.instance.androidpiechart;

import java.util.ArrayList;

import android.app.Exercise;
import android.graphics.Shade;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Exercise {
non-public TextView textView;
PieView pieView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
tremendous.onCreate(savedInstanceState);
setContentView(R.structure.activity_main);
textView = (TextView)findViewById(R.id.textView);
pieView = (PieView)findViewById(R.id.pie_view);
button = (Button)findViewById(R.id.pie_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
randomSet(pieView);
}
});
set(pieView);
}
non-public void randomSet(PieView pieView){
ArrayList pieHelperArrayList = new ArrayList();
ArrayList intList = new ArrayList();
int totalNum = (int) (5*Math.random()) + 5;

int totalInt = 0;
for(int i=0; i pieHelperArrayList = new ArrayList();
pieHelperArrayList.add(new PieHelper(20, Shade.BLACK));
pieHelperArrayList.add(new PieHelper(6));
pieHelperArrayList.add(new PieHelper(30));
pieHelperArrayList.add(new PieHelper(12));
pieHelperArrayList.add(new PieHelper(32));

pieView.setDate(pieHelperArrayList);
pieView.setOnPieClickListener(new PieView.OnPieClickListener() {
@Override
public void onPieClick(int index) {
if(index != PieView.NO_SELECTED_INDEX) {
textView.setText(index + ” chosen”);
}else{
textView.setText(“No selected pie”);
}
}
});
pieView.selectedPie(2);
}

}

PieHelper.java

bundle com.instance.androidpiechart;

public class PieHelper {

non-public float startDegree;
non-public float endDegree;
non-public float targetStartDegree;
non-public float targetEndDegree;
non-public String title;
non-public int colour;
non-public float sweepDegree;

int velocity = 5;

public PieHelper(float %){
this(%, null, 0);
}

public PieHelper(float %, int colour){
this(%, null, colour);
}

PieHelper(float %, String title){
this(%, title, 0);
}

PieHelper(float %, String title, int colour){
this.sweepDegree = % * 360 / 100;
this.title = title;
this.colour = colour;
}

PieHelper(float startDegree, float endDegree, PieHelper targetPie){
this.startDegree = startDegree;
this.endDegree = endDegree;
targetStartDegree = targetPie.getStartDegree();
targetEndDegree = targetPie.getEndDegree();
this.sweepDegree = targetPie.getSweep();
this.title = targetPie.getTitle();
this.colour = targetPie.getColor();
}

PieHelper setTarget(PieHelper targetPie){
this.targetStartDegree = targetPie.getStartDegree();
this.targetEndDegree = targetPie.getEndDegree();
this.title = targetPie.getTitle();
this.colour = targetPie.getColor();
this.sweepDegree = targetPie.getSweep();
return this;
}

void setDegree(float startDegree, float endDegree){
this.startDegree = startDegree;
this.endDegree = endDegree;
}

boolean isColorSetted(){return colour != 0;}

boolean isAtRest(){
return (startDegree==targetStartDegree)&&(endDegree==targetEndDegree);
}

void replace(){
this.startDegree = updateSelf(startDegree, targetStartDegree, velocity);
this.endDegree = updateSelf(endDegree, targetEndDegree, velocity);
this.sweepDegree = endDegree – startDegree;
}

String getPercentStr(){
float % = sweepDegree / 360 * 100;
return String.valueOf((int)%) + “%”;
}

public int getColor(){ return colour; }

public String getTitle(){
return title;
}

public float getSweep(){
return sweepDegree;
}

public float getStartDegree(){
return startDegree;
}

public float getEndDegree(){
return endDegree;
}

non-public float updateSelf(float origin, float goal, int velocity){
if (origin < target) { origin += velocity; } else if (origin > goal){
origin-= velocity;
}
if(Math.abs(target-origin)

PieView.java

bundle com.instance.androidpiechart;

import android.content material Context;
import android.graphics.Canvas;
import android.graphics.Shade;
import android.graphics.Paint;
import android.graphics.Level;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class PieView extends View {

public interface OnPieClickListener{
void onPieClick(int index);
}

non-public Paint cirPaint;
non-public Paint whiteLinePaint;
non-public Level pieCenterPoint;
non-public Paint textPaint;
non-public RectF cirRect;
non-public RectF cirSelectedRect;

non-public int mViewWidth;
non-public int mViewHeight;
non-public int margin;
non-public int pieRadius;

non-public OnPieClickListener onPieClickListener;

non-public ArrayList pieHelperList;
non-public int selectedIndex = NO_SELECTED_INDEX;

non-public boolean showPercentLabel = true;
public static closing int NO_SELECTED_INDEX = -999;
non-public closing int[] DEFAULT_COLOR_LIST = {Shade.parseColor(“#33B5E5”),
Shade.parseColor(“#AA66CC”),
Shade.parseColor(“#99CC00”),
Shade.parseColor(“#FFBB33”),
Shade.parseColor(“#FF4444”)};

non-public Runnable animator = new Runnable() {
@Override
public void run() {
boolean needNewFrame = false;
for(PieHelper pie : pieHelperList){
pie.replace();
if(!pie.isAtRest()){
needNewFrame = true;
}
}
if (needNewFrame) {
postDelayed(this, 10);
}
invalidate();
}
};

public PieView(Context context){
this(context,null);
}
public PieView(Context context, AttributeSet attrs){
tremendous(context, attrs);

pieHelperList = new ArrayList();
cirPaint = new Paint();
cirPaint.setAntiAlias(true);
cirPaint.setColor(Shade.GRAY);
whiteLinePaint = new Paint(cirPaint);
whiteLinePaint.setColor(Shade.WHITE);
whiteLinePaint.setStrokeWidth(2f);
textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setColor(Shade.WHITE);
textPaint.setTextSize(MyUtils.sp2px(getContext(), 13));
textPaint.setStrokeWidth(5);
textPaint.setTextAlign(Paint.Align.CENTER);
pieCenterPoint = new Level();
cirRect = new RectF();
cirSelectedRect = new RectF();
}

public void showPercentLabel(boolean present){
showPercentLabel = present;
postInvalidate();
}

public void setOnPieClickListener(OnPieClickListener listener){
onPieClickListener = listener;
}

public void setDate(ArrayList helperList){
initPies(helperList);
pieHelperList.clear();
removeSelectedPie();

if(helperList != null && !helperList.isEmpty()){
for(PieHelper pieHelper:helperList){
pieHelperList.add(new PieHelper(pieHelper.getStartDegree(),pieHelper.getStartDegree(),pieHelper));
}
}else {
pieHelperList.clear();
}

removeCallbacks(animator);
publish(animator);

}

non-public void initPies(ArrayList helperList){
float totalAngel = 270;
for(PieHelper pie:helperList){
pie.setDegree(totalAngel, totalAngel + pie.getSweep());
totalAngel += pie.getSweep();
}
}

public void selectedPie(int index){
selectedIndex = index;
if(onPieClickListener!=null) onPieClickListener.onPieClick(index);
postInvalidate();
}

public void removeSelectedPie(){
selectedIndex = NO_SELECTED_INDEX;
if(onPieClickListener!=null) onPieClickListener.onPieClick(NO_SELECTED_INDEX);
postInvalidate();
}

@Override
protected void onDraw(Canvas canvas) {
if(pieHelperList.isEmpty()){
return ;
}

int index = 0;
for(PieHelper pieHelper : pieHelperList){
boolean chosen = (selectedIndex == index);
RectF rect = chosen? cirSelectedRect: cirRect;
if(pieHelper.isColorSetted()){
cirPaint.setColor(pieHelper.getColor());
}else {
cirPaint.setColor(DEFAULT_COLOR_LIST[index%5]);
}
canvas.drawArc(rect, pieHelper.getStartDegree(), pieHelper.getSweep(), true, cirPaint);
drawPercentText(canvas, pieHelper);

drawLineBesideCir(canvas, pieHelper.getStartDegree(), chosen);
drawLineBesideCir(canvas, pieHelper.getEndDegree(), chosen);
index++;
}

}

non-public void drawLineBesideCir(Canvas canvas, float angel, boolean selectedCir){
int sth2 = selectedCir? mViewHeight/2 : pieRadius; // Sorry I’m actually don’t know easy methods to title the variable..
int sth = 1;                                       // And it’s
if(angel%360 > 180 && angel%360 < 360){ sth = -1; } float lineToX = (float)(mViewHeight/2 + Math.cos(Math.toRadians(-angel)) * sth2); float lineToY = (float)(mViewHeight/2 + sth * Math.abs(Math.sin(Math.toRadians(-angel))) * sth2); canvas.drawLine(pieCenterPoint.x, pieCenterPoint.y, lineToX, lineToY, whiteLinePaint); } private void drawPercentText(Canvas canvas, PieHelper pieHelper){ if(!showPercentLabel) return ; float angel = (pieHelper.getStartDegree() + pieHelper.getEndDegree()) /2; int sth = 1; if(angel%360 > 180 && angel%360 < 360){ sth = -1; } float x = (float)(mViewHeight/2 + Math.cos(Math.toRadians(-angel)) * pieRadius/2); float y = (float)(mViewHeight/2 + sth * Math.abs(Math.sin(Math.toRadians(-angel))) * pieRadius/2); canvas.drawText(pieHelper.getPercentStr(), x, y, textPaint); } private void drawText(Canvas canvas, PieHelper pieHelper){ if(pieHelper.getTitle() == null) return ; float angel = (pieHelper.getStartDegree() + pieHelper.getEndDegree()) /2; int sth = 1; if(angel%360 > 180 && angel%360 < 360){ sth = -1; } float x = (float)(mViewHeight/2 + Math.cos(Math.toRadians(-angel)) * pieRadius/2); float y = (float)(mViewHeight/2 + sth * Math.abs(Math.sin(Math.toRadians(-angel))) * pieRadius/2); canvas.drawText(pieHelper.getTitle(), x, y, textPaint); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN ||event.getAction() == MotionEvent.ACTION_MOVE){ selectedIndex = findPointAt((int) event.getX(), (int) event.getY()); if(onPieClickListener != null){ onPieClickListener.onPieClick(selectedIndex); } postInvalidate(); } return true; } private int findPointAt(int x, int y){ double degree = Math.atan2(x-pieCenterPoint.x, y-pieCenterPoint.y)* 180 / Math.PI; degree = -(degree-180) + 270; int index = 0; for(PieHelper pieHelper:pieHelperList){ if(degree>=pieHelper.getStartDegree() && diploma<=pieHelper.getEndDegree()){ return index; } index++; } return NO_SELECTED_INDEX; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { mViewWidth = measureWidth(widthMeasureSpec); mViewHeight = measureHeight(heightMeasureSpec); margin = mViewWidth/16; pieRadius = (mViewWidth)/2-margin; pieCenterPoint.set(pieRadius+margin, pieRadius+margin); cirRect.set(pieCenterPoint.x-pieRadius, pieCenterPoint.y-pieRadius, pieCenterPoint.x+pieRadius, pieCenterPoint.y+pieRadius); cirSelectedRect.set(2, //minor margin for larger circle 2, mViewWidth-2, mViewHeight-2); setMeasuredDimension(mViewWidth, mViewHeight); } non-public int measureWidth(int measureSpec){ int most well-liked = 3; return getMeasurement(measureSpec, most well-liked); } non-public int measureHeight(int measureSpec){ int most well-liked = mViewWidth; return getMeasurement(measureSpec, most well-liked); } non-public int getMeasurement(int measureSpec, int most well-liked){ int specSize = View.MeasureSpec.getSize(measureSpec); int measurement; swap(View.MeasureSpec.getMode(measureSpec)){ case View.MeasureSpec.EXACTLY: measurement = specSize; break; case View.MeasureSpec.AT_MOST: measurement = Math.min(most well-liked, specSize); break; default: measurement = most well-liked; break; } return measurement; } }

You May Also Like