From 99ea73999952a34a7adf5c0e86513320be21bb0c Mon Sep 17 00:00:00 2001 From: Dacer Date: Fri, 25 Aug 2017 15:40:52 +0800 Subject: [PATCH] Reformat Code --- AndroidCharts/src/main/AndroidManifest.xml | 2 +- .../java/im/dacer/androidcharts/BarView.java | 161 +++--- .../dacer/androidcharts/ClockPieHelper.java | 52 +- .../im/dacer/androidcharts/ClockPieView.java | 120 ++-- .../java/im/dacer/androidcharts/LineView.java | 524 +++++++++--------- .../java/im/dacer/androidcharts/MyUtils.java | 10 +- .../im/dacer/androidcharts/PieHelper.java | 59 +- .../java/im/dacer/androidcharts/PieView.java | 176 +++--- .../java/im/dacer/androidcharts/TempLog.java | 15 +- .../src/main/AndroidManifest.xml | 14 +- .../androidchartsexample/BarFragment.java | 29 +- .../ClockPieFragment.java | 32 +- .../androidchartsexample/LineFragment.java | 68 ++- .../androidchartsexample/MainActivity.java | 33 +- .../NavigationDrawerFragment.java | 131 ++--- .../androidchartsexample/PieFragment.java | 35 +- .../src/main/res/layout/activity_main.xml | 12 +- .../src/main/res/layout/fragment_bar.xml | 26 +- .../src/main/res/layout/fragment_line.xml | 30 +- .../res/layout/fragment_navigation_drawer.xml | 5 +- .../src/main/res/layout/fragment_pie.xml | 20 +- .../src/main/res/layout/fragment_pie_s.xml | 27 +- .../src/main/res/values/dimens.xml | 4 +- 23 files changed, 763 insertions(+), 822 deletions(-) diff --git a/AndroidCharts/src/main/AndroidManifest.xml b/AndroidCharts/src/main/AndroidManifest.xml index 9d8c7f4..c5756a3 100644 --- a/AndroidCharts/src/main/AndroidManifest.xml +++ b/AndroidCharts/src/main/AndroidManifest.xml @@ -1,4 +1,4 @@ - diff --git a/AndroidCharts/src/main/java/im/dacer/androidcharts/BarView.java b/AndroidCharts/src/main/java/im/dacer/androidcharts/BarView.java index 184dab8..d4aba10 100644 --- a/AndroidCharts/src/main/java/im/dacer/androidcharts/BarView.java +++ b/AndroidCharts/src/main/java/im/dacer/androidcharts/BarView.java @@ -7,13 +7,18 @@ import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; - import java.util.ArrayList; /** * Created by Dacer on 11/11/13. */ public class BarView extends View { + private final int MINI_BAR_WIDTH; + private final int BAR_SIDE_MARGIN; + private final int TEXT_TOP_MARGIN; + private final int TEXT_COLOR = Color.parseColor("#9B9A9B"); + private final int BACKGROUND_COLOR = Color.parseColor("#F6F6F6"); + private final int FOREGROUND_COLOR = Color.parseColor("#FC496D"); private ArrayList percentList; private ArrayList targetPercentList; private Paint textPaint; @@ -21,46 +26,38 @@ public class BarView extends View { private Paint fgPaint; private Rect rect; private int barWidth; -// private boolean showSideMargin = true; private int bottomTextDescent; private boolean autoSetWidth = true; private int topMargin; private int bottomTextHeight; private ArrayList bottomTextList = new ArrayList(); - private final int MINI_BAR_WIDTH; - private final int BAR_SIDE_MARGIN; - private final int TEXT_TOP_MARGIN; - private final int TEXT_COLOR = Color.parseColor("#9B9A9B"); - private final int BACKGROUND_COLOR = Color.parseColor("#F6F6F6"); - private final int FOREGROUND_COLOR = Color.parseColor("#FC496D"); - private Runnable animator = new Runnable() { - @Override - public void run() { - boolean needNewFrame = false; - for (int i=0; i targetPercentList.get(i)){ - percentList.set(i,percentList.get(i)-0.02f); - needNewFrame = true; - } - if(Math.abs(targetPercentList.get(i)-percentList.get(i))<0.02f){ - percentList.set(i,targetPercentList.get(i)); - } + @Override public void run() { + boolean needNewFrame = false; + for (int i = 0; i < targetPercentList.size(); i++) { + if (percentList.get(i) < targetPercentList.get(i)) { + percentList.set(i, percentList.get(i) + 0.02f); + needNewFrame = true; + } else if (percentList.get(i) > targetPercentList.get(i)) { + percentList.set(i, percentList.get(i) - 0.02f); + needNewFrame = true; } - if (needNewFrame) { - postDelayed(this, 20); + if (Math.abs(targetPercentList.get(i) - percentList.get(i)) < 0.02f) { + percentList.set(i, targetPercentList.get(i)); } - invalidate(); + } + if (needNewFrame) { + postDelayed(this, 20); + } + invalidate(); } }; - public BarView(Context context){ - this(context,null); + public BarView(Context context) { + this(context, null); } - public BarView(Context context, AttributeSet attrs){ + + public BarView(Context context, AttributeSet attrs) { super(context, attrs); bgPaint = new Paint(); bgPaint.setAntiAlias(true); @@ -70,9 +67,9 @@ public BarView(Context context, AttributeSet attrs){ rect = new Rect(); topMargin = MyUtils.dip2px(context, 5); int textSize = MyUtils.sp2px(context, 15); - barWidth = MyUtils.dip2px(context,22); - MINI_BAR_WIDTH = MyUtils.dip2px(context,22); - BAR_SIDE_MARGIN = MyUtils.dip2px(context,22); + barWidth = MyUtils.dip2px(context, 22); + MINI_BAR_WIDTH = MyUtils.dip2px(context, 22); + BAR_SIDE_MARGIN = MyUtils.dip2px(context, 22); TEXT_TOP_MARGIN = MyUtils.dip2px(context, 5); textPaint = new Paint(); textPaint.setAntiAlias(true); @@ -84,23 +81,24 @@ public BarView(Context context, AttributeSet attrs){ /** * dataList will be reset when called is method. + * * @param bottomStringList The String ArrayList in the bottom. */ - public void setBottomTextList(ArrayList bottomStringList){ -// this.dataList = null; + public void setBottomTextList(ArrayList bottomStringList) { + // this.dataList = null; this.bottomTextList = bottomStringList; Rect r = new Rect(); bottomTextDescent = 0; barWidth = MINI_BAR_WIDTH; - for(String s:bottomTextList){ - textPaint.getTextBounds(s,0,s.length(),r); - if(bottomTextHeight bottomStringList){ } /** - * * @param list The ArrayList of Integer with the range of [0-max]. */ - public void setDataList(ArrayList list, int max){ + public void setDataList(ArrayList list, int max) { targetPercentList = new ArrayList(); - if(max == 0) max = 1; + if (max == 0) max = 1; - for(Integer integer : list){ - targetPercentList.add(1-(float)integer/(float)max); + for (Integer integer : list) { + targetPercentList.add(1 - (float) integer / (float) max); } // Make sure percentList.size() == targetPercentList.size() - if(percentList.isEmpty() || percentList.size()targetPercentList.size()){ - int temp = percentList.size()-targetPercentList.size(); - for(int i=0; i targetPercentList.size()) { + int temp = percentList.size() - targetPercentList.size(); + for (int i = 0; i < temp; i++) { + percentList.remove(percentList.size() - 1); } } setMinimumWidth(2); @@ -137,67 +134,66 @@ public void setDataList(ArrayList list, int max){ post(animator); } - @Override - protected void onDraw(Canvas canvas) { + @Override protected void onDraw(Canvas canvas) { int i = 1; - if(percentList != null && !percentList.isEmpty()){ - for(Float f:percentList){ - rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1), - topMargin, - (BAR_SIDE_MARGIN+barWidth)* i, - getHeight()-bottomTextHeight-TEXT_TOP_MARGIN); - canvas.drawRect(rect,bgPaint); + if (percentList != null && !percentList.isEmpty()) { + for (Float f : percentList) { + rect.set(BAR_SIDE_MARGIN * i + barWidth * (i - 1), topMargin, + (BAR_SIDE_MARGIN + barWidth) * i, + getHeight() - bottomTextHeight - TEXT_TOP_MARGIN); + canvas.drawRect(rect, bgPaint); /*rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1), topMargin+(int)((getHeight()-topMargin)*percentList.get(i-1)), (BAR_SIDE_MARGIN+barWidth)* i, getHeight()-bottomTextHeight-TEXT_TOP_MARGIN);*/ - /** - * The correct total height is "getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN",not "getHeight()-topMargin". - * fix by zhenghuiy@gmail.com on 11/11/13. - */ - rect.set(BAR_SIDE_MARGIN*i+barWidth*(i-1), - topMargin+(int)((getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN)*percentList.get(i-1)), - (BAR_SIDE_MARGIN+barWidth)* i, - getHeight()-bottomTextHeight-TEXT_TOP_MARGIN); - canvas.drawRect(rect,fgPaint); + /** + * The correct total height is "getHeight()-topMargin-bottomTextHeight-TEXT_TOP_MARGIN",not "getHeight()-topMargin". + * fix by zhenghuiy@gmail.com on 11/11/13. + */ + rect.set(BAR_SIDE_MARGIN * i + barWidth * (i - 1), topMargin + (int) ((getHeight() + - topMargin + - bottomTextHeight + - TEXT_TOP_MARGIN) * percentList.get(i - 1)), + (BAR_SIDE_MARGIN + barWidth) * i, + getHeight() - bottomTextHeight - TEXT_TOP_MARGIN); + canvas.drawRect(rect, fgPaint); i++; } } - if(bottomTextList != null && !bottomTextList.isEmpty()){ + if (bottomTextList != null && !bottomTextList.isEmpty()) { i = 1; - for(String s:bottomTextList){ - canvas.drawText(s,BAR_SIDE_MARGIN*i+barWidth*(i-1)+barWidth/2, - getHeight()-bottomTextDescent,textPaint); + for (String s : bottomTextList) { + canvas.drawText(s, BAR_SIDE_MARGIN * i + barWidth * (i - 1) + barWidth / 2, + getHeight() - bottomTextDescent, textPaint); i++; } } } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int mViewWidth = measureWidth(widthMeasureSpec); int mViewHeight = measureHeight(heightMeasureSpec); - setMeasuredDimension(mViewWidth,mViewHeight); + setMeasuredDimension(mViewWidth, mViewHeight); } - private int measureWidth(int measureSpec){ + private int measureWidth(int measureSpec) { int preferred = 0; - if(bottomTextList != null){ - preferred = bottomTextList.size()*(barWidth+BAR_SIDE_MARGIN); + if (bottomTextList != null) { + preferred = bottomTextList.size() * (barWidth + BAR_SIDE_MARGIN); } return getMeasurement(measureSpec, preferred); } - private int measureHeight(int measureSpec){ + private int measureHeight(int measureSpec) { int preferred = 222; return getMeasurement(measureSpec, preferred); } - private int getMeasurement(int measureSpec, int preferred){ + private int getMeasurement(int measureSpec, int preferred) { int specSize = MeasureSpec.getSize(measureSpec); int measurement; - switch(MeasureSpec.getMode(measureSpec)){ + switch (MeasureSpec.getMode(measureSpec)) { case MeasureSpec.EXACTLY: measurement = specSize; break; @@ -210,5 +206,4 @@ private int getMeasurement(int measureSpec, int preferred){ } return measurement; } - } diff --git a/AndroidCharts/src/main/java/im/dacer/androidcharts/ClockPieHelper.java b/AndroidCharts/src/main/java/im/dacer/androidcharts/ClockPieHelper.java index 234ed82..0ee2d5d 100644 --- a/AndroidCharts/src/main/java/im/dacer/androidcharts/ClockPieHelper.java +++ b/AndroidCharts/src/main/java/im/dacer/androidcharts/ClockPieHelper.java @@ -1,80 +1,80 @@ package im.dacer.androidcharts; - /** * Created by Dacer on 11/14/13. */ public class ClockPieHelper { + int velocity = 5; private float start; private float end; private float targetStart; private float targetEnd; - int velocity = 5; - ClockPieHelper(float startDegree, float endDegree, ClockPieHelper targetPie){ + ClockPieHelper(float startDegree, float endDegree, ClockPieHelper targetPie) { start = startDegree; end = endDegree; targetStart = targetPie.getStart(); targetEnd = targetPie.getEnd(); } - public ClockPieHelper(int startHour, int startMin, int endHour, int endMin){ - start = 270+startHour*15+startMin*15/60; - end = 270+endHour*15+endMin*15/60; - while(end target){ - origin-= velocity; + } else if (origin > target) { + origin -= velocity; } - if(Math.abs(target-origin) pieArrayList = new ArrayList(); - - private final int TEXT_COLOR = Color.parseColor("#9B9A9B"); - private final int GRAY_COLOR = Color.parseColor("#D4D3D4"); - private final int RED_COLOR = Color.argb(50, 255, 0, 51); - private Runnable animator = new Runnable() { - @Override - public void run() { + @Override public void run() { boolean needNewFrame = false; - for(ClockPieHelper pie : pieArrayList){ + for (ClockPieHelper pie : pieArrayList) { pie.update(); - if(!pie.isAtRest()){ + if (!pie.isAtRest()) { needNewFrame = true; } } @@ -60,10 +54,11 @@ public void run() { } }; - public ClockPieView(Context context){ - this(context,null); + public ClockPieView(Context context) { + this(context, null); } - public ClockPieView(Context context, AttributeSet attrs){ + + public ClockPieView(Context context, AttributeSet attrs) { super(context, attrs); textSize = MyUtils.sp2px(context, 15); lineThickness = MyUtils.dip2px(context, 1); @@ -77,7 +72,7 @@ public ClockPieView(Context context, AttributeSet attrs){ Paint.FontMetrics fm = new Paint.FontMetrics(); textPaint.getFontMetrics(fm); textRect = new Rect(); - textPaint.getTextBounds("18",0,1,textRect); + textPaint.getTextBounds("18", 0, 1, textRect); redPaint = new Paint(textPaint); redPaint.setColor(RED_COLOR); linePaint = new Paint(textPaint); @@ -94,88 +89,89 @@ public ClockPieView(Context context, AttributeSet attrs){ topTextHeight = textRect.height(); } - public void setDate(ArrayList helperList){ - if(helperList != null && !helperList.isEmpty()){ - int pieSize = pieArrayList.isEmpty()? 0:pieArrayList.size(); - for(int i=0;ipieSize-1){ -// float mStart = helperList.get(i).getStart(); - pieArrayList.add(new ClockPieHelper(0,0,helperList.get(i))); - }else{ + public void setDate(ArrayList helperList) { + if (helperList != null && !helperList.isEmpty()) { + int pieSize = pieArrayList.isEmpty() ? 0 : pieArrayList.size(); + for (int i = 0; i < helperList.size(); i++) { + if (i > pieSize - 1) { + // float mStart = helperList.get(i).getStart(); + pieArrayList.add(new ClockPieHelper(0, 0, helperList.get(i))); + } else { pieArrayList.set(i, pieArrayList.get(i).setTarget(helperList.get(i))); } } int temp = pieArrayList.size() - helperList.size(); - for(int i=0; i bottomTextList = new ArrayList(); - private ArrayList> dataLists; - private ArrayList xCoordinateList = new ArrayList(); private ArrayList yCoordinateList = new ArrayList(); - private ArrayList> drawDotLists = new ArrayList>(); - private Paint bottomTextPaint = new Paint(); private int bottomTextDescent; - - //popup private Paint popupTextPaint = new Paint(); - private final int bottomTriangleHeight = 12; - public boolean showPopup = true; private boolean showFloatNumInPopup; - - private Dot pointToSelect; - private Dot selectedDot; + private Dot pointToSelect; + private Dot selectedDot; private int popupBottomPadding = MyUtils.dip2px(getContext(), 2); + /* + | | ←topLineLength + --+--+--+--+--+--+-- + --+--+--+--+--+--+-- + ↑sideLineLength + */ + private int topLineLength = MyUtils.dip2px(getContext(), 12); + private int sideLineLength = MyUtils.dip2px(getContext(), 45) / 3 * 2; - private int topLineLength = MyUtils.dip2px(getContext(), 12);; // | | ←this - //-+-+- - private int sideLineLength = MyUtils.dip2px(getContext(),45)/3*2;// --+--+--+--+--+--+-- - // ↑this - private int backgroundGridWidth = MyUtils.dip2px(getContext(),45); - - //Constants - private final int popupTopPadding = MyUtils.dip2px(getContext(),2); - private final int popupBottomMargin = MyUtils.dip2px(getContext(),5); - private final int bottomTextTopMargin = MyUtils.sp2px(getContext(),5); - private final int bottomLineLength = MyUtils.sp2px(getContext(), 22); - private final int DOT_INNER_CIR_RADIUS = MyUtils.dip2px(getContext(), 2); - private final int DOT_OUTER_CIR_RADIUS = MyUtils.dip2px(getContext(),5); - private final int MIN_TOP_LINE_LENGTH = MyUtils.dip2px(getContext(),12); - private final int MIN_VERTICAL_GRID_NUM = 4; - private final int MIN_HORIZONTAL_GRID_NUM = 1; - private final int BACKGROUND_LINE_COLOR = Color.parseColor("#EEEEEE"); - private final int BOTTOM_TEXT_COLOR = Color.parseColor("#9B9A9B"); - - public static final int SHOW_POPUPS_All = 1; - public static final int SHOW_POPUPS_MAXMIN_ONLY = 2; - public static final int SHOW_POPUPS_NONE = 3; - + private int backgroundGridWidth = MyUtils.dip2px(getContext(), 45); private int showPopupType = SHOW_POPUPS_NONE; - public void setShowPopup(int popupType) { - this.showPopupType = popupType; - } - private Boolean drawDotLine = false; - private int[] colorArray = {Color.parseColor("#e74c3c"),Color.parseColor("#2980b9"),Color.parseColor("#1abc9c")}; - - // onDraw optimisations - private final Point tmpPoint = new Point(); - - public void setDrawDotLine(Boolean drawDotLine) { - this.drawDotLine = drawDotLine; - } - - private Runnable animator = new Runnable() { - @Override - public void run() { + private int[] colorArray = { + Color.parseColor("#e74c3c"), Color.parseColor("#2980b9"), Color.parseColor("#1abc9c") + }; + private Runnable animator = new Runnable() { + @Override public void run() { boolean needNewFrame = false; - for(ArrayList data : drawDotLists){ - for(Dot dot : data){ + for (ArrayList data : drawDotLists) { + for (Dot dot : data) { dot.update(); - if(!dot.isAtRest()){ + if (!dot.isAtRest()) { needNewFrame = true; } } @@ -112,10 +93,11 @@ public void run() { } }; - public LineView(Context context){ - this(context,null); + public LineView(Context context) { + this(context, null); } - public LineView(Context context, AttributeSet attrs){ + + public LineView(Context context, AttributeSet attrs) { super(context, attrs); popupTextPaint.setAntiAlias(true); popupTextPaint.setColor(Color.WHITE); @@ -124,48 +106,58 @@ public LineView(Context context, AttributeSet attrs){ popupTextPaint.setTextAlign(Paint.Align.CENTER); bottomTextPaint.setAntiAlias(true); - bottomTextPaint.setTextSize(MyUtils.sp2px(getContext(),12)); + bottomTextPaint.setTextSize(MyUtils.sp2px(getContext(), 12)); bottomTextPaint.setTextAlign(Paint.Align.CENTER); bottomTextPaint.setStyle(Paint.Style.FILL); bottomTextPaint.setColor(BOTTOM_TEXT_COLOR); refreshTopLineLength(); } - public void setColorArray(int[] colors){ + public void setShowPopup(int popupType) { + this.showPopupType = popupType; + } + + public void setDrawDotLine(Boolean drawDotLine) { + this.drawDotLine = drawDotLine; + } + + public void setColorArray(int[] colors) { this.colorArray = colors; } /** * dataList will be reset when called is method. + * * @param bottomTextList The String ArrayList in the bottom. */ - public void setBottomTextList(ArrayList bottomTextList){ + public void setBottomTextList(ArrayList bottomTextList) { this.bottomTextList = bottomTextList; Rect r = new Rect(); int longestWidth = 0; String longestStr = ""; bottomTextDescent = 0; - for(String s:bottomTextList){ - bottomTextPaint.getTextBounds(s,0,s.length(),r); - if(bottomTextHeight bottomTextList){ } /** - * * @param dataLists The Float ArrayLists for showing, - * dataList.size() must be smaller than bottomTextList.size() + * dataList.size() must be smaller than bottomTextList.size() */ - public void setFloatDataList(ArrayList> dataLists){ + public void setFloatDataList(ArrayList> dataLists) { setFloatDataList(dataLists, true); } - + public void setDataList(ArrayList> dataLists) { ArrayList> newList = new ArrayList<>(); for (ArrayList list : dataLists) { @@ -192,28 +183,29 @@ public void setDataList(ArrayList> dataLists) { } setFloatDataList(newList, false); } - - public void setFloatDataList(ArrayList> dataLists, boolean showFloatNumInPopup){ + + public void setFloatDataList(ArrayList> dataLists, + boolean showFloatNumInPopup) { selectedDot = null; this.showFloatNumInPopup = showFloatNumInPopup; this.dataLists = dataLists; - for(ArrayList list : dataLists){ - if(list.size() > bottomTextList.size()){ - throw new RuntimeException("dacer.LineView error:" + - " dataList.size() > bottomTextList.size() !!!"); + for (ArrayList list : dataLists) { + if (list.size() > bottomTextList.size()) { + throw new RuntimeException( + "dacer.LineView error:" + " dataList.size() > bottomTextList.size() !!!"); } } float biggestData = 0; - for(ArrayList list : dataLists){ - if(autoSetDataOfGird){ - for(Float i:list){ - if(biggestData list : dataLists) { + if (autoSetDataOfGird) { + for (Float i : list) { + if (biggestData < i) { biggestData = i; } } } dataOfAGird = 1; - while(biggestData/10 > dataOfAGird){ + while (biggestData / 10 > dataOfAGird) { dataOfAGird *= 10; } } @@ -225,266 +217,271 @@ public void setFloatDataList(ArrayList> dataLists, boolean show postInvalidate(); } - private void refreshAfterDataChanged(){ + private void refreshAfterDataChanged() { int verticalGridNum = getVerticalGridlNum(); refreshYCoordinateList(verticalGridNum); refreshDrawDotList(verticalGridNum); } - private int getVerticalGridlNum(){ + private int getVerticalGridlNum() { int verticalGridNum = MIN_VERTICAL_GRID_NUM; - if(dataLists != null && !dataLists.isEmpty()){ - for(ArrayList list : dataLists){ - for(Float f:list){ - if(verticalGridNum<(f + 1)){ - verticalGridNum = (int) Math.floor(f + 1); - } - } - } + if (dataLists != null && !dataLists.isEmpty()) { + for (ArrayList list : dataLists) { + for (Float f : list) { + if (verticalGridNum < (f + 1)) { + verticalGridNum = (int) Math.floor(f + 1); + } + } + } } return verticalGridNum; } - private int getHorizontalGridNum(){ - int horizontalGridNum = bottomTextList.size()-1; - if(horizontalGridNum()); - } - } - for(int k = 0; k < dataLists.size(); k++){ - int drawDotSize = drawDotLists.get(k).isEmpty()? 0:drawDotLists.get(k).size(); - - for(int i=0;i()); + } + } + for (int k = 0; k < dataLists.size(); k++) { + int drawDotSize = drawDotLists.get(k).isEmpty() ? 0 : drawDotLists.get(k).size(); + + for (int i = 0; i < dataLists.get(k).size(); i++) { int x = xCoordinateList.get(i); float y = getYAxesOf(dataLists.get(k).get(i), verticalGridNum); - if(i>drawDotSize-1){ - drawDotLists.get(k).add(new Dot(x, 0, x, y, dataLists.get(k).get(i),k)); - }else{ - drawDotLists.get(k).set(i, drawDotLists.get(k).get(i).setTargetData(x,y,dataLists.get(k).get(i),k)); + if (i > drawDotSize - 1) { + drawDotLists.get(k).add(new Dot(x, 0, x, y, dataLists.get(k).get(i), k)); + } else { + drawDotLists.get(k) + .set(i, drawDotLists.get(k) + .get(i) + .setTargetData(x, y, dataLists.get(k).get(i), k)); } } - - int temp = drawDotLists.get(k).size() - dataLists.get(k).size(); - for(int i=0; i data : drawDotLists) { for (Dot dot : data) { final int pointX = dot.x; - final int pointY = (int)dot.y; + final int pointY = (int) dot.y; r.set(pointX - width, pointY - width, pointX + width, pointY + width); - if (r.contains(x, y)){ + if (r.contains(x, y)) { return dot; } } @@ -536,18 +532,16 @@ private Dot findPointAt(int x, int y) { return null; } - - - class Dot{ + class Dot { int x; float y; float data; int targetX; float targetY; int linenumber; - int velocity = MyUtils.dip2px(getContext(),18); + int velocity = MyUtils.dip2px(getContext(), 18); - Dot(int x, float y, int targetX, float targetY, float data, int linenumber){ + Dot(int x, float y, int targetX, float targetY, float data, int linenumber) { this.x = x; this.y = y; this.linenumber = linenumber; @@ -559,7 +553,7 @@ Point setupPoint(Point point) { return point; } - Dot setTargetData(int targetX, float targetY, float data, int linenumber){ + Dot setTargetData(int targetX, float targetY, float data, int linenumber) { this.targetX = targetX; this.targetY = targetY; this.data = data; @@ -567,22 +561,22 @@ Dot setTargetData(int targetX, float targetY, float data, int linenumber){ return this; } - boolean isAtRest(){ - return (x==targetX) && (y==targetY); + boolean isAtRest() { + return (x == targetX) && (y == targetY); } - void update(){ + void update() { x = (int) updateSelf(x, targetX, velocity); y = updateSelf(y, targetY, velocity); } - private float updateSelf(float origin, float target, int velocity){ + private float updateSelf(float origin, float target, int velocity) { if (origin < target) { origin += velocity; - } else if (origin > target){ - origin-= velocity; + } else if (origin > target) { + origin -= velocity; } - if(Math.abs(target-origin) target){ - origin-= velocity; + } else if (origin > target) { + origin -= velocity; } - if(Math.abs(target-origin) pieHelperList; private int selectedIndex = NO_SELECTED_INDEX; - private boolean showPercentLabel = true; - public static final int NO_SELECTED_INDEX = -1; - private final int[] DEFAULT_COLOR_LIST = {Color.parseColor("#33B5E5"), - Color.parseColor("#AA66CC"), - Color.parseColor("#99CC00"), - Color.parseColor("#FFBB33"), - Color.parseColor("#FF4444")}; - - private Runnable animator = new Runnable() { - @Override - public void run() { + @Override public void run() { boolean needNewFrame = false; - for(PieHelper pie : pieHelperList){ + for (PieHelper pie : pieHelperList) { pie.update(); - if(!pie.isAtRest()){ + if (!pie.isAtRest()) { needNewFrame = true; } } @@ -64,10 +51,11 @@ public void run() { } }; - public PieView(Context context){ - this(context,null); + public PieView(Context context) { + this(context, null); } - public PieView(Context context, AttributeSet attrs){ + + public PieView(Context context, AttributeSet attrs) { super(context, attrs); pieHelperList = new ArrayList(); @@ -88,73 +76,73 @@ public PieView(Context context, AttributeSet attrs){ cirSelectedRect = new RectF(); } - public void showPercentLabel(boolean show){ + public void showPercentLabel(boolean show) { showPercentLabel = show; postInvalidate(); } - public void setOnPieClickListener(OnPieClickListener listener){ + public void setOnPieClickListener(OnPieClickListener listener) { onPieClickListener = listener; } - public void setDate(ArrayList helperList){ + 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)); + if (helperList != null && !helperList.isEmpty()) { + for (PieHelper pieHelper : helperList) { + pieHelperList.add( + new PieHelper(pieHelper.getStartDegree(), pieHelper.getStartDegree(), + pieHelper)); } - }else { + } else { pieHelperList.clear(); } removeCallbacks(animator); post(animator); -// pieHelperList = helperList; -// postInvalidate(); + // pieHelperList = helperList; + // postInvalidate(); } /** * Set startDegree and endDegree for each PieHelper - * @param helperList */ - private void initPies(ArrayList helperList){ + private void initPies(ArrayList helperList) { float totalAngel = 270; - for(PieHelper pie:helperList){ + for (PieHelper pie : helperList) { pie.setDegree(totalAngel, totalAngel + pie.getSweep()); totalAngel += pie.getSweep(); } } - public void selectedPie(int index){ + public void selectedPie(int index) { selectedIndex = index; - if(onPieClickListener!=null) onPieClickListener.onPieClick(index); + if (onPieClickListener != null) onPieClickListener.onPieClick(index); postInvalidate(); } - public void removeSelectedPie(){ + public void removeSelectedPie() { selectedIndex = NO_SELECTED_INDEX; - if(onPieClickListener!=null) onPieClickListener.onPieClick(NO_SELECTED_INDEX); + if (onPieClickListener != null) onPieClickListener.onPieClick(NO_SELECTED_INDEX); postInvalidate(); } - @Override - protected void onDraw(Canvas canvas) { - if(pieHelperList.isEmpty()){ - return ; + @Override protected void onDraw(Canvas canvas) { + if (pieHelperList.isEmpty()) { + return; } int index = 0; - for(PieHelper pieHelper : pieHelperList){ + for (PieHelper pieHelper : pieHelperList) { boolean selected = (selectedIndex == index); - RectF rect = selected? cirSelectedRect: cirRect; - if(pieHelper.isColorSetted()){ + RectF rect = selected ? cirSelectedRect : cirRect; + if (pieHelper.isColorSetted()) { cirPaint.setColor(pieHelper.getColor()); - }else { - cirPaint.setColor(DEFAULT_COLOR_LIST[index%5]); + } else { + cirPaint.setColor(DEFAULT_COLOR_LIST[index % 5]); } canvas.drawArc(rect, pieHelper.getStartDegree(), pieHelper.getSweep(), true, cirPaint); drawPercentText(canvas, pieHelper); @@ -163,54 +151,56 @@ protected void onDraw(Canvas canvas) { drawLineBesideCir(canvas, pieHelper.getEndDegree(), selected); index++; } - } - private void drawLineBesideCir(Canvas canvas, float angel, boolean selectedCir){ - int sth2 = selectedCir? mViewHeight/2 : pieRadius; // Sorry I'm really don't know how to name the variable.. + private void drawLineBesideCir(Canvas canvas, float angel, boolean selectedCir) { + int sth2 = selectedCir ? mViewHeight / 2 + : pieRadius; // Sorry I'm really don't know how to name the variable.. int sth = 1; // And it's - if(angel%360 > 180 && angel%360 < 360){ + 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); + 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; + 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){ + 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); + 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; + 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){ + 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); + 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_UP){ + @Override public boolean onTouchEvent(MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_UP) { int clickedIndex = findPointAt((int) event.getX(), (int) event.getY()); if (clickedIndex == selectedIndex) { selectedIndex = NO_SELECTED_INDEX; } else { selectedIndex = clickedIndex; } - if(onPieClickListener != null){ + if (onPieClickListener != null) { onPieClickListener.onPieClick(selectedIndex); } postInvalidate(); @@ -221,16 +211,13 @@ public boolean onTouchEvent(MotionEvent event) { /** * find pie index where point is - * @param x - * @param y - * @return */ - private int findPointAt(int x, int y){ - double degree = Math.atan2(x-pieCenterPoint.x, y-pieCenterPoint.y)* 180 / Math.PI; - degree = -(degree-180) + 270; + 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() && degree<=pieHelper.getEndDegree()){ + for (PieHelper pieHelper : pieHelperList) { + if (degree >= pieHelper.getStartDegree() && degree <= pieHelper.getEndDegree()) { return index; } index++; @@ -238,39 +225,34 @@ private int findPointAt(int x, int y){ return NO_SELECTED_INDEX; } - @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + @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); + 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 bigger circle - 2, - mViewWidth-2, - mViewHeight-2); + 2, mViewWidth - 2, mViewHeight - 2); setMeasuredDimension(mViewWidth, mViewHeight); } - private int measureWidth(int measureSpec){ + private int measureWidth(int measureSpec) { int preferred = 3; return getMeasurement(measureSpec, preferred); } - private int measureHeight(int measureSpec){ + private int measureHeight(int measureSpec) { int preferred = mViewWidth; return getMeasurement(measureSpec, preferred); } - private int getMeasurement(int measureSpec, int preferred){ + private int getMeasurement(int measureSpec, int preferred) { int specSize = View.MeasureSpec.getSize(measureSpec); int measurement; - switch(View.MeasureSpec.getMode(measureSpec)){ + switch (View.MeasureSpec.getMode(measureSpec)) { case View.MeasureSpec.EXACTLY: measurement = specSize; break; @@ -283,4 +265,8 @@ private int getMeasurement(int measureSpec, int preferred){ } return measurement; } + + public interface OnPieClickListener { + void onPieClick(int index); + } } diff --git a/AndroidCharts/src/main/java/im/dacer/androidcharts/TempLog.java b/AndroidCharts/src/main/java/im/dacer/androidcharts/TempLog.java index 67bda06..7d6f592 100644 --- a/AndroidCharts/src/main/java/im/dacer/androidcharts/TempLog.java +++ b/AndroidCharts/src/main/java/im/dacer/androidcharts/TempLog.java @@ -6,12 +6,15 @@ * Created by Dacer on 10/23/13. */ public class TempLog { - public static void show(String s){ - Log.e("Log--->",s); + public static void show(String s) { + Log.e("Log--->", s); } - public static void show(int i){ - Log.e("Log--->",String.valueOf(i)); - }public static void show(float i){ - Log.e("Log--->",String.valueOf(i)); + + public static void show(int i) { + Log.e("Log--->", String.valueOf(i)); + } + + public static void show(float i) { + Log.e("Log--->", String.valueOf(i)); } } diff --git a/AndroidChartsExample/src/main/AndroidManifest.xml b/AndroidChartsExample/src/main/AndroidManifest.xml index 380f100..00dce0c 100644 --- a/AndroidChartsExample/src/main/AndroidManifest.xml +++ b/AndroidChartsExample/src/main/AndroidManifest.xml @@ -1,25 +1,25 @@ + android:versionName="1.0" + package="com.dacer.androidchartsexample"> + android:targetSdkVersion="19"/> + android:theme="@style/AppTheme"> + android:label="@string/app_name"> - + - + diff --git a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/BarFragment.java b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/BarFragment.java index 645bd49..be82a40 100644 --- a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/BarFragment.java +++ b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/BarFragment.java @@ -6,25 +6,20 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; - import im.dacer.androidcharts.BarView; -import im.dacer.androidcharts.LineView; - import java.util.ArrayList; -import java.util.Arrays; /** * Created by Dacer on 11/15/13. */ public class BarFragment extends Fragment { - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_bar, container, false); - final BarView barView = (BarView)rootView.findViewById(R.id.bar_view); - Button button = (Button)rootView.findViewById(R.id.bar_button); + final BarView barView = (BarView) rootView.findViewById(R.id.bar_view); + Button button = (Button) rootView.findViewById(R.id.bar_button); button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { + @Override public void onClick(View view) { randomSet(barView); } }); @@ -32,20 +27,20 @@ public void onClick(View view) { return rootView; } - private void randomSet(BarView barView){ - int random = (int)(Math.random()*20)+6; + private void randomSet(BarView barView) { + int random = (int) (Math.random() * 20) + 6; ArrayList test = new ArrayList(); - for (int i=0; i barDataList = new ArrayList(); - for(int i=0; i clockPieHelperArrayList = new ArrayList(); - for(int i=0; i<20; i++){ - int startHour = (int)(24*Math.random()); - int startMin = (int)(60*Math.random()); - int duration = (int)(50*Math.random()); - clockPieHelperArrayList.add(new ClockPieHelper(startHour,startMin,0,startHour,startMin+duration,0)); + for (int i = 0; i < 20; i++) { + int startHour = (int) (24 * Math.random()); + int startMin = (int) (60 * Math.random()); + int duration = (int) (50 * Math.random()); + clockPieHelperArrayList.add( + new ClockPieHelper(startHour, startMin, 0, startHour, startMin + duration, 0)); } clockPieView.setDate(clockPieHelperArrayList); } - private void set(ClockPieView clockPieView){ + private void set(ClockPieView clockPieView) { ArrayList clockPieHelperArrayList = new ArrayList(); - clockPieHelperArrayList.add(new ClockPieHelper(1,50,2,30)); - clockPieHelperArrayList.add(new ClockPieHelper(6,50,8,30)); + clockPieHelperArrayList.add(new ClockPieHelper(1, 50, 2, 30)); + clockPieHelperArrayList.add(new ClockPieHelper(6, 50, 8, 30)); clockPieView.setDate(clockPieHelperArrayList); } } \ No newline at end of file diff --git a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/LineFragment.java b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/LineFragment.java index e835a42..786054c 100644 --- a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/LineFragment.java +++ b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/LineFragment.java @@ -7,9 +7,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; - import im.dacer.androidcharts.LineView; - import java.util.ArrayList; /** @@ -18,22 +16,18 @@ public class LineFragment extends Fragment { int randomint = 9; - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_line, container, false); - final LineView lineView = (LineView)rootView.findViewById(R.id.line_view); - final LineView lineViewFloat = (LineView)rootView.findViewById(R.id.line_view_float); - + final LineView lineView = (LineView) rootView.findViewById(R.id.line_view); + final LineView lineViewFloat = (LineView) rootView.findViewById(R.id.line_view_float); initLineView(lineView); initLineView(lineViewFloat); - Button lineButton = (Button)rootView.findViewById(R.id.line_button); + Button lineButton = (Button) rootView.findViewById(R.id.line_button); lineButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { + @Override public void onClick(View view) { randomSet(lineView, lineViewFloat); - } }); @@ -43,33 +37,35 @@ public void onClick(View view) { private void initLineView(LineView lineView) { ArrayList test = new ArrayList(); - for (int i=0; i dataList = new ArrayList<>(); - float random = (float)(Math.random()*9+1); - for (int i=0; i dataList2 = new ArrayList<>(); - random = (int)(Math.random()*9+1); - for (int i=0; i dataList3 = new ArrayList<>(); - random = (int)(Math.random()*9+1); - for (int i=0; i> dataLists = new ArrayList<>(); @@ -79,23 +75,22 @@ private void randomSet(LineView lineView, LineView lineViewFloat){ lineView.setDataList(dataLists); - ArrayList dataListF = new ArrayList<>(); - float randomF = (float)(Math.random()*9+1); - for (int i=0; i dataListF2 = new ArrayList<>(); - randomF = (int)(Math.random()*9+1); - for (int i=0; i dataListF3 = new ArrayList<>(); - randomF = (int)(Math.random()*9+1); - for (int i=0; i> dataListFs = new ArrayList<>(); @@ -104,6 +99,5 @@ private void randomSet(LineView lineView, LineView lineViewFloat){ dataListFs.add(dataListF3); lineViewFloat.setFloatDataList(dataListFs); - } } \ No newline at end of file diff --git a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/MainActivity.java b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/MainActivity.java index a9dc314..24c312d 100644 --- a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/MainActivity.java +++ b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/MainActivity.java @@ -1,22 +1,12 @@ package com.dacer.androidchartsexample; -import android.app.Activity; -; import android.app.ActionBar; -import android.app.Fragment; +import android.app.Activity; import android.app.FragmentManager; -import android.content.Context; -import android.os.Build; import android.os.Bundle; -import android.view.Gravity; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; import android.support.v4.widget.DrawerLayout; -import android.widget.ArrayAdapter; -import android.widget.TextView; + +; public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks { @@ -31,26 +21,24 @@ public class MainActivity extends Activity */ private CharSequence mTitle; - @Override - protected void onCreate(Bundle savedInstanceState) { + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - mNavigationDrawerFragment = (NavigationDrawerFragment) - getFragmentManager().findFragmentById(R.id.navigation_drawer); + mNavigationDrawerFragment = + (NavigationDrawerFragment) getFragmentManager().findFragmentById( + R.id.navigation_drawer); mTitle = getTitle(); // Set up the drawer. - mNavigationDrawerFragment.setUp( - R.id.navigation_drawer, + mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); } - @Override - public void onNavigationDrawerItemSelected(int position) { + @Override public void onNavigationDrawerItemSelected(int position) { // update the main content by replacing fragments FragmentManager fragmentManager = getFragmentManager(); - switch (position){ + switch (position) { case 0: fragmentManager.beginTransaction() .replace(R.id.container, new LineFragment()) @@ -97,5 +85,4 @@ public void restoreActionBar() { actionBar.setDisplayShowTitleEnabled(true); actionBar.setTitle(mTitle); } - } diff --git a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/NavigationDrawerFragment.java b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/NavigationDrawerFragment.java index 264d116..92a0b86 100644 --- a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/NavigationDrawerFragment.java +++ b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/NavigationDrawerFragment.java @@ -1,26 +1,24 @@ package com.dacer.androidchartsexample; -; -import android.app.Activity; import android.app.ActionBar; +import android.app.Activity; import android.app.Fragment; -import android.support.v4.app.ActionBarDrawerToggle; -import android.support.v4.view.GravityCompat; -import android.support.v4.widget.DrawerLayout; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.v4.app.ActionBarDrawerToggle; +import android.support.v4.view.GravityCompat; +import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; -import android.widget.Toast; + +; /** * Fragment used for managing interactions for and presentation of a navigation drawer. @@ -61,8 +59,7 @@ public class NavigationDrawerFragment extends Fragment { public NavigationDrawerFragment() { } - @Override - public void onCreate(Bundle savedInstanceState) { + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Read in the flag indicating whether or not the user has demonstrated awareness of the @@ -79,34 +76,27 @@ public void onCreate(Bundle savedInstanceState) { selectItem(mCurrentSelectedPosition); } - @Override - public void onActivityCreated (Bundle savedInstanceState) { + @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // Indicate that this fragment would like to influence the set of actions in the action bar. setHasOptionsMenu(true); } - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - mDrawerListView = (ListView) inflater.inflate( - R.layout.fragment_navigation_drawer, container, false); + mDrawerListView = + (ListView) inflater.inflate(R.layout.fragment_navigation_drawer, container, false); mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { selectItem(position); } }); - mDrawerListView.setAdapter(new ArrayAdapter( - getActionBar().getThemedContext(), - android.R.layout.simple_list_item_activated_1, - android.R.id.text1, - new String[]{ - getString(R.string.title_section1), - getString(R.string.title_section2), - getString(R.string.title_section3), - getString(R.string.title_section4) - })); + mDrawerListView.setAdapter(new ArrayAdapter(getActionBar().getThemedContext(), + android.R.layout.simple_list_item_activated_1, android.R.id.text1, new String[] { + getString(R.string.title_section1), getString(R.string.title_section2), + getString(R.string.title_section3), getString(R.string.title_section4) + })); mDrawerListView.setItemChecked(mCurrentSelectedPosition, true); return mDrawerListView; } @@ -118,7 +108,7 @@ public boolean isDrawerOpen() { /** * Users of this fragment must call this method to set up the navigation drawer interactions. * - * @param fragmentId The android:id of this fragment in its activity's layout. + * @param fragmentId The android:id of this fragment in its activity's layout. * @param drawerLayout The DrawerLayout containing this fragment's UI. */ public void setUp(int fragmentId, DrawerLayout drawerLayout) { @@ -135,42 +125,39 @@ public void setUp(int fragmentId, DrawerLayout drawerLayout) { // ActionBarDrawerToggle ties together the the proper interactions // between the navigation drawer and the action bar app icon. - mDrawerToggle = new ActionBarDrawerToggle( - getActivity(), /* host Activity */ - mDrawerLayout, /* DrawerLayout object */ - R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ - R.string.navigation_drawer_open, /* "open drawer" description for accessibility */ - R.string.navigation_drawer_close /* "close drawer" description for accessibility */ - ) { - @Override - public void onDrawerClosed(View drawerView) { - super.onDrawerClosed(drawerView); - if (!isAdded()) { - return; - } - - getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() - } - - @Override - public void onDrawerOpened(View drawerView) { - super.onDrawerOpened(drawerView); - if (!isAdded()) { - return; - } - - if (!mUserLearnedDrawer) { - // The user manually opened the drawer; store this flag to prevent auto-showing - // the navigation drawer automatically in the future. - mUserLearnedDrawer = true; - SharedPreferences sp = PreferenceManager - .getDefaultSharedPreferences(getActivity()); - sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); - } - - getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() - } - }; + mDrawerToggle = + new ActionBarDrawerToggle(getActivity(), /* host Activity */ + mDrawerLayout, /* DrawerLayout object */ + R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ + R.string.navigation_drawer_open, /* "open drawer" description for accessibility */ + R.string.navigation_drawer_close /* "close drawer" description for accessibility */) { + @Override public void onDrawerClosed(View drawerView) { + super.onDrawerClosed(drawerView); + if (!isAdded()) { + return; + } + + getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() + } + + @Override public void onDrawerOpened(View drawerView) { + super.onDrawerOpened(drawerView); + if (!isAdded()) { + return; + } + + if (!mUserLearnedDrawer) { + // The user manually opened the drawer; store this flag to prevent auto-showing + // the navigation drawer automatically in the future. + mUserLearnedDrawer = true; + SharedPreferences sp = + PreferenceManager.getDefaultSharedPreferences(getActivity()); + sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply(); + } + + getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu() + } + }; // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer, // per the navigation drawer design guidelines. @@ -180,8 +167,7 @@ public void onDrawerOpened(View drawerView) { // Defer code dependent on restoration of previous instance state. mDrawerLayout.post(new Runnable() { - @Override - public void run() { + @Override public void run() { mDrawerToggle.syncState(); } }); @@ -202,8 +188,7 @@ private void selectItem(int position) { } } - @Override - public void onAttach(Activity activity) { + @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallbacks = (NavigationDrawerCallbacks) activity; @@ -212,27 +197,23 @@ public void onAttach(Activity activity) { } } - @Override - public void onDetach() { + @Override public void onDetach() { super.onDetach(); mCallbacks = null; } - @Override - public void onSaveInstanceState(Bundle outState) { + @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition); } - @Override - public void onConfigurationChanged(Configuration newConfig) { + @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Forward the new configuration the drawer toggle component. mDrawerToggle.onConfigurationChanged(newConfig); } - @Override - public boolean onOptionsItemSelected(MenuItem item) { + @Override public boolean onOptionsItemSelected(MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } diff --git a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/PieFragment.java b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/PieFragment.java index efa137f..0decdea 100644 --- a/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/PieFragment.java +++ b/AndroidChartsExample/src/main/java/com/dacer/androidchartsexample/PieFragment.java @@ -8,11 +8,8 @@ import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; -import android.widget.Toast; - import im.dacer.androidcharts.PieHelper; import im.dacer.androidcharts.PieView; - import java.util.ArrayList; /** @@ -21,15 +18,14 @@ public class PieFragment extends Fragment { private TextView textView; - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_pie_s, container, false); textView = (TextView) rootView.findViewById(R.id.textView); - final PieView pieView = (PieView)rootView.findViewById(R.id.pie_view); - Button button = (Button)rootView.findViewById(R.id.pie_button); + final PieView pieView = (PieView) rootView.findViewById(R.id.pie_view); + Button button = (Button) rootView.findViewById(R.id.pie_button); button.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { + @Override public void onClick(View view) { randomSet(pieView); } }); @@ -37,19 +33,19 @@ public void onClick(View view) { return rootView; } - private void randomSet(PieView pieView){ + private void randomSet(PieView pieView) { ArrayList pieHelperArrayList = new ArrayList(); ArrayList intList = new ArrayList(); - int totalNum = (int) (5*Math.random()) + 5; + int totalNum = (int) (5 * Math.random()) + 5; int totalInt = 0; - for(int i=0; i pieHelperArrayList = new ArrayList(); pieHelperArrayList.add(new PieHelper(20, Color.BLACK)); pieHelperArrayList.add(new PieHelper(6)); @@ -67,11 +63,10 @@ private void set(PieView pieView){ pieView.setDate(pieHelperArrayList); pieView.setOnPieClickListener(new PieView.OnPieClickListener() { - @Override - public void onPieClick(int index) { - if(index != PieView.NO_SELECTED_INDEX) { + @Override public void onPieClick(int index) { + if (index != PieView.NO_SELECTED_INDEX) { textView.setText(index + " selected"); - }else{ + } else { textView.setText("No selected pie"); } } diff --git a/AndroidChartsExample/src/main/res/layout/activity_main.xml b/AndroidChartsExample/src/main/res/layout/activity_main.xml index 41b0e24..afe9840 100644 --- a/AndroidChartsExample/src/main/res/layout/activity_main.xml +++ b/AndroidChartsExample/src/main/res/layout/activity_main.xml @@ -5,14 +5,16 @@ android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".MainActivity" + > + android:layout_height="match_parent" + /> - + /> diff --git a/AndroidChartsExample/src/main/res/layout/fragment_bar.xml b/AndroidChartsExample/src/main/res/layout/fragment_bar.xml index ecf22c9..8c3382d 100644 --- a/AndroidChartsExample/src/main/res/layout/fragment_bar.xml +++ b/AndroidChartsExample/src/main/res/layout/fragment_bar.xml @@ -1,29 +1,33 @@ + android:layout_width="match_parent" + android:layout_height="match_parent" + android:background="#ffffff" + > + android:layout_centerHorizontal="true" + > - - + /> +