소스 검색

Added graphsapp for testing purposes

Berislav 9 년 전
부모
커밋
b1106e2445
27개의 변경된 파일과 329개의 추가작업 그리고 49개의 파일을 삭제
  1. 1 0
      .idea/gradle.xml
  2. 2 1
      .idea/modules.xml
  3. 1 1
      app/build.gradle
  4. 76 46
      app/src/main/java/hr/tacitus/graphs/BarGraphView.java
  5. 1 0
      app/src/main/res/values/attrs.xml
  6. 1 0
      graphsapp/.gitignore
  7. 34 0
      graphsapp/build.gradle
  8. 25 0
      graphsapp/proguard-rules.pro
  9. 26 0
      graphsapp/src/androidTest/java/hr/tacitus/graphsapp/ExampleInstrumentedTest.java
  10. 16 0
      graphsapp/src/main/AndroidManifest.xml
  11. 64 0
      graphsapp/src/main/java/hr/tacitus/graphsapp/MainActivity.java
  12. 44 0
      graphsapp/src/main/res/layout/activity_main.xml
  13. BIN
      graphsapp/src/main/res/mipmap-hdpi/ic_launcher.png
  14. BIN
      graphsapp/src/main/res/mipmap-hdpi/ic_launcher_round.png
  15. BIN
      graphsapp/src/main/res/mipmap-mdpi/ic_launcher.png
  16. BIN
      graphsapp/src/main/res/mipmap-mdpi/ic_launcher_round.png
  17. BIN
      graphsapp/src/main/res/mipmap-xhdpi/ic_launcher.png
  18. BIN
      graphsapp/src/main/res/mipmap-xhdpi/ic_launcher_round.png
  19. BIN
      graphsapp/src/main/res/mipmap-xxhdpi/ic_launcher.png
  20. BIN
      graphsapp/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
  21. BIN
      graphsapp/src/main/res/mipmap-xxxhdpi/ic_launcher.png
  22. BIN
      graphsapp/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
  23. 6 0
      graphsapp/src/main/res/values/colors.xml
  24. 3 0
      graphsapp/src/main/res/values/strings.xml
  25. 11 0
      graphsapp/src/main/res/values/styles.xml
  26. 17 0
      graphsapp/src/test/java/hr/tacitus/graphsapp/ExampleUnitTest.java
  27. 1 1
      settings.gradle

+ 1 - 0
.idea/gradle.xml

@@ -9,6 +9,7 @@
           <set>
             <option value="$PROJECT_DIR$" />
             <option value="$PROJECT_DIR$/app" />
+            <option value="$PROJECT_DIR$/graphsapp" />
           </set>
         </option>
         <option name="resolveModulePerSourceSet" value="false" />

+ 2 - 1
.idea/modules.xml

@@ -2,8 +2,9 @@
 <project version="4">
   <component name="ProjectModuleManager">
     <modules>
-      <module fileurl="file://$PROJECT_DIR$/Graphs.iml" filepath="$PROJECT_DIR$/Graphs.iml" />
+      <module fileurl="file://$PROJECT_DIR$/android-graphs.iml" filepath="$PROJECT_DIR$/android-graphs.iml" />
       <module fileurl="file://$PROJECT_DIR$/app/app.iml" filepath="$PROJECT_DIR$/app/app.iml" />
+      <module fileurl="file://$PROJECT_DIR$/graphsapp/graphsapp.iml" filepath="$PROJECT_DIR$/graphsapp/graphsapp.iml" />
     </modules>
   </component>
 </project>

+ 1 - 1
app/build.gradle

@@ -21,7 +21,7 @@ android {
 }
 
 dependencies {
-    compile fileTree(dir: 'libs', include: ['*.jar', 'src/main/res/values/attrs.xml'])
+    compile fileTree(dir: 'libs', include: ['*.jar'])
     androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
         exclude group: 'com.android.support', module: 'support-annotations'
     })

+ 76 - 46
app/src/main/java/hr/tacitus/graphs/BarGraphView.java

@@ -15,8 +15,6 @@ import android.view.MotionEvent;
 import android.view.View;
 import android.view.WindowManager;
 
-import hr.tacitus.graphs.R;
-
 public class BarGraphView extends View {
     // Spacing between axis and values, and values and description.
     private final static float DEFAULT_AXIS_LINE_SPACING_DP = 2.0f;
@@ -24,6 +22,7 @@ public class BarGraphView extends View {
     private final static float DEFAULT_AXIS_DESCRIPTION_FONT_SIZE_SP = 16.f;
     private final static float AVERAGE_LINE_STROKE_WIDTH = 1.f;
     private final static float VALUE_TEXT_OFFSET = 5f;
+    private final static float NO_DATA_TEXT_SIZE_SP = 18.f;
 
     VerticalAxisRect verticalAxisRect = new VerticalAxisRect();
     HorizontalAxisRect horizontalAxisRect = new HorizontalAxisRect();
@@ -33,10 +32,12 @@ public class BarGraphView extends View {
     // Scroll value, default is leftmost.
     private float offsetX = 0;
     private boolean drawAverageLine = false;
+    private String noDataText;
 
     private Paint averageLinePaint = null;
     private Paint canvasPaint = null;
     private Paint axisPaint = null;
+    private Paint textPaint = null;
 
     private DisplayMetrics dm = null;
 
@@ -50,6 +51,39 @@ public class BarGraphView extends View {
     float barPaddingPx;
     float ordinateMinMarkingsDistance;
 
+    GestureDetector gestureDetector = new GestureDetector(this.getContext(), new OnGestureListener() {
+        @Override
+        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
+            scrollBy((int) distanceX, (int) distanceY);
+            return true;
+        }
+
+        @Override
+        public boolean onDown(MotionEvent e) {
+            return true;
+        }
+
+        @Override
+        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
+            return false;
+        }
+
+        @Override
+        public void onLongPress(MotionEvent e) {
+
+        }
+
+        @Override
+        public void onShowPress(MotionEvent e) {
+
+        }
+
+        @Override
+        public boolean onSingleTapUp(MotionEvent e) {
+            return false;
+        }
+    });
+
     public BarGraphView(Context context) {
         super(context);
         init(context, null);
@@ -70,6 +104,7 @@ public class BarGraphView extends View {
         barPaddingPx = getDimensionFromDp(6.f);
         axisValuesFontSizePx = getDimensionFromSp(14.f);
         ordinateMinMarkingsDistance = getDimensionFromDp(40.f);
+        noDataText = "No data available.";
     }
 
     private void setupDisplayMetrics() {
@@ -102,6 +137,9 @@ public class BarGraphView extends View {
                 horizontalAxisRect.setShowValues(a.getBoolean(R.styleable.BarGraph_showHorizontalValues, false));
                 verticalAxisRect.setShowValues(a.getBoolean(R.styleable.BarGraph_showVerticalValues, true));
 
+                if (a.hasValue(R.styleable.BarGraph_noDataText))
+                    noDataText = a.getString(R.styleable.BarGraph_noDataText);
+
             } finally {
                 a.recycle();
             }
@@ -117,12 +155,18 @@ public class BarGraphView extends View {
         canvasPaint = new Paint();
         canvasPaint.setStyle(Paint.Style.FILL);
         canvasPaint.setColor(Color.WHITE);
-        canvasPaint.setAntiAlias(true);
+        // canvasPaint.setAntiAlias(true);
 
         axisPaint = new Paint();
         axisPaint.setStyle(Paint.Style.FILL);
         axisPaint.setColor(Color.BLACK);
         axisPaint.setAntiAlias(true);
+
+        textPaint = new Paint();
+        textPaint.setStyle(Paint.Style.FILL);
+        textPaint.setColor(Color.BLACK);
+        textPaint.setAntiAlias(true);
+        textPaint.setTextSize(getDimensionFromSp(NO_DATA_TEXT_SIZE_SP));
     }
 
     private void init(Context context, AttributeSet attrs) {
@@ -158,15 +202,28 @@ public class BarGraphView extends View {
         canvas.drawLine(verticalAxisRect.getWidth() + getPaddingLeft(), y1, width - getPaddingRight(), y2, averageLinePaint);
     }
 
+    private void drawNoDataText(Canvas canvas) {
+        Rect drawingRect = new Rect();
+        getDrawingRect(drawingRect);
+
+        Rect textBounds = new Rect();
+        textPaint.getTextBounds(noDataText, 0, noDataText.length() - 1, textBounds);
+
+        canvas.drawText(noDataText, drawingRect.exactCenterX() - textBounds.width() / 2,
+                drawingRect.exactCenterY() - textBounds.height() / 2, textPaint);
+    }
+
     @Override
     protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
 
-        if (adapter == null)
-            return;
-
         canvas.drawPaint(canvasPaint);
 
+        if (adapter == null) {
+            drawNoDataText(canvas);
+            return;
+        }
+
         drawHorizontalAxis(canvas);
         drawVerticalAxis(canvas);
         drawValues(canvas);
@@ -348,43 +405,11 @@ public class BarGraphView extends View {
         return (getWidth() - verticalAxisRect.getWidth() - getPaddingLeft() - getPaddingRight());
     }
 
-    GestureDetector gd = new GestureDetector(this.getContext(), new OnGestureListener() {
-        @Override
-        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
-            scrollBy((int) distanceX, (int) distanceY);
-            return true;
-        }
-
-        @Override
-        public boolean onDown(MotionEvent e) {
-            return true;
-        }
-
-        @Override
-        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
-            return false;
-        }
-
-        @Override
-        public void onLongPress(MotionEvent e) {
-
-        }
-
-        @Override
-        public void onShowPress(MotionEvent e) {
-
-        }
-
-        @Override
-        public boolean onSingleTapUp(MotionEvent e) {
-            return false;
-        }
-    });
     private double maxDataValue;
 
     @Override
     public boolean onTouchEvent(MotionEvent event) {
-        return gd.onTouchEvent(event);
+        return gestureDetector.onTouchEvent(event);
     }
 
     @Override
@@ -433,6 +458,10 @@ public class BarGraphView extends View {
         invalidate();
     }
 
+    public BarGraphAdapter getAdapter() {
+        return adapter;
+    }
+
     public void setAdapter(BarGraphAdapter adapter) {
         this.adapter = adapter;
 
@@ -458,15 +487,8 @@ public class BarGraphView extends View {
      * @return
      */
     private double getGraphMaxValue(double maxDataValue) {
-        // odmah računamo sve potrebno, da se ne zamaramo s tim na onDraw
-        // vrijednost za graf
-        // zanima me kolko dekadskih znamenki ima najveća vrijednost
         int numberOfDigits = (int) Math.floor(Math.log10(maxDataValue)) + 1;
-        // osnova je 10^(broj_znakova - 1)
         int order = (int) Math.pow(10, numberOfDigits - 1);
-        // pocetak je na n*order tako da je n*order najmanji veći za neki cijeli
-        // broj n
-        // ovaj broj nam je u biti logički maksimum
         return ((int) (Math.ceil(maxDataValue / order))) * order;
     }
 
@@ -632,6 +654,14 @@ public class BarGraphView extends View {
         horizontalAxisRect.setDescription(desc);
     }
 
+    public String getNoDataText() {
+        return noDataText;
+    }
+
+    public void setNoDataText(String noDataText) {
+        this.noDataText = noDataText;
+    }
+
     private class BarConfig {
         private float padding;
         private float whiteSpace;

+ 1 - 0
app/src/main/res/values/attrs.xml

@@ -8,5 +8,6 @@
         <attr name="showAverageLine" format="boolean" />
         <attr name="showVerticalValues" format="boolean" />
         <attr name="showHorizontalValues" format="boolean" />
+        <attr name="noDataText" format="string" />
     </declare-styleable>
 </resources>

+ 1 - 0
graphsapp/.gitignore

@@ -0,0 +1 @@
+/build

+ 34 - 0
graphsapp/build.gradle

@@ -0,0 +1,34 @@
+apply plugin: 'com.android.application'
+
+android {
+    compileSdkVersion 26
+    buildToolsVersion "25.0.3"
+
+    defaultConfig {
+        applicationId "hr.tacitus.graphsapp"
+        minSdkVersion 19
+        targetSdkVersion 26
+        versionCode 1
+        versionName "1.0"
+
+        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
+
+    }
+    buildTypes {
+        release {
+            minifyEnabled false
+            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
+        }
+    }
+}
+
+dependencies {
+    compile fileTree(include: ['*.jar'], dir: 'libs')
+    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
+        exclude group: 'com.android.support', module: 'support-annotations'
+    })
+    compile 'com.android.support:appcompat-v7:26.+'
+    compile 'com.android.support.constraint:constraint-layout:1.0.2'
+    testCompile 'junit:junit:4.12'
+    compile project(':app')
+}

+ 25 - 0
graphsapp/proguard-rules.pro

@@ -0,0 +1,25 @@
+# Add project specific ProGuard rules here.
+# By default, the flags in this file are appended to flags specified
+# in C:\Users\Bero\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
+# You can edit the include path and order by changing the proguardFiles
+# directive in build.gradle.
+#
+# For more details, see
+#   http://developer.android.com/guide/developing/tools/proguard.html
+
+# Add any project specific keep options here:
+
+# If your project uses WebView with JS, uncomment the following
+# and specify the fully qualified class name to the JavaScript interface
+# class:
+#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
+#   public *;
+#}
+
+# Uncomment this to preserve the line number information for
+# debugging stack traces.
+#-keepattributes SourceFile,LineNumberTable
+
+# If you keep the line number information, uncomment this to
+# hide the original source file name.
+#-renamesourcefileattribute SourceFile

+ 26 - 0
graphsapp/src/androidTest/java/hr/tacitus/graphsapp/ExampleInstrumentedTest.java

@@ -0,0 +1,26 @@
+package hr.tacitus.graphsapp;
+
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.*;
+
+/**
+ * Instrumentation test, which will execute on an Android device.
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+@RunWith(AndroidJUnit4.class)
+public class ExampleInstrumentedTest {
+    @Test
+    public void useAppContext() throws Exception {
+        // Context of the app under test.
+        Context appContext = InstrumentationRegistry.getTargetContext();
+
+        assertEquals("hr.tacitus.graphsapp", appContext.getPackageName());
+    }
+}

+ 16 - 0
graphsapp/src/main/AndroidManifest.xml

@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="hr.tacitus.graphsapp">
+
+    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher"
+        android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"
+        android:supportsRtl="true" android:theme="@style/AppTheme">
+        <activity android:name=".MainActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+</manifest>

+ 64 - 0
graphsapp/src/main/java/hr/tacitus/graphsapp/MainActivity.java

@@ -0,0 +1,64 @@
+package hr.tacitus.graphsapp;
+
+import android.support.v7.app.AppCompatActivity;
+import android.os.Bundle;
+import android.view.View;
+
+import hr.tacitus.graphs.BarGraphAdapter;
+import hr.tacitus.graphs.BarGraphView;
+import java.text.DecimalFormat;
+
+public class MainActivity extends AppCompatActivity {
+
+    private BarGraphView bgv;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+
+        bgv = (BarGraphView) findViewById(R.id.bgvGraf);
+        updateAdapter();
+    }
+
+    public void toggleAdapter(View view) {
+        updateAdapter();
+    }
+
+    private BarGraphAdapter createSampleAdapter() {
+        return new BarGraphAdapter() {
+
+            double[] testValues = { 0.54, 3.77, 2.44, 2.00, 2.5, 5.32, 1., 10, 3.66, 2.11, 4.6, 5.76, 23.23, 7.99, 5.09, 6.7, 14.22, 4.55 };
+
+            @Override
+            public int getCount() {
+                return testValues.length;
+            }
+
+            @Override
+            public String getHorizontalValueDescription(int position) {
+                return Integer.toString(position);
+            }
+
+            @Override
+            public String getValueDescription(int position) {
+                return new DecimalFormat("0.00").format(testValues[position]);
+            }
+
+            @Override
+            public double getValue(int position) {
+                return testValues[position];
+            }
+
+            @Override
+            public String getVerticalMarkingText(double markingValue) {
+                return new DecimalFormat("0.0").format(markingValue);
+            }
+
+        };
+    }
+
+    private void updateAdapter() {
+        bgv.setAdapter(bgv.getAdapter() == null ? createSampleAdapter() : null);
+    }
+}

+ 44 - 0
graphsapp/src/main/res/layout/activity_main.xml

@@ -0,0 +1,44 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    tools:context="hr.tacitus.graphsapp.MainActivity"
+    >
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:id="@+id/toolbar">
+        <Button
+            android:id="@+id/button"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:onClick="toggleAdapter"
+            android:text="Toggle Adapter" />
+    </LinearLayout>
+
+
+    <hr.tacitus.graphs.BarGraphView
+        android:layout_below="@id/toolbar"
+        android:id="@+id/bgvGraf"
+        android:layout_width="fill_parent"
+        android:layout_height="fill_parent"
+        android:paddingTop="24dp"
+        android:paddingLeft="6dp"
+        android:paddingBottom="6dp"
+        android:paddingRight="6dp"
+
+        app:barTextSize="16sp"
+        app:barPadding="4dp"
+        app:yAxisText="Time"
+        app:xAxisText="Testiram"
+        app:axisValuesTextSize="12sp"
+        app:showAverageLine="false"
+        app:showHorizontalValues="true"
+        app:noDataText="Shame, no data"
+
+        />
+
+</RelativeLayout>

BIN
graphsapp/src/main/res/mipmap-hdpi/ic_launcher.png


BIN
graphsapp/src/main/res/mipmap-hdpi/ic_launcher_round.png


BIN
graphsapp/src/main/res/mipmap-mdpi/ic_launcher.png


BIN
graphsapp/src/main/res/mipmap-mdpi/ic_launcher_round.png


BIN
graphsapp/src/main/res/mipmap-xhdpi/ic_launcher.png


BIN
graphsapp/src/main/res/mipmap-xhdpi/ic_launcher_round.png


BIN
graphsapp/src/main/res/mipmap-xxhdpi/ic_launcher.png


BIN
graphsapp/src/main/res/mipmap-xxhdpi/ic_launcher_round.png


BIN
graphsapp/src/main/res/mipmap-xxxhdpi/ic_launcher.png


BIN
graphsapp/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png


+ 6 - 0
graphsapp/src/main/res/values/colors.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <color name="colorPrimary">#3F51B5</color>
+    <color name="colorPrimaryDark">#303F9F</color>
+    <color name="colorAccent">#FF4081</color>
+</resources>

+ 3 - 0
graphsapp/src/main/res/values/strings.xml

@@ -0,0 +1,3 @@
+<resources>
+    <string name="app_name">GraphsApp</string>
+</resources>

+ 11 - 0
graphsapp/src/main/res/values/styles.xml

@@ -0,0 +1,11 @@
+<resources>
+
+    <!-- Base application theme. -->
+    <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
+        <!-- Customize your theme here. -->
+        <item name="colorPrimary">@color/colorPrimary</item>
+        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
+        <item name="colorAccent">@color/colorAccent</item>
+    </style>
+
+</resources>

+ 17 - 0
graphsapp/src/test/java/hr/tacitus/graphsapp/ExampleUnitTest.java

@@ -0,0 +1,17 @@
+package hr.tacitus.graphsapp;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Example local unit test, which will execute on the development machine (host).
+ *
+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
+ */
+public class ExampleUnitTest {
+    @Test
+    public void addition_isCorrect() throws Exception {
+        assertEquals(4, 2 + 2);
+    }
+}

+ 1 - 1
settings.gradle

@@ -1 +1 @@
-include ':app'
+include ':app', ':graphsapp'