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 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 180 && angel%360 180 && angel%360 =pieHelper.getStartDegree() && diploma

You May Also Like

Calculation Number of Days Between Two Dates

Topic: Delphi Language charlene44 wrote: 18/08/2006 at 15h08 calculation of number of…

World of Warcraft Classic: Some Players Use Illegal Tools to Stay Connected

Earlier this week, Blizzard released World of Warcraft Classic. The launch was,…

Crash Team Racing Bientt Back on Ps4!

Activision seems not to have finished with the remastering of the Playstation…

Top Benefits of Social Media Marketing

We cannot deny the fact that when it comes to marketing, social…