Commits

StephaneBg  committed 4d28a6d

Corrected touch event management.

  • Participants
  • Parent commits 83c841d

Comments (0)

Files changed (7)

File HoloGraphLibrary/res/values/attrs.xml

 
     <declare-styleable name="LineGraph">
         <attr name="strokeColor" format="color"/>
+        <attr name="lineAxisColor" format="color"/>
         <attr name="strokeWidth" format="dimension"/>
         <attr name="strokeSpacing" format="dimension"/>
         <attr name="useDip" format="boolean"/>
     </declare-styleable>
 
     <declare-styleable name="BarGraph">
-        <attr name="axisColor" format="color"/>
+        <attr name="barAxisColor" format="color"/>
         <attr name="orientation" format="enum">
             <enum name="horizontal" value="0"/>
             <enum name="vertical" value="1"/>

File HoloGraphLibrary/src/com/echo/holographlibrary/BarGraph.java

 
         TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BarGraph);
         mOrientation = a.getInt(R.styleable.BarGraph_orientation, ORIENTATION_VERTICAL);
-        mAxisColor = a.getColor(R.styleable.BarGraph_axisColor, Color.LTGRAY);
+        mAxisColor = a.getColor(R.styleable.BarGraph_barAxisColor, Color.LTGRAY);
     }
 
     public void setShowBarText(boolean show) {
         point.y = (int) event.getY();
 
         int count = 0;
+        Region r = new Region();
         for (Bar bar : mBars) {
-            Region r = new Region();
             r.setPath(bar.getPath(), bar.getRegion());
             switch (event.getAction()) {
                 default:
                     }
                     break;
                 case MotionEvent.ACTION_UP:
-                    if (r.contains(point.x, point.y) && null != mListener) {
-                        if (mSelectedIndex > -1) {
-                            mListener.onClick(mSelectedIndex);
-                        }
-                        mSelectedIndex = -1;
+                    if (count == mSelectedIndex
+                            && mListener != null
+                            && r.contains(point.x, point.y)) {
+                        mListener.onClick(mSelectedIndex);
                     }
-                    postInvalidate();
-                    break;
-                case MotionEvent.ACTION_CANCEL:
-                    mSelectedIndex = -1;
-                    postInvalidate();
                     break;
             }
             count++;
         }
+        // Reset selection
+        if (MotionEvent.ACTION_UP == event.getAction()
+                || MotionEvent.ACTION_CANCEL == event.getAction()) {
+            mSelectedIndex = -1;
+            postInvalidate();
+        }
         return true;
     }
 

File HoloGraphLibrary/src/com/echo/holographlibrary/LineGraph.java

     private static final int DEFAULT_PADDING = 10;
     private final int mDipPadding;
     private final int mFillColor;
+    private final int mAxisColor;
     private final float mStrokeWidth;
     private final int mStrokeSpacing;
     private ArrayList<Line> mLines = new ArrayList<Line>();
 
         TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.LineGraph, 0, 0);
         mFillColor = a.getColor(R.styleable.LineGraph_strokeColor, Color.BLACK);
+        mAxisColor = a.getColor(R.styleable.LineGraph_lineAxisColor, Color.LTGRAY);
         mStrokeWidth = a.getDimension(R.styleable.LineGraph_strokeWidth, 2);
         mStrokeSpacing = a.getDimensionPixelSize(R.styleable.LineGraph_strokeSpacing, 10);
         mUseDips = a.getBoolean(R.styleable.LineGraph_useDip, false);
             mCanvas = new Canvas(mFullImage);
         }
 
+        mCanvas.drawColor(Color.WHITE);
         mPaint.reset();
         float bottomPadding = 10, topPadding = 10;
         float sidePadding = 10;
             float lastYPixels = 0, newXPixels = 0;
 
             if (lineCount == mLineToFill) {
+                // Draw lines
                 mPaint.setColor(mFillColor);
                 mPaint.setStrokeWidth(mStrokeWidth);
                 for (int i = 10; i - getWidth() < getHeight(); i = i + mStrokeSpacing) {
                     mCanvas.drawLine(i, getHeight() - bottomPadding, 0, getHeight() - bottomPadding - i, mPaint);
                 }
 
+                // Erase lines above the line
                 mPaint.reset();
-
                 mPaint.setXfermode(mXfermode);
                 for (LinePoint p : line.getPoints()) {
                     float yPercent = (p.getY() - minY) / (maxY - minY);
                 }
 
                 mPath.reset();
-
                 mPath.moveTo(0, getHeight() - bottomPadding);
                 mPath.lineTo(sidePadding, getHeight() - bottomPadding);
                 mPath.lineTo(sidePadding, 0);
                 mCanvas.drawPath(mPath, mPaint);
 
                 mPath.reset();
-
                 mPath.moveTo(getWidth(), getHeight() - bottomPadding);
                 mPath.lineTo(getWidth() - sidePadding, getHeight() - bottomPadding);
                 mPath.lineTo(getWidth() - sidePadding, 0);
                 mPath.close();
 
                 mCanvas.drawPath(mPath, mPaint);
-
             }
             lineCount++;
         }
 
+        // Draw x-axis line
         mPaint.reset();
-        mPaint.setColor(Color.BLACK);
-        mPaint.setAlpha(50);
+        mPaint.setColor(mAxisColor);
+        mPaint.setStrokeWidth(2 * getResources().getDisplayMetrics().density);
         mPaint.setAntiAlias(true);
         mCanvas.drawLine(sidePadding, getHeight() - bottomPadding, getWidth() - sidePadding, getHeight() - bottomPadding, mPaint);
-        mPaint.setAlpha(255);
+        mPaint.reset();
 
+        // Draw lines
         for (Line line : mLines) {
             int count = 0;
             float lastXPixels = 0, newYPixels = 0;
             }
         }
 
-
+        // Draw points
         int pointCount = 0;
-
         for (Line line : mLines) {
-
             mPaint.setColor(line.getColor());
             mPaint.setStrokeWidth(getStrokeWidth(line));
             mPaint.setStrokeCap(Paint.Cap.ROUND);
                     mPaint.setColor(Color.WHITE);
                     mCanvas.drawCircle(xPixels, yPixels, innerRadius, mPaint);
 
+                    // Create selection region
                     Path path = p.getPath();
                     path.reset();
-                    path.addCircle(xPixels, yPixels, 30, Direction.CW);
-                    p.getRegion().set((int) (xPixels - 30), (int) (yPixels - 30), (int) (xPixels + 30), (int) (yPixels + 30));
-
+                    outerRadius *= 2;
+                    path.addCircle(xPixels, yPixels, outerRadius, Direction.CW);
+                    p.getRegion().set((int) (xPixels - outerRadius),
+                            (int) (yPixels - outerRadius),
+                            (int) (xPixels + outerRadius),
+                            (int) (yPixels + outerRadius));
+
+                    // Draw selection
                     if (mSelectedIndex == pointCount && mListener != null) {
                         mPaint.setColor(p.getSelectedColor());
                         mCanvas.drawPath(p.getPath(), mPaint);
                         }
                         break;
                     case MotionEvent.ACTION_UP:
-                        if (r.contains(point.x, point.y) && mListener != null) {
-                            if (mSelectedIndex > -1) {
-                                mListener.onClick(lineCount, pointCount);
-                            }
-                            mSelectedIndex = -1;
-                            postInvalidate();
+                        if (count == mSelectedIndex
+                                && mListener != null
+                                && r.contains(point.x, point.y)) {
+                            mListener.onClick(lineCount, pointCount);
                         }
                         break;
                 }
             }
             lineCount++;
         }
+        // Reset selection
+        if (MotionEvent.ACTION_UP == event.getAction()
+                || MotionEvent.ACTION_CANCEL == event.getAction()) {
+            mSelectedIndex = -1;
+            postInvalidate();
+        }
         return true;
     }
 

File HoloGraphLibrary/src/com/echo/holographlibrary/PieGraph.java

                     (currentAngle + mPadding) + (currentSweep - mPadding), -(currentSweep - mPadding));
             p.close();
 
+            // Create selection region
             Region r = slice.getRegion();
             r.set((int) (midX - radius),
                     (int) (midY - radius),
             point.y = (int) event.getY();
 
             int count = 0;
+            Region r = new Region();
             for (PieSlice slice : mSlices) {
-                Region r = new Region();
                 r.setPath(slice.getPath(), slice.getRegion());
                 switch (event.getAction()) {
                     default:
                         }
                         break;
                     case MotionEvent.ACTION_UP:
-                        if (r.contains(point.x, point.y) && mListener != null) {
-                            if (mSelectedIndex > -1) {
-                                mListener.onClick(mSelectedIndex);
-                            }
-                            mSelectedIndex = -1;
-                            postInvalidate();
+                        if (count == mSelectedIndex
+                                && mListener != null
+                                && r.contains(point.x, point.y)) {
+                            mListener.onClick(mSelectedIndex);
                         }
                         break;
-                    case MotionEvent.ACTION_CANCEL:
-                        mSelectedIndex = -1;
-                        postInvalidate();
-                        break;
                 }
                 count++;
             }
         }
+        // Reset selection
+        if (MotionEvent.ACTION_UP == event.getAction()
+                || MotionEvent.ACTION_CANCEL == event.getAction()) {
+            mSelectedIndex = -1;
+            postInvalidate();
+        }
         return true;
     }
 

File HoloGraphLibrarySample/res/layout/fragment_bargraph.xml

 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
-    
+
     <com.echo.holographlibrary.BarGraph
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:id="@+id/bargraph"
-        android:layout_margin="@dimen/default_margin"/>
+        android:layout_margin="@dimen/default_margin"
+        app:barAxisColor="@color/transparent_blue"/>
 
 </LinearLayout>

File HoloGraphLibrarySample/res/layout/fragment_linegraph.xml

         android:id="@+id/linegraph"
         android:layout_margin="@dimen/default_margin"
         app:strokeColor="@color/transparent_orange"
+        app:lineAxisColor="@color/transparent_blue"
         app:strokeWidth="1dip"
         app:strokeSpacing="10dip"
         app:useDip="true"/>

File HoloGraphLibrarySample/src/com/echo/holographlibrarysample/LineFragment.java

 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.Toast;
 
 import com.echo.holographlibrary.Line;
 import com.echo.holographlibrary.LineGraph;
 
             @Override
             public void onClick(int lineIndex, int pointIndex) {
-
+                Toast.makeText(getActivity(),
+                        "Line " + lineIndex + " / Point " + pointIndex + " clicked",
+                        Toast.LENGTH_SHORT)
+                        .show();
             }
-
         });
 
         return v;