Created
October 4, 2016 11:13
-
-
Save CMingTseng/8eaa84521872a2c66a58dc7d29499757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void DountChart() { | |
LinearLayout v = (LinearLayout)findViewById(R.id.v_dount); | |
v.getLayoutParams().width = mobileHeight/100*43; | |
v.getLayoutParams().height = mobileHeight/100*43; | |
ArrayList<Float> values = new ArrayList<Float>(); | |
values.add(36f); | |
values.add(12f); | |
values.add(12f); | |
values.add(36f); | |
values.add(48f); | |
ArrayList<Integer> colors = new ArrayList<>(); | |
colors.add(ContextCompat.getColor(this, R.color.Transparent)); | |
colors.add(ContextCompat.getColor(this, R.color.TodayColor)); | |
colors.add(ContextCompat.getColor(this, R.color.Transparent)); | |
colors.add(ContextCompat.getColor(this, R.color.TodayColor)); | |
colors.add(ContextCompat.getColor(this, R.color.Transparent)); | |
v.addView(new DountChartView(this, ((ColorDrawable) v.getBackground()).getColor(), calculateData(values), colors)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.RectF; | |
import android.view.View; | |
import java.util.ArrayList; | |
public class DountChartView extends View { | |
private ArrayList<Integer> colors = new ArrayList<>(); | |
private ArrayList<Float> value_degree = new ArrayList<>(); | |
private RectF rectf; | |
private final Paint paint = new Paint(1); | |
private final Paint paintCircle = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private float degree = 0.0F; | |
private float temp = 0.0F; | |
private int i = 0; | |
private int rectSize = 0; | |
private int backgroundColor = 0; | |
private int width = 0; | |
public DountChartView(Context context) { | |
super(context); | |
} | |
public DountChartView(Context context, int backgroundColor, ArrayList<Float> values, ArrayList<Integer> colors) { | |
super(context); | |
this.value_degree = values; | |
this.colors = colors; | |
this.temp = this.value_degree.get(0); | |
this.backgroundColor = backgroundColor; | |
} | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
if ((rectf == null) || (width != canvas.getWidth())) { | |
paintCircle.setStyle(Paint.Style.FILL); | |
paintCircle.setColor(Color.LTGRAY); | |
canvas.drawColor(backgroundColor); | |
width = canvas.getWidth(); | |
int height = canvas.getHeight(); | |
this.rectSize = ((int) (width * 0.85)); | |
int xPoint = width / 2 - rectSize / 2; | |
int yPoint = height / 2 - rectSize / 2; | |
if (width > height) { | |
width = height; | |
height = canvas.getWidth(); | |
this.rectSize = ((int) (width * 0.6)); | |
xPoint = height / 2 - rectSize / 2; | |
yPoint = width / 2 - rectSize / 2; | |
} | |
rectf = new RectF(xPoint, yPoint, rectSize + xPoint, rectSize + yPoint); | |
} | |
if (i == 0) { | |
paint.setColor(colors.get(i)); | |
canvas.drawArc(rectf, temp - value_degree.get(i), degree, true, paint); | |
} else { | |
paint.setColor(colors.get(i)); | |
canvas.drawArc(rectf, temp - value_degree.get(i), degree - (temp - value_degree.get(i)), true, paint); | |
} | |
for (int j = 0; j < i; j++) { | |
paint.setColor(colors.get(j)); | |
float v = 0; | |
for (int x = 0; x < j; x++) { | |
v += value_degree.get(x); | |
} | |
canvas.drawArc(rectf, v, value_degree.get(j), true, paint); | |
} | |
canvas.drawCircle(rectf.centerX(), rectf.centerY(), (float) (this.rectSize * 0.4), paintCircle); | |
degree = degree + 30; | |
if (degree >= temp) { | |
i = i + 1; | |
if (i >= value_degree.size()) { | |
i = i - 1; | |
} else { | |
temp += value_degree.get(i); | |
} | |
} | |
if ((degree <= 360)) { | |
invalidate(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment