Skip to content

Update AndroidX #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions SlimChart/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28

defaultConfig {
minSdkVersion 14
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "0.1.0"
}
Expand All @@ -19,6 +18,6 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:27.1.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
}
7 changes: 5 additions & 2 deletions SlimChart/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mancj.slimchart">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mancj.slimchart">

<application android:allowBackup="true" android:label="@string/app_name"
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

</application>
Expand Down
110 changes: 63 additions & 47 deletions SlimChart/src/main/java/com/mancj/slimchart/SlimChart.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
Expand All @@ -21,8 +17,11 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;

import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.StringRes;
import androidx.core.content.ContextCompat;

public class SlimChart extends View {
private static final float DEFAULT_SIZE = 100;
Expand Down Expand Up @@ -55,7 +54,7 @@ public SlimChart(Context context, AttributeSet attrs, int defStyleAttr) {
init(attrs);
}

private void init(AttributeSet attrs){
private void init(AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SlimChart);
try {
this.density = getContext().getResources().getDisplayMetrics().density;
Expand All @@ -69,7 +68,7 @@ private void init(AttributeSet attrs){
this.color = typedArray.getColor(R.styleable.SlimChart_defaultColor, defColor);

chartRect = new RectF();
}finally {
} finally {
typedArray.recycle();
}
}
Expand All @@ -84,7 +83,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = 0;

switch (widthSpec){
switch (widthSpec) {
case MeasureSpec.AT_MOST:
width = defaultSize;
break;
Expand All @@ -93,7 +92,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
break;
}

switch (heightSpec){
switch (heightSpec) {
case MeasureSpec.AT_MOST:
height = defaultSize;
break;
Expand All @@ -105,13 +104,13 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int chartSize = Math.min(width, height);
int centerX = width / 2 - chartSize / 2;
int centerY = height / 2 - chartSize / 2;
int strokeHalf = strokeWidth/2;
int strokeHalf = strokeWidth / 2;

chartRect.set(centerX + strokeHalf, centerY + strokeHalf, centerX + chartSize - strokeHalf, centerY + chartSize - strokeHalf);
setMeasuredDimension(width, height);
}

public void playStartAnimation(){
public void playStartAnimation() {
float from = 0;
float to = 1;
ValueAnimator animator = ValueAnimator.ofFloat(from, to);
Expand All @@ -132,14 +131,14 @@ public void onAnimationUpdate(ValueAnimator animation) {
animator.start();
}

public void setStartAnimationDuration(int duration){
public void setStartAnimationDuration(int duration) {
this.animDuration = duration;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (stats != null){
if (stats != null) {
if (colors == null) colors = createColors();

if (colors.size() != stats.size()) {
Expand All @@ -150,17 +149,17 @@ protected void onDraw(Canvas canvas) {
for (int i = 0; i < stats.size(); i++) {
drawChart(canvas, colors.get(i), calculatePercents(stats.get(i)));
}
}else {
} else {
drawChart(canvas, color, calculatePercents(FULL_CIRCLE_ANGLE));
}
drawText(canvas);
}

private float calculatePercents(float degree){
private float calculatePercents(float degree) {
return degree * FULL_CIRCLE_ANGLE / maxStat;
}

private ArrayList<Integer> createColors(){
private ArrayList<Integer> createColors() {
ArrayList<Integer> colors = new ArrayList<>();
int chartsCount = stats.size();
float add = .9f / chartsCount;
Expand All @@ -174,38 +173,41 @@ private ArrayList<Integer> createColors(){
return colors;
}

private void drawText(Canvas canvas){
if (this.text == null){
private void drawText(Canvas canvas) {
if (this.text == null) {
return;
}
Paint paint = new Paint();
paint.setColor(textColor);
paint.setAntiAlias(true);
paint.setTextSize(chartRect.height()/3);
paint.setTextSize(chartRect.height() / 3);
paint.setTypeface(Typeface.DEFAULT_BOLD);
Rect textBounds = new Rect();
paint.getTextBounds(text, 0, text.length(), textBounds);
paint.measureText(text);
canvas.drawText(text, getWidth()/2-textBounds.right/2, getHeight()/2 + textBounds.height()/2, paint);
canvas.drawText(text, getWidth() / 2 - textBounds.right / 2, getHeight() / 2 + textBounds.height() / 2, paint);
}

private void drawChart(Canvas canvas, int color, float degree){
private void drawChart(Canvas canvas, int color, float degree) {
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(color);
paint.setStrokeWidth(strokeWidth);
paint.setAntiAlias(true);
if (roundEdges){
if (roundEdges) {
paint.setStrokeCap(Paint.Cap.ROUND);
}
canvas.drawArc(chartRect, 90, degree, false, paint);
}

public ArrayList<Float> getStats() {
return stats;
}

public void setStats(ArrayList<Float> stats) {
Collections.sort(stats, Collections.<Float>reverseOrder());
this.stats = stats;
if (stacked)
{
if (stacked) {
Float statCounter = 0f;
for (int i = this.stats.size() - 1; i >= 0; i--) {
Float statHolder = this.stats.get(i);
Expand All @@ -216,9 +218,6 @@ public void setStats(ArrayList<Float> stats) {
maxStat = stats.get(0); //First stat is the largest, save for arc calculations.
invalidate();
}
public ArrayList<Float> getStats() {
return stats;
}

public void setStatList(ArrayList<Stat> statList) {
Collections.sort(statList, new Comparator<Stat>() {
Expand All @@ -237,13 +236,11 @@ else if (t1 < t2)
});
stats = new ArrayList<>();
colors = new ArrayList<>();
for (Stat stat : statList)
{
for (Stat stat : statList) {
this.stats.add(stat.getValue());
this.colors.add(stat.getColor());
}
if (stacked)
{
if (stacked) {
Log.d("Stacked", "True");
Float statCounter = 0f;
for (int i = this.stats.size() - 1; i >= 0; i--) {
Expand All @@ -260,16 +257,21 @@ else if (t1 < t2)
public void setColor(ArrayList<Integer> colors) {
this.colors = colors;
}

public void setColorRes(@ColorRes int colorResId) {
setColor(ContextCompat.getColor(getContext(), colorResId));
}

public int getColor() {
return color;
}

public void setColor(@ColorInt int color) {
this.colors = null;
this.color = color;
invalidate();
}
public void setColorRes(@ColorRes int colorResId) { setColor(ContextCompat.getColor(getContext(), colorResId)); }

public int getColor() {
return color;
}
public ArrayList<Integer> getColors() {
return colors;
}
Expand All @@ -278,38 +280,52 @@ public void setText(String text) {
this.text = text;
invalidate();
}
public void setText(@StringRes int textIdRes){
setText(getContext().getString(textIdRes));
}

public String getText() {
return text;
}

public void setTextColor(@ColorInt int textColorInt) {
this.textColor = textColorInt;
invalidate();
public void setText(@StringRes int textIdRes) {
setText(getContext().getString(textIdRes));
}
public void setTextColorRes(@ColorRes int textColorResId){ setTextColor(ContextCompat.getColor(getContext(), textColorResId)); }

public void setTextColorRes(@ColorRes int textColorResId) {
setTextColor(ContextCompat.getColor(getContext(), textColorResId));
}

public int getTextColor() {
return textColor;
}

public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = (int) (strokeWidth * density);
public void setTextColor(@ColorInt int textColorInt) {
this.textColor = textColorInt;
invalidate();
}

public int getStrokeWidth() {
return strokeWidth;
}

public void setStrokeWidth(int strokeWidth) {
this.strokeWidth = (int) (strokeWidth * density);
invalidate();
}

public void setRoundEdges(boolean roundEdges) {
this.roundEdges = roundEdges;
invalidate();
}

public boolean isRoundEdgesEnabled() {
return roundEdges;
}

public void setStacked(boolean stacked) { this.stacked = stacked; invalidate(); }
public boolean isStacked() { return stacked; }
public boolean isStacked() {
return stacked;
}

public void setStacked(boolean stacked) {
this.stacked = stacked;
invalidate();
}
}
21 changes: 16 additions & 5 deletions SlimChart/src/main/java/com/mancj/slimchart/Stat.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,27 @@ public class Stat {
Float s_value;
Integer s_color;

public Stat() {}
public Stat() {
}

public Stat(Float value, Integer color) {
s_value = value;
s_color = color;
}

public Float getValue() { return s_value; }
public Integer getColor() { return s_color; }
public Float getValue() {
return s_value;
}

public void setValue(Float value) {
s_value = value;
}

public void setValue(Float value) { s_value = value; }
public void setColor(Integer color) { s_color = color; }
public Integer getColor() {
return s_color;
}

public void setColor(Integer color) {
s_color = color;
}
}
13 changes: 6 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
compileSdkVersion 28
defaultConfig {
applicationId "com.mancj.example"
minSdkVersion 14
targetSdkVersion 27
targetSdkVersion 28
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
Expand All @@ -20,9 +19,9 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(path: ':slimchart')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':slimchart')

compile 'com.android.support:appcompat-v7:27.1.0'
compile 'com.android.support:design:27.1.0'
implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
implementation 'com.google.android.material:material:1.0.0-rc01'
}
Loading